Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Stack and Subroutine
Stack <ul><li>Is a section of memory set aside for storing return addresses. </li></ul><ul><li>It is also used to save the...
<ul><li>The 8086 lets u set aside up to an entire 64 kb of memory as stack. </li></ul><ul><li>It uses 2 registers-SS and S...
PUSH & POP <ul><li>Are 2 instructions that can operate on stack. </li></ul><ul><li>Push to Stack- PUSH SRC </li></ul><ul><...
Subroutines (Procedures) <ul><li>A subroutine is a set of code that can b branched to and returned from  such a way that t...
<ul><li>CALL Subroutine A </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>CALL Subroutine A </li></ul><ul><li>. ...
<ul><li>Subroutines provide the primary means of breaking the code in a program to modules. </li></ul><ul><li>Requirements...
CALL & RET <ul><li>R two instructions used with procedure. </li></ul><ul><li>CALL-not only branches to the indicated adrs ...
Sub Routine Eg. <ul><li>.DATA  </li></ul><ul><li>num1 dw 22  </li></ul><ul><li>num2 dw 32  </li></ul><ul><li>result dw 0  ...
<ul><ul><li>start: mov ax, @data </li></ul></ul><ul><ul><li>mov ds, ax  </li></ul></ul><ul><ul><li>call addnum   ; add num...
A ssembly  L anguage  P rograms <ul><li>Programs with an Assembler: </li></ul><ul><li>The assemblers used for programs her...
Program Structure <ul><li>TITLE …….. </li></ul><ul><li>DOS SEG </li></ul><ul><li>.MODEL </li></ul><ul><li>.STACK </li></ul...
<ul><li>TITLE : identifies the program listing title. Any text typed to the right side of the directive is printed at the ...
* Write an ALP to perform simple unsigned Multiplication. The two 16 bit numbers r 1121H and 1301H.Store the product in th...
* An ALP to compare two 16 bit numbers stored in the AX and BX registers. If both r equal the increment SI register. <ul><...
* 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 loc...
* 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 addr...
* Write an ALP to evaluate an expression  (a+b)*(c+d),  where a,b,c and d are the hexa decimal bytes. <ul><li>MOV AL,a </l...
Upcoming SlideShare
Loading in …5
×

Stack and subroutine

20,638 views

Published on

Published in: Technology

Stack and subroutine

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

×