Understanding the STACK

Heck, as long as I've mentioned it before, lets look at the STACK:

The STACK is an area of memory that has the properties of a STACK of plates- the last one you put on is the first one take off. The only difference is that the stack of plates is on the roof. (Ok, so that can't really happen... unless gravity was shut down...) Meaning that as you put another plate (or piece of data) on the stack, the STACK grows DOWNWARD. Meaning that the stack pointer is DECREASED after each PUSH, and INCREASED after each POP.

_____ Top of the allocated memory in the stack segment (SS)
þ
þ
þ
þ ® SP (the stack pointer points to the most recently pushed byte)

Truthfully, you don't need to know much more than a stack is Last In, First Out (LIFO).

WRONG ex:
push cx ;this swaps the contents of CX and AX
push ax ;of course, if you wanted to do this quicker, you'd
...
pop cx ;just say XCHG cx,ax
pop ax ; but thats not my point.

RIGHT ex:
push cx ;this correctly restores AX & CX
push ax
...
pop ax
pop cx

Now I'll do a quick run through on the assembler instructions that you MUST know:

Examples of different addressing modes:

MOV ax,5 ;moves and IMMEDIATE value into ax (think 'AX = 5')
MOV bx,cx ;moves a register into another register
MOV cx,[SI] ;moves [DS:SI] into cx (the Default Segment is Used)
MOV [DI+5],ax ;moves ax into [DS:DI+5]
MOV [ES:DI+BX+34],al ;same as above, but has a more complicated
;OFFSET (=DI+BX+34) and a SEGMENT OVERRIDE
MOV ax,[546] ;moves whats at [DS:546] into AX

Note that the last example would be totally different if the brackets were left out. It would mean that an IMMEDIATE value of 546 is put into AX, instead of what's at offset 546 in the Default Segment.

ANOTHER STANDARD NOTATION TO KNOW:
Whenever you see brackets [] around something, it means that it refers to what is AT that offset. For instance, say you had this situation:

MyData dw 55
...
mov ax,MyData

What is that supposed to mean? Is MyData an Immediate Value? This is confusing and for our purposes WRONG. The 'Correct' way to do this would be:

MyData dw 55
...
mov ax,[MyData]

This is clearly moving what is AT the address of MyData, which would be 55, and not moving the OFFSET of MyData itself. But what if you actually wanted the OFFSET? Well, you must specify directly.

MyData dw 55
...
mov ax,OFFSET MyData

Similiarly, if you wanted the SEGMENT that MyData was in, you'd do this:

MyData dw 55
...
mov ax,SEG MyData

See?I think you will get it after all this brief post. :-)
As for he next post I will describe about the INT. Wait and see.

No comments: