Stack operations in Assembly
A stack is a one dimensional data structure. The elements of the stack is added and removed from one end. That is why it is also known as
Last-In-First-Out or LIFO system. That end of the stack is called TOP.
You can have the Idea by watching the stack of papers to the right. It is only possible to add or remove new paper holders from the TOP.
The add operation on a stack is called push and the remove operation on the stack is called pop.
As we mentioned earlier the operations are only done at the top.
PUSH ( add element to top)
The push instruction adds a new element to the top. The syntax is
PUSH source ; no destination defined because destination is always TOP
A few things to notice is that the PUSH operation works as follows
Push Pop
POP ( remove element to top)
The pop instruction removes the top element. The syntax is
POP destination ; no source defined because source is always TOP
A few things to notice is that the PUSH operation works as follows
The PUSH and the POP instructions take 16 bit registers only. So
PUSHF ( a variation of PUSH)
There are no operand of PUSHF. It just copies the FLAGS register to the stack.
POPF ( a variation of POP)
There are no operand of POPF either , it does the reverse of PUSHF. Executing it restores the FLAGS register from the stack TOP.
Example ( Reversing a line of text )
The program shall take one character at a time and PUSH it to the stack until its a carriage return. When "Enter" is pressed the characters are POPed out and printed.
CODE:
TOP:
POP DX
INT 21H
LOOP TOP
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
A stack is a one dimensional data structure. The elements of the stack is added and removed from one end. That is why it is also known as
Last-In-First-Out or LIFO system. That end of the stack is called TOP.
You can have the Idea by watching the stack of papers to the right. It is only possible to add or remove new paper holders from the TOP.
The add operation on a stack is called push and the remove operation on the stack is called pop.
As we mentioned earlier the operations are only done at the top.
Stack Operations Push and Pop
The push instruction adds a new element to the top. The syntax is
PUSH source ; no destination defined because destination is always TOP
A few things to notice is that the PUSH operation works as follows
1) A copy of the source content is copied to the address specified by the TOP
2) SP or TOP is decreased
POP ( remove element to top)
The pop instruction removes the top element. The syntax is
POP destination ; no source defined because source is always TOP
A few things to notice is that the PUSH operation works as follows
1) SP or TOP is increased
2) The content of the TOP is copied to the destination
The PUSH and the POP instructions take 16 bit registers only. So
PUSH DL
and
PUSH 3
are both illegalPUSHF ( a variation of PUSH)
There are no operand of PUSHF. It just copies the FLAGS register to the stack.
POPF ( a variation of POP)
There are no operand of POPF either , it does the reverse of PUSHF. Executing it restores the FLAGS register from the stack TOP.
Example ( Reversing a line of text )
The program shall take one character at a time and PUSH it to the stack until its a carriage return. When "Enter" is pressed the characters are POPed out and printed.
CODE:
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH, 2
MOV DL, ‘?’
INT 21H
XOR CX, CX
MOV AH, 1
INT 21H
.STACK 100H
.CODE
MAIN PROC
MOV AH, 2
MOV DL, ‘?’
INT 21H
XOR CX, CX
MOV AH, 1
INT 21H
WHILE:
CMP AL, 0DH
JE END_WHILE
PUSH AX
INC CX
INT 21H
JMP WHILE
CMP AL, 0DH
JE END_WHILE
PUSH AX
INC CX
INT 21H
JMP WHILE
END_WHILE:
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
JCXZ EXIT
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
JCXZ EXIT
POP DX
INT 21H
LOOP TOP
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
No comments:
Post a Comment