Your SlideShare is downloading. ×
Stack and subroutine
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Stack and subroutine


Published on

Published in: Technology

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Stack and Subroutine
  • 2. Stack
    • Is a section of memory set aside for storing return addresses.
    • It is also used to save the contents of registers for the calling program while a procedure executes.
    • Another use of stack is to hold data or addresses that will b acted upon by a procedure.
  • 3.
    • The 8086 lets u set aside up to an entire 64 kb of memory as stack.
    • It uses 2 registers-SS and SP.
    • SS-hold the upper 16 bits of the starting address given to the stack segment.
    • SP-hold the offset of the last word written on the stack.
    • 8086 produces the physical address for a stack location by adding the offset contained in the SP register to the SS base adrs represented by the 16 bit number in the SS register.
  • 4. PUSH & POP
    • Are 2 instructions that can operate on stack.
    • Push to Stack- PUSH SRC
    • Pop from stack- POP DST
  • 5. Subroutines (Procedures)
    • A subroutine is a set of code that can b branched to and returned from such a way that the code is as if it were inserted at the point from which it is branched to.
    • The branch to a subroutine is referred to as CALL and the corresponding branch back is known as RETURN.
    • The return is always made to the instruction immediately following the call.
  • 6.
    • CALL Subroutine A
    • .
    • .
    • CALL Subroutine A
    • .
    • .
    Subroutine . . . RET
  • 7.
    • Subroutines provide the primary means of breaking the code in a program to modules.
    • Requirements of subroutine:
    • 1)A procedure CALL must save the adrs of the next instruction , so that the return will b able to branch back to the proper place in the calling program.
    • 2)The registers r used by the procedure need to b stored before their contents r changed and then restored just before the procedure is exited.
  • 8. CALL & RET
    • R two instructions used with procedure.
    • CALL-not only branches to the indicated adrs but also pushes the return adrs on to the stack. CALL ProcName.
    • RET- it simply pops the return address from the stack.
  • 9. Sub Routine Eg.
    • .DATA
    • num1 dw 22
    • num2 dw 32
    • result dw 0
    • .CODE
    • addnum proc
      • mov ax, [num1]
      • mov bx, [num2]
      • add ax, bx
      • mov [result], ax
      • ret
    • addnum endp
  • 10.
      • start: mov ax, @data
      • mov ds, ax
      • call addnum ; add num1 and num2
      • mov ax, 4c00h
      • int 21h
      • END start
  • 11. A ssembly L anguage P rograms
    • Programs with an Assembler:
    • The assemblers used for programs here is 8086 Microsoft Macro assembler for the IBM PC.
    • For different assemblers the assembly language directive may change.
  • 12. Program Structure
    • TITLE ……..
    • DOS SEG
    • .MODEL
    • .STACK
    • .DATA
    • ………… ..
    • ………… ..
    • .CODE
    • Main PROC
    • ……………
    • ………… ..
    • Main ENDP
    • END Main
  • 13.
    • TITLE : identifies the program listing title. Any text typed to the right side of the directive is printed at the top of each page in the listing file.
    • DOS SEG : it directs the MASM to place the segments in the standard order.
    • .MODEL : selects a standard memory model for the programs.
    • .STACK : sets the size of the program stack which may b any size up to 64kb.
    • .CODE : identifies the part of the program that contains instructions .
    • PROC : creates a name and a address for the beginning of a procedure.
    • ENDP : indicates the end of the procedure.
    • DATA : all variables pertaining to the program r defined in the area following this directive called data segment.
    • END : terminates assembly of the program. Any lines of text placed after this directive is ignored.
  • 14. * Write an ALP to perform simple unsigned Multiplication. The two 16 bit numbers r 1121H and 1301H.Store the product in the location whose offset adrs is 8100H.
    • TITLE Multiplication
    • DOS SEG
    • .MODEL Small
    • .STACK 100H
    • DATA
      • Num1 DW 1121H
      • Num2 DW 1301H
    • .CODE
    • Main PROC
      • MOV AX, num1 ;Bring the first number to AX
      • MOV BX, num2 ;Bring second
      • MUL AX,BX ;Multiply the contents of AX and BX
      • MOV [8100H],AX ;store the result at 8100H
      • MOV AX,4C00H ;Return to DOS
      • INT 21 H
    • Main ENDP
    • END Main
  • 15. * An ALP to compare two 16 bit numbers stored in the AX and BX registers. If both r equal the increment SI register.
    • CMP AX, BX
    • JE LAB
    • NOP
    • LAB INC SI
    * An ALP to add two 16 bit numbers stored in the AX and BX registers. If no carry exists after the addition increment SI register.
    • ADD AX, BX
    • JNC LAB
    • NOP
    • LAB INC SI
  • 16. * Write an ALP to find the greatest number in a given series of 8-bit numbers. The length of the series is stored in a location whose 16 bit offst adrs is 8100H.the series begins from the location whose offset adrs is 8102H.Store the result in the location whose 16-bit offset adrs is 8150H.
    • TITLE Find the max. in a given series of data
    • DOS SEG
    • .MODEL Small
    • .STACK 100H
    • .DATA
        • List db 20,45,13,15,04,72
    • .CODE
    • Main PROC
        • MOV AX,@data ;initialise DS Register
        • MOV DS, AX
        • MOV SI,[8102H] ;initialize SI register
        • MOV AL,00H
        • MOV CX,OFFSET 8100H;Length of series in CX
    • BACK CMP AL,[SI] ;is next element>max
        • JNC LAB
        • MOV AL,[SI]
    • LAB INC SI
        • LOOP BACK ;repeat until CX=0
        • MOV [8150H],AL
        • MOV AX,4C00H ;return to DOS
        • INT 21 H
    • Main ENDP
    • END main
  • 17. * Write an ALP to find the sum of series of data. The length of the array is stored in a location whose 16-bit offset address is 8100H.the series begins from the location, whose offset 16-bit adrs is the result in location whose 16-bit offset is 8150H.
    • .DATA
        • list db 07,82,17,59,A3,3E
    • Main PROC
        • MOV AX,@data
        • MOV DS,AX
        • MOV DX,00H
        • MOV CX,[8100H]
        • MOV SI,OFFSET list
    • BACK ADD DL,[SI]
        • ADC DH,0
        • INC SI
        • LOOP BACK
        • MOV [8150],DX
        • MOV AX,4C00H
    • Main ENDP
        • END main
  • 18. * Write an ALP to evaluate an expression (a+b)*(c+d), where a,b,c and d are the hexa decimal bytes.
    • MOV AL,a
    • MOV BL,b
    • ADD AL,BL
    • PUSH AL
    • MOV AL,c
    • MOV BL,d
    • ADD AL,BL
    • MOV BL,AL
    • POP AL
    • MUL AL,BL