Eshmam
Eshmam

Reputation: 53

What is a stack in assembly and how does the command .stack work?

I'm just gonna jump straight into it: I'm learning assembly and am using turbo assembler. I literally just started learning so I'm trying to familiarize myself with some pre-defined services of the language and also am reading up on registers so that I can better understand why what's happening is actually happening. However, I can't seem to wrap my mind around what a stack actually means in this instance.

The way I understand stack is that it follows the procedure of LIFO regarding how instructions and data are treated. So what does the command .stack 100h do?

Am I assigning a limit to amount of things that can be loaded in the stack?

So what does this line actually do?:

.stack 100h

If it accepts numbers in other bases, is .stack 100h the same as .stack 256d? Will there be any technical or internal difference?

Upvotes: 0

Views: 1760

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 363980

.stack sets metadata in a .exe that affects how much memory the program-loader allocates for the stack segment.

One way for this to work is that on entry to a program, the SS base address is the lowest usable stack address and SP = the size you gave to .stack. If SP was higher, you could accidentally use more than .stack bytes without having SP wrap around, creating a stack-clash with something else. (IDK if there's a standard that requires an EXE program-loader to choose a specific offset for SP.)

It doesn't create any extra instructions inside your program. Its argument is just a numeric constant; you can specify it in any number base you like, depending on your assembler.


.stack also has no effect for .com programs: those start with cs=ds=es=ss with SP=0fffeH (or lower if less than 64kiB of contiguous memory is available).

(In a .com, SP points to a return address of 0 (in the PSP), where there's an int 20h instruction that will make an exit system call. So before pushing that 0, SP started at 0000h)


This answer is a summary of comments, thanks to @Jester, @rcgldr and @MichaelPetch for confirming that it is as simple as it looks, and pointing out the difference between .exe can .com programs.

Upvotes: 4

Related Questions