Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, 2010. All rights reserved. You may modify and copy this slide.

Similar presentations


Presentation on theme: "Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, 2010. All rights reserved. You may modify and copy this slide."— Presentation transcript:

1 Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, 2010. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed. Slides prepared by the author Revision date: 2/15/2010 Kip R. Irvine

2 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 2 Creating Procedures Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java or C/C++ function Following is an assembly language procedure named sample: To transfer control to the procedure ProcName we do: CALL ProcName The RET instruction transfers control to the instruction immediately following CALL sample PROC. ret sample ENDP

3 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 3 Documenting Procedures A description of all tasks accomplished by the procedure. Receives: A list of input parameters; state their usage and requirements. Returns: A description of values returned by the procedure. Requires: Optional list of requirements called preconditions that must be satisfied before the procedure is called. Suggested documentation for each procedure: If a procedure is called without its preconditions satisfied, it will probably not produce the expected output.

4 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 4 Example: SumOf Procedure SumOf PROC ;--------------------------------------------------------- ; ; Calculates and returns the sum of three 32-bit integers. ; Receives: EAX, EBX, ECX, the three integers. May be ; signed or unsigned. ; Returns: EAX = sum, and the status flags (Carry, ; Overflow, etc.) are changed. ; Requires: nothing ;--------------------------------------------------------- add eax,ebx add eax,ecx ret SumOf ENDP

5 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 5 CALL and RET Instructions The CALL instruction calls a procedure pushes offset of next instruction on the stack ESP is decremented by 4 The content of EIP is copied at the dword pointed by ESP (Note: the content of EIP is the offset address of the instruction following CALL: where the procedure must return) copies the address of the called procedure into EIP The offset address of the first instruction in the called procedure is copied into EIP (this will thus be the next instruction to execute) The RET instruction returns from a procedure pops top of stack into EIP The dword pointed by ESP is copied into EIP ESP is incremented by 4 (the instruction pointed by EIP is then executed)

6 6 Illustration of CALL and RET Calling Program Called Procedure main:... 006A5100h: call ProcA 006A5105h: inc eax... ProcA PROC 006A5180h: MOV eax,1... RET ProcA ENDP 05 51 6A 00 ESP CALL pushes the return address onto the stack 05 51 6A 00 ESP RET pops the returned address from the stack into EIP

7 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 7 CALL-RET Example (1 of 2) main PROC 00000020 call MySub 00000025 mov eax,ebx. main ENDP MySub PROC 00000040 mov eax,edx. 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 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 8 CALL-RET Example (2 of 2) The CALL instruction pushes 00000025 onto the stack, and loads 00000040 into EIP The RET instruction pops 00000025 from the stack into EIP (stack shown before RET executes)

9 9 Exercise 1  A program contains the following sequence of instructions: CALL PROC1 MOV BX,AX  The instruction MOV BX,AX is located at the address 0000011Ah. In addition, PROC1 starts at address 00000456h. Finally, ESP initially contains 00008000h.  (A) What is the content, in hexadecimal, of the registers EIP, and ESP just after the execution of the instruction CALL PROC1 (and just before the execution of the 1st instruction of PROC1)?  (B) What is the double word pointed by [ESP]?

10 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 10 Nested Procedure Calls By the time Sub3 is called, the stack contains all three return addresses:

11 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 11 Local and Global Labels main PROC jmp L2; error L1::; global label exit main ENDP sub2 PROC L2:; local label jmp L1; ok ret sub2 ENDP A local label is visible only to statements inside the same procedure. A global label is visible everywhere.

12 12 Passing Arguments to Procedures  Arguments can be passed to procedures via:  The stack: this is the technique used in HLLs. We will use this technique only in later chapters.  Registers: a much faster way to pass arguments (but very few registers are available). We will start by using this technique.  Global variables: the scope of a variable is the.ASM file into which it is defined. Trivial to do and extremely fast but it is contrary to modular programming practice.  Procedures usually return their results in:  Registers : either the returned value or the address of the returned value (ex: a modified array).  Flags : by modifying one or more flags, a procedure can specify the presence or the absence of a property.

