Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java.

Similar presentations


Presentation on theme: "Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java."— Presentation transcript:

1 Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java or C++ function

2 Procedures  A procedure is a logically self-contained unit of code  Called sometimes a function, subprogram, or subroutine  Receives a list of parameters, also called arguments  Performs computation and returns results  Plays an important role in modular program development  Example of a procedure (called function) in C language int sumof ( int x,int y,int z ) { int temp; temp = x + y + z; return temp; }  The above function sumof can be called as follows: sum = sumof( num1,num2,num3 ); Result type Return function result Formal parameter list

3 Defining a Procedure in Assembly  Assembler provides two directives to define procedures  PROC to define name of procedure and mark its beginning  ENDP to mark end of procedure  A typical procedure definition is procedure_name PROC... ; procedure body... RET ;to return to caller procedure_name ENDP  procedure_name should match in PROC and ENDP

4 Example of a Procedure Definition  The sumof procedure receives three integer parameters  Assumed to be in AX, BX, and CX  Computes and returns result in register AX ;------------------------------------------------ ; Sumof: Calculates the sum of three integers ; Receives: AX, BX, CX, the three integers ; Returns: AX = sum, and the flags:CF,OF,ZF,SF ; Requires: nothing ;------------------------------------------------ sumof PROC addAX, BX; AX = AX + second number addAX, CX; AX = AX + third number ret; return to caller sumof ENDP  The ret instruction returns control to the caller

5 The Call Instruction  To invoke a procedure, the call instruction is used  The call instruction has the following format call procedure_name  Example on calling the procedure sumof  Caller passes actual parameters in AX, BX, and CX  Before calling procedure sumof movAX, num1; pass first parameter in AX movBX, num2; pass second parameter in BX movCX, num3; pass third parameter in CX callsumof; result is in EAX movsum, AX; save result in variable sum  call sumof will call the procedure sumof

6 How a Procedure Call / Return Works  How does a procedure know where to return?  There can be multiple calls to same procedure in a program  Procedure has to return differently for different calls  It knows by saving the return address (RA) on the stack  This is the address of next instruction after call  The call instruction does the following  Pushes the return address on the stack  Jumps into the first instruction inside procedure  ESP = ESP – 4; [ESP] = RA; EIP = procedure address  The ret (return) instruction does the following  Pops return address from stack  Jumps to return address: EIP = [ESP]; ESP = ESP + 4

7 CALL-RET Example main PROC 00000020 call MySub 00000025 mov ax,bx. main ENDP MySub PROC 00000040 mov ax,dx. ret MySub ENDP 0000025 is the offset of the instruction immediately following the CALL instruction 00000040 is the offset of the first instruction inside MySub

8 CALL-RET Example The CALL instruction pushes 00000025 onto the stack, and loads 00000040 into EIP The RET instruction pops 00000025 from the stack into EIP

9 Free Area Allocated Before Call ESP = 0012FFC4 Details of CALL and Return ESP AddressMachine CodeAssembly Language.CODE main PROC 00401020A1 00405000movEAX, num1 004010258B 1D 00405004movEBX, num2 0040102B8B 0D 00405008movECX, num3 00401031E8 0000004Bcallsumof 00401036A3 0040500Cmovsum, EAX......... exit main ENDP sumof PROC 0040108103 C3addEAX, EBX 0040108303 C1addEAX, ECX 00401085C3ret sumof ENDP END main RA=00401036 IP-relative call EIP = 00401036 0000004B EIP = 00401081 + After Call ESP = 0012FFC0 After Ret (Return) ESP = 0012FFC4 Runtime Stack

10 Don’t Mess Up the Stack ! Just before returning from a procedure ›Make sure the stack pointer ESP is pointing at return address Example of a messed-up procedure ›Pushes EAX on the stack before returning ›Stack pointer ESP is NOT pointing at return address! main PROC callmessedup... exit main ENDP messedupPROC pushEAX ret messedupENDP Free Area Used high addr Stack ESP Return Addr ESP EAX Value Where to return? EAX value is NOT the return address!

11 Nested Procedure Calls By the time Sub3 is called, the stack contains all three return addresses main PROC.. call Sub1 exit main ENDP Sub1 PROC.. call Sub2 ret Sub1 ENDP Sub2 PROC.. call Sub3 ret Sub2 ENDP Sub3 PROC.. ret Sub3 ENDP return address of call Sub3 ESP return address of call Sub2 return address of call Sub1

12 Example: Find the product of two positive integers A and B, by addition and bit shifting using procedure Pseudo Algorithm: Product=0 REPEAT IF lsb of B is 1 (Recall 1sb= LSB) THEN Product= Product +A END_IF Shift Left A, Shift Right B UNTIL B=0

13 Assembly Code TITLE PGM8_2: Multiplication by ADD AND SHIFT. MODEL SMALL. STACK 100H.CODE MAIN PROC ; EXECUTE IN DEBUG. Place A in AX and B in BX Mov Ax,A Mov Bx,B CALL MULTIPLY ;DX will contain the product MOV AH,4CH INT 21h MAIN ENDP MULTIPLY PROC ; AX=A, BX=B, in range 0-FFh, OUTPUT=DX=product=DX PUSH AX PUSH BX XOR DX,DX,

14 REPEAT: ; if B is odd TEST BX,1 ;is B odd? JZ END_IF ;no. is even ADD DX,AX ; product=product+A END_IF: SHL AX,1 ;shift left A SHR BX,1 ; shift right B ; until JNZ REPEAT POP BX POP AX RET MULTIPLY ENDP END MAIN

15 Ex: Write a procedure REVERSE that will reverse an array of N- word ; that is the N th word becomes the first, and ( N-1) word become the second The procedures is entered with SI pointing to array, Bx has N, the number of the words solution: Exchange the 1 st element with N th element, and the 2 nd with N-1 element. The number of exchanges will be N/2.model small.stack 100h.data Array dw 3456,2AB3, 8234,6789,2030,4080,BC3A,7890,2390,ABC4,7890,2C3D.code Main proc Mov si, OFFSET array Mov Bx, LENGTHOF Array Call REVERSE Mov 4ch,ah int 21h Main endp

16 REVERSE Proc Push AX Push BX Push si Push CX Mov CX,BX ; Cx=N Mov DX,SI Dec BX ; Bx=N-1 SHL BX,1 ; Bx=2*(N-1) Add DX,BX ; DX=2(N-1)+SI SHR CX,1 ; CX=N/2 ; Swap operation xx: Mov BX,[SI] XCHG BX, [DX] Mov [SI], BX Add SI,2 Sub DX,2 Loop xx Ret REVERSE endp End main


Download ppt "Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java."

Similar presentations


Ads by Google