Chapter 4 Stack Organization
Chapter 4 Stack Organization
Stack Organization in AL
1
Stack
4. Stack instructions:- These instructions allow the use of the
stack to store or retrieve data.
• Stack is an area of memory for keeping temporary data
Two basic operations
1. POP
2. PUSH 2. POP INSTRUCTION
1. PUSH INSTRUCTION Purpose: It recovers a piece of
Purpose: It places data on the stack. information from the stack
• gets 16 bit value from the
• stores 16 bit value in the stack. stack.
Syntax: PUSH source Syntax: POP destiny
• The source can be:- • Destiny can be :-
• REG: • REG
• SREG • SREG
• Memory • memory
• immediate
2
Continued
REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, CS.
memory: [BX], [BX+SI+7], 16 bit variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
• The stack uses LIFO (Last In First Out) algorithm, this means
that if we push these values one by one into the stack.
• Example: push 1, 2, 3, 4, 5 into stack then the first value that
we will get on pop will be 5, then 4, 3, 2, and only then 1.
• It is very important to do equal number of PUSHs and POPs,
otherwise the stack maybe corrupted and it will be impossible
to return to operating system.
• PUSH and POP instruction are especially useful because we
don't have too much registers to operate with, so here is a
trick:
• Store original value of the register in stack (using PUSH).
• Use the register for any purpose. 3
Continued ….
• Restore the original value of the register from stack (using POP).
Algorithm:
1 SP = SP - 2
SS:[SP] (top of the stack) = operand
Algorithm:
Example: PUSH AX
MOV AX, 1234h 2
PUSH CX
PUSH AX PUSH DX
POP DX ; DX = 1234h PUSH BX
RET PUSH SP
Push all general purpose registers AX, PUSH BP
CX, DX, BX, SP, BP, SI, DI in the stack. PUSH SI
Original value of SP register (before PUSH DI
PUSH) is used.
4
Continued ….
Pop all general purpose registers DI, SI, BP, SP, BX, DX, CX, AX
from the stack.
SP value is ignored, it is Popped but not set to SP register).
Algorithm:
1. ORG 100h
• POP DI
2. MOV AX, 1234h
• POP SI
3. PUSH AX ; store value of AX in stack.
• POP BP
4. MOV AX, 5678h ; modify the AX value.
• POP xx (SP ignored)
5. POP AX ;restore the original value of AX.
• POP BX
6. RET
• POP DX
• POP CX
• POP AX
5
Continued …
1. ORG 100h
2. MOV AX, 1212h ; store 1212h in AX.
3. MOV BX, 3434h ; store 3434h in BX
4. PUSH AX ; store value of AX in stack.
5. PUSH BX ; store value of BX in stack.
6. POP AX ; set AX to original value of BX.
7. POP BX ; set BX to original value of AX.
8. RET
• The exchange happens because stack uses LIFO (Last In
First Out) algorithm, so when we push 1212h and then
3434h, on pop we will first get 3434h and only after it
1212h.
6
1. Write an assembly language program to reverse five
number/digits
1. org 100h 16. printn
2. include 'emu8086.inc' 17. print 'The reverse order is:'
3. .data 18. l2:
4. num dw 1 19. pop bx
5. .code 20. mov dx,bx
6. mov cx,5 21. mov ah,02h
7. print 'Enter five integer numbers: ' 22. int 21h
8. l1: 23. loop l2
9. mov ah,01h 24. ret
10. int 21h
11. mov ax,ax
12. push ax
13. inc num Output:
14. loop l1
15. mov cx,num
Enter five integer numbers: 12345
The reverse order is: 54321
7
Continued ….
PUSH B
SP <- SP-1
SP <- B ;transfer high order bit to TOS
SP <- SP-1
SP <- C ;transfer low order bit to TOS
Registers Memory
A SP 23 0008
SP 06 0007
B 06 40 C SP 40 0006
D E 0005
0004
H L 0003
0002
0001
PUSH: Push the register pair onto the stack
Registers Memor
SP y 03 0008
A
SP 06 0007
B 06 40 C SP 40 0006
D 0005
E
0004
H L 0003
0002
0001
POP B
C <- SP ; transfer to low order bit from TOS
SP <- SP+1
1. org 100h
2. .data
3. msg1 db "Assembly$"
4. msg2 db "Language$" 22. dispSpace Proc
5. msg3 db "Programming$" 23. mov dx,0Dh
6. .code 24. mov ah,02h
7. Main Proc 25. int 21h
8. mov ax,@data 26. mov dx,0Ah
9. mov ds,ax 27. mov ah,02h
10. mov dx,offset msg1 28. int 21h
11. mov ah,09h 29. dispSpace ENDP
12. int 21h 30. ret
13. call dispSpace
14. mov dx,offset msg2 Output:
15. mov ah,09h
16. int 21h Assembly
17. call dispSpace
18. mov dx,offset msg3
Language
19. mov ah,09h Programming
20. int 21h
21. Main ENDP
2. Write an assembly prog code to determine sixteen bit integer
number is even or odd using procedure.
1. org 100h
2. include 'emu8086.inc'
3. print "Enter the number :"
4. proc1 proc
5. mov ah,01h
6. int 21h
7. mov ax,ax
8. mov bx,2
9. div bx
10. cmp dx,0
11. je even
12. printn
13. print "The number is ODD"
14. jmp exit
15. even:
16. printn
17. print "The number is EVEN"
18. exit:
19. ret
20. proc1 endp
3. Write an assembly language program to compute the following operations
of two integer numbers to be read from key board using Procedures.
4. Write an assembly language program that takes an eight-bit integer number from the
keyboard and determines whether it is negative or positive
Hint: mov ah,01h
int 21h ; Instruction code to take inputs
END?
18