stack and subroutine

18

Click here to load reader

Upload: ashim-saha

Post on 14-Jun-2015

8.857 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Stack and subroutine

Stack and SubroutineStack and Subroutine

Page 2: Stack and subroutine

StackStack► Is a section of memory set aside for Is a section of memory set aside for

storing return addresses.storing return addresses.► It is also used to save the contents of It is also used to save the contents of

registers for the calling program while a registers for the calling program while a procedure executes.procedure executes.

►Another use of stack is to hold data or Another use of stack is to hold data or addresses that will b acted upon by a addresses that will b acted upon by a procedure. procedure.

Page 3: Stack and subroutine

► The 8086 lets u set aside up to an entire 64 kb The 8086 lets u set aside up to an entire 64 kb of memory as stack.of memory as stack.

► It uses 2 registers-SS and SP.It uses 2 registers-SS and SP.► SS-hold the upper 16 bits of the starting SS-hold the upper 16 bits of the starting

address given to the stack segment.address given to the stack segment.► SP-hold the offset of the last word written on SP-hold the offset of the last word written on

the stack.the stack.► 8086 produces the physical address for a stack 8086 produces the physical address for a stack

location by adding the offset contained in the location by adding the offset contained in the SP register to the SS base adrs represented by SP register to the SS base adrs represented by the 16 bit number in the SS register.the 16 bit number in the SS register.

Page 4: Stack and subroutine

PUSH & POPPUSH & POP

►Are 2 instructions that can operate on Are 2 instructions that can operate on stack.stack.

►Push to Stack-Push to Stack- PUSH SRCPUSH SRC►Pop from stack-Pop from stack- POP DSTPOP DST

Page 5: Stack and subroutine

Subroutines (Procedures)Subroutines (Procedures)►A subroutine is a set of code that can b A subroutine is a set of code that can b

branched to and returned from such a branched to and returned from such a way that the code is as if it were inserted way that the code is as if it were inserted at the point from which it is branched to.at the point from which it is branched to.

►The branch to a subroutine is referred to The branch to a subroutine is referred to as CALL and the corresponding branch as CALL and the corresponding branch back is known as RETURN.back is known as RETURN.

►The return is always made to the The return is always made to the instruction immediately following the call.instruction immediately following the call.

Page 6: Stack and subroutine

►CALL Subroutine ACALL Subroutine A► ..► ..►CALL Subroutine ACALL Subroutine A► ..► ..

Subroutine

.

.

.

RET

Page 7: Stack and subroutine

►Subroutines provide the primary means Subroutines provide the primary means of breaking the code in a program to of breaking the code in a program to modules.modules.

►Requirements of subroutine:Requirements of subroutine:►1)A procedure CALL must save the adrs 1)A procedure CALL must save the adrs

of the next instruction , so that the of the next instruction , so that the return will b able to branch back to the return will b able to branch back to the proper place in the calling program.proper place in the calling program.

►2)The registers r used by the procedure 2)The registers r used by the procedure need to b stored before their contents r need to b stored before their contents r changed and then restored just before changed and then restored just before the procedure is exited.the procedure is exited.

Page 8: Stack and subroutine

CALL & RETCALL & RET

►R two instructions used with R two instructions used with procedure.procedure.

►CALL-not only branches to the CALL-not only branches to the indicated adrs but also pushes the indicated adrs but also pushes the return adrs on to the stack. CALL return adrs on to the stack. CALL ProcName.ProcName.

►RET- it simply pops the return address RET- it simply pops the return address from the stack.from the stack.

Page 9: Stack and subroutine

Sub Routine Eg.Sub Routine Eg.► .DATA .DATA ► num1 dw 22 num1 dw 22 ► num2 dw 32 num2 dw 32 ► result dw 0 result dw 0 ► .CODE .CODE ► addnumaddnum procproc

mov ax, [num1]mov ax, [num1] mov bx, [num2] mov bx, [num2] add ax, bx add ax, bx mov [result], ax mov [result], ax ret ret

► addnum endpaddnum endp

Page 10: Stack and subroutine

start: mov ax, @datastart: mov ax, @data mov ds, ax mov ds, ax call addnumcall addnum ; add num1 and num2 ; add num1 and num2 mov ax, 4c00hmov ax, 4c00h int 21h int 21h END start END start

Page 11: Stack and subroutine

AAssembly ssembly LLanguage anguage PProgramsrograms

►Programs with an Assembler:Programs with an Assembler:►The assemblers used for programs here The assemblers used for programs here

is 8086 Microsoft Macro assembler for is 8086 Microsoft Macro assembler for the IBM PC.the IBM PC.

►For different assemblers the assembly For different assemblers the assembly language directive may change.language directive may change.

Page 12: Stack and subroutine

Program StructureProgram Structure► TITLE ……..TITLE ……..► DOS SEGDOS SEG► .MODEL.MODEL► .STACK.STACK► .DATA.DATA► ……………………....► ……………………....► .CODE.CODE► Main PROCMain PROC► …………………………► ……………………....► Main ENDPMain ENDP

► END MainEND Main

Page 13: Stack and subroutine

► TITLETITLE: identifies the program listing title. Any text typed : identifies the program listing title. Any text typed to the right side of the directive is printed at the top of to the right side of the directive is printed at the top of each page in the listing file.each page in the listing file.

► DOS SEGDOS SEG: it directs the MASM to place the segments in : it directs the MASM to place the segments in the standard order.the standard order.

► .MODEL.MODEL: selects a standard memory model for the : selects a standard memory model for the programs.programs.

► .STACK.STACK: sets the size of the program stack which may b : sets the size of the program stack which may b any size up to 64kb.any size up to 64kb.

