Assembly - How to modify stack size?

后端 未结 2 739
耶瑟儿~
耶瑟儿~ 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 13:37

    Whatever loaded ("the loader") your program into memory, and passed control to it, determines where in memory the stack is located, and how much space is available for the stack.

    It does so by the simple artifice of loading the stack pointer, typically using a MOV ESP, ... instruction before calling/jumping to your code. Your program then uses the stack area supplied.

    If your program uses too much, it will write beyond the end of the allocated stack area. This is a program bug, because the memory past the end may be allocated for some other purpose in the application. Writing on that other memory is likely to change the program behavior (e.g., "bug") when that memory gets used, and finding the cause of that bug is likely to be difficult (people assume that stacks don't damage program data and vice versa).

    If your application wants to use a larger stack, generally all you have to do is allocate your own area, large enough for your purposes, and do a MOV ESP, ... yourself to set the stack to the chosen location. How you allocate an area depends on the execution environment in which you run. (You need to respect ESP conventions: must be a multiple of 4, should be initialized to the bottom of a cache line, often useful to initialize to the bottom of virtual memory page).

    It is generally a good idea when "switching" stacks to save the old value of ESP provided by the loader, and restore ESP to that old value before returning control to the loader/caller/OS. Likewise, you should free the extended stack space no longer being used.

    This scheme will work if you know the amount of stack space you need in advance. In practice, this is rather hard to "guess" (and may be impossible if your code has a recursive algorithm that nests deeply). So you can either pick a really huge number bigger than you need (ick) or you can use an organized approach to switch stacks when it is clear to the program that it needs more. See How does a stackless language work? for more discussion.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题