13 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 13 Procedure Parameters (1 of 3) A good procedure might be usable in many different programs but not if it refers to specific variable names Parameters help to make procedures flexible because parameter values can change at runtime

14 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 14 Procedure Parameters (2 of 3) ArraySum PROC mov esi,0; array index mov eax,0; set the sum to zero mov ecx,LENGTHOF myarray ; set number of elements L1:add eax,myArray[esi]; add each integer to sum add esi,4; point to next integer loop L1; repeat for array size mov theSum,eax; store the sum ret ArraySum ENDP The ArraySum procedure calculates the sum of an array. It makes two references to specific variable names: What if you wanted to calculate the sum of two or three arrays within the same program?

15 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 15 Procedure Parameters (3 of 3) ArraySum PROC ; Receives: ESI points to an array of doublewords, ; ECX = number of array elements. ; Returns: EAX = sum ;----------------------------------------------------- mov eax,0; set the sum to zero L1:add eax,[esi]; add each integer to sum add esi,4; point to next integer loop L1; repeat for array size ret ArraySum ENDP This version of ArraySum returns the sum of any doubleword array whose address is in ESI. The sum is returned in EAX:

16 16 Using Procedures  When a procedure returns to the caller it should preserve the content of the registers (except those used to return a value)  Hence, the procedure should first save the content of the registers that it will modify and restore them just before returning to the caller  Caution on stack usage:  ESP points to the return address when entering the procedure. Make sure that this is the case just before executing RET.  This also applies to the main procedure. Make sure to push and pop an equal amount of data before exiting with RET.  Here are examples of programs using procedures:  readstr.asm readstr.asm  is_alpha.asm is_alpha.asm

17 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 17 USES Operator Lists the registers that will be preserved ArraySum PROC USES esi ecx mov eax,0; set the sum to zero etc. MASM generates the code shown in gold: ArraySum PROC push esi push ecx. pop ecx pop esi ret ArraySum ENDP

18 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 18 When not to push a register SumOf PROC; sum of three integers push eax; 1 add eax,ebx; 2 add eax,ecx; 3 pop eax; 4 ret SumOf ENDP Skip to Page 25 The sum of the three registers is stored in EAX on line (3), but the POP instruction replaces it with the starting value of EAX on line (4):

19 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 19 Program Design Using Procedures Top-Down Design (functional decomposition) involves the following: design your program before starting to code break large tasks into smaller ones use a hierarchical structure based on procedure calls test individual procedures separately

20 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 20 Integer Summation Program (1 of 4) Main steps: Prompt user for multiple integers Calculate the sum of the array Display the sum Description: Write a program that prompts the user for multiple 32-bit integers, stores them in an array, calculates the sum of the array, and displays the sum on the screen.

21 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 21 Procedure Design (2 of 4) Main Clrscr; clear screen PromptForIntegers WriteString; display string ReadInt ; input integer ArraySum ; sum the integers DisplaySum WriteString; display string WriteInt; display integer

22 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 22 Structure Chart (3 of 4) gray indicates library procedure View the stub programstub program View the final programfinal program

23 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 23 Sample Output (4 of 4) Enter a signed integer: 550 Enter a signed integer: -23 Enter a signed integer: -96 The sum of the integers is: +431

24 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 24 Summary Procedure – named block of executable code Runtime stack – LIFO structure holds return addresses, parameters, local variables PUSH – add value to stack POP – remove value from stack Use the Irvine32 library for all standard I/O and data conversion Want to learn more? Study the library source code in the c:\Irvine\Examples\Lib32 folderc:\Irvine\Examples\Lib32

25 Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010. 25 55 64 67 61 6E 67 65 6E


Download ppt "Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, 2010. All rights reserved. You may modify and copy this slide."

Similar presentations


Ads by Google