► .CODE.CODE: identifies the part of the program that contains : identifies the part of the program that contains instructions .instructions .

► PROCPROC: creates a name and a address for the beginning of : creates a name and a address for the beginning of a procedure.a procedure.

► ENDPENDP: indicates the end of the procedure.: indicates the end of the procedure.► DATADATA: all variables pertaining to the program r defined in : all variables pertaining to the program r defined in

the area following this directive called data segment.the area following this directive called data segment.► ENDEND: terminates assembly of the program. Any lines of : terminates assembly of the program. Any lines of

text placed after this directive is ignored.text placed after this directive is ignored.

Page 14: Stack and subroutine

**Write an ALP to perform simple unsigned Write an ALP to perform simple unsigned Multiplication. The two 16 bit numbers r 1121H and Multiplication. The two 16 bit numbers r 1121H and 1301H.Store the product in the location whose offset 1301H.Store the product in the location whose offset adrs is 8100H.adrs is 8100H.

► TITLE MultiplicationTITLE Multiplication► DOS SEGDOS SEG► .MODEL Small.MODEL Small► .STACK 100H.STACK 100H► DATADATA

Num1 DW 1121HNum1 DW 1121H Num2 DW 1301HNum2 DW 1301H

► .CODE.CODE► Main PROCMain PROC

MOV AX, num1MOV AX, num1 ;Bring the first number to AX;Bring the first number to AX MOV BX, num2MOV BX, num2 ;Bring second;Bring second MUL AX,BXMUL AX,BX ;Multiply the contents of AX ;Multiply the contents of AX

and BXand BX MOV [8100H],AXMOV [8100H],AX ;store the result at 8100H;store the result at 8100H MOV AX,4C00HMOV AX,4C00H ;Return to DOS;Return to DOS INT 21 HINT 21 H

► Main ENDPMain ENDP

► END MainEND Main

Page 15: Stack and subroutine

**An ALP to compare two 16 bit numbers stored in the An ALP to compare two 16 bit numbers stored in the AX and BX registers. If both r equal the increment SI AX and BX registers. If both r equal the increment SI register.register.

► CMP AX, BXCMP AX, BX► JE LABJE LAB► NOPNOP► LABLAB INC SIINC SI

**An ALP to add two 16 bit numbers stored in the AX An ALP to add two 16 bit numbers stored in the AX and BX registers. If no carry exists after the addition and BX registers. If no carry exists after the addition increment SI register.increment SI register.

► ADD AX, BXADD AX, BX► JNC LABJNC LAB► NOPNOP► LABLAB INC SIINC SI

Page 16: Stack and subroutine

**Write an ALP to find the greatest number in a given series of 8-bit 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 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 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.8102H.Store the result in the location whose 16-bit offset adrs is 8150H.

► TITLE Find the max. in a given series of dataTITLE Find the max. in a given series of data► DOS SEGDOS SEG► .MODEL Small.MODEL Small► .STACK 100H.STACK 100H► .DATA.DATA

► List db 20,45,13,15,04,72List db 20,45,13,15,04,72► .CODE.CODE► Main PROCMain PROC

► MOV AX,@dataMOV AX,@data ;initialise DS Register;initialise DS Register► MOV DS, AXMOV DS, AX► MOV SI,[8102H]MOV SI,[8102H] ;initialize SI register;initialize SI register► MOV AL,00HMOV AL,00H► MOV CX,OFFSET 8100H;Length of series in MOV CX,OFFSET 8100H;Length of series in

CXCX► BACK CMP AL,[SI]BACK CMP AL,[SI] ;is next element>max;is next element>max

► JNC LABJNC LAB► MOV AL,[SI]MOV AL,[SI]

► LAB INC SILAB INC SI► LOOP BACKLOOP BACK ;repeat until CX=0;repeat until CX=0► MOV [8150H],ALMOV [8150H],AL► MOV AX,4C00HMOV AX,4C00H ;return to DOS;return to DOS► INT 21 HINT 21 H

► Main ENDPMain ENDP

► END END mainmain

Page 17: Stack and subroutine

**Write an ALP to find the sum of series of data. The length of the 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 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 series begins from the location, whose offset 16-bit adrs is 8102H.store the result in location whose 16-bit offset is 8150H.8102H.store the result in location whose 16-bit offset is 8150H.

► .DATA.DATA► list db 07,82,17,59,A3,3Elist db 07,82,17,59,A3,3E

► Main PROCMain PROC► MOV AX,@dataMOV AX,@data► MOV DS,AXMOV DS,AX► MOV DX,00HMOV DX,00H► MOV CX,[8100H]MOV CX,[8100H]► MOV SI,OFFSET listMOV SI,OFFSET list

► BACK ADD DL,[SI]BACK ADD DL,[SI]► ADC DH,0ADC DH,0► INC SIINC SI► LOOP BACKLOOP BACK► MOV [8150],DXMOV [8150],DX► MOV AX,4C00HMOV AX,4C00H

► Main ENDPMain ENDP► END mainEND main

Page 18: Stack and subroutine

**Write an ALP to evaluate an expression Write an ALP to evaluate an expression (a+b)*(c+d),(a+b)*(c+d), where a,b,c and d are the hexa decimal bytes.where a,b,c and d are the hexa decimal bytes.

►MOV AL,aMOV AL,a►MOV BL,bMOV BL,b► ADD AL,BLADD AL,BL► PUSH ALPUSH AL►MOV AL,cMOV AL,c►MOV BL,dMOV BL,d► ADD AL,BLADD AL,BL►MOV BL,ALMOV BL,AL► POP ALPOP AL►MUL AL,BLMUL AL,BL