Assembly - How to modify stack size?

后端 未结 2 738
耶瑟儿~
耶瑟儿~ 2021-01-16 13:09

I am a newbie in assembly programming and I am using push and pop instructions that use the memory stack. So, What is the stack default size, How to modify it and What is th

2条回答
  •  北海茫月
    2021-01-16 14:01

    Stack size depends upon a lot of factors. It depends on where you start the stack, how much memory you have, what CPU you are using etc. The CPU you are using is not called a "Windows CPU". If you are specifying what CPU you are using, you specify the name of that CPU in detail and also, very important, the architecture of the CPU. In this case, you are probably using x86 architecture.

    Here is a memory map for x86 architecture:

    
    All addresses Before 0X100000 - Free
    0x100000 - 0xc0000 - BIOS
    0xc0000 - 0xa0000 - Video Memory
    0xa0000 - 0x9fc00 - Extended BIOS data area
    0x9fC00 - 0x7e00 - Free
    0x7e00 - 0x7c00 - Boot loader
    0x7c00 - 0x500 - Free
    0x500 - 0x400 - BIOS data area
    0x400 - 0x00 - Interupt vector table

    In x86, stack information is held by two registers:

    
        Base pointer (bp): Holds starting address of the stack
        Stack pointer (sp): Holds the address in which next value will be stored
    

    These registers have different names in different modes:

    
                                `Base pointer           Stack pointer`
        16 bit real mode:       bp                     sp
        32 bit protected mode:  ebp                    esp
        64 bit mode:            rbp                    rsp
    

    When you set up a stack, stack pointer and base pointer gets the same address.

    Stack is setup in the address specified in base pointer register.
    You can set up your stack anywhere in memory that is free and the stack grows downwards.

    Each time you "push" something on to the stack, the value is stored in the address specified by stack pointer (which is same as base pointer at the beginning), and the stack pointer register is decremented.

    Each time you "pop" something from the stack, the value stored in address specified by stack pointer register is stored in a register specified by the programmer and the stack pointer register is incremented.

    In 16 bit real mode, you "push" and "pop" 16 bits. So each time you "push" or "pop", The stack pointer register is decremented or incremented by 0x02, since each address holds 8 bits..

    In 32 bit protected mode, you "push" and "pop" 32 bits. So each time you "push" or "pop", The stack pointer register is decremented or incremented by 0x04, since each address holds 8 bits.

    You will have to setup the stack in the right place dpending upon how many values you are going to be "pushing".

    If you keep "pushing" your stack keeps growing downwards and at some point of time your stack may overwrite something. So be wise and set up the stack in a address in the memory where there is plenty of room for the stack to grow downwards.

    For example:
    If you setup your stack at 0x7c00, just below the bootloader and you "push" too many values, your stack might overwrite the BIOS data area at some point of time which causes a lot of errors.

    You should have a basic idea of a stack and the size of it by now.

提交回复
热议问题