CHAPTER FOUR
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 ….
• The stack memory area is set by SS (Stack Segment) register,
  and SP (Stack Pointer) register. Generally operating system
  sets values of these registers on program start.
 "PUSH source" instruction does the following:
     Subtract 2 from SP register.
     Write the value of source to the address SS:SP.
 "POP destination" instruction does the following:
     Write the value at the address SS:SP to destination.
     Add 2 to SP register.
 The current address pointed by SS:SP is called the top of the stack. For COM
  files stack segment is generally the code segment, and stack pointer is set to value
  of 0FFFEh. At the address SS:0FFFEh stored a return address for RET
  instruction that is executed in the end of the program.
                                                                   8
Example: PUSH Instruction
     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
          Instruction                    Description                   Example
        Opcode   Operand
         PUSH      Rp      The contents of the register pair           PUSH B
                           designated in the operand are copied
                           onto the stack in the following
                           sequence.
                           1.    The SP register is decremented
                                 and the contents of the high order
                                 register (B, D, H) are copied into
                                 that location.
                           2.    The SP register is decremented
                                 again and the contents of the low-
                                 order register (C, E, L) are copied
                                 to that location.
Example: POP Instruction
                  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
            B <- SP      ; transfer to high order bit from TOS
            SP <- SP+1
POP: Pop off stack to the register pair
            Instruction                    Description                    Example
         Opcode   Operand
         POP         Rp     The contents of the memory location           POP B
                            pointed out by the stack pointer register
                            are copied to the low-order register (C,
                            E, L) of the operand.
                            1.     The stack pointer is incremented
                                   by 1 and the contents of that
                                   memory location are copied to the
                                   high-order register (B, D, H) of the
                                   operand.
                            2.     The stack pointer register is again
                                   incremented by 1.
Procedures in Assembly Language
Procedure in assembly language/function in a high-level
 programming language
Why procedure?
  Easy to manage
  Easy to write a program
  Code reusability
  Reduce complexity
  Eliminate duplicate code
  AL language codes are complex & using Procedure
   becomes easy to manage and simple
                       PROCEDURE CALL
Procedures can be called using CALL key word followed by
 procedure_name.
Syntax: Procedure_name PROC • Syntax: Procedure_name:
          ; statements
                           OR •          ; statements
          ; statements       •              ; statements
       Procedure_name ENDP •                Ret
Look for the following examples
1. What is the output of the following program?
 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.
 ===============select your choice===================
 ================1: for ADDITION====================
 ================2: for subtraction===================
 ================3: DIVISION=======================
 ================4: MULTIPLICATION================
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