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

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language Procedures.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Accessing parameters from the stack and calling functions.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language & System Programming October 24, 2006.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Instruction Set Design by Kip R. Irvine (c) Kip Irvine, All rights reserved. You may modify and copy this slide show for your personal use,
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Stack and Procedures Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa
CSC 221 Computer Organization and Assembly Language
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/11/13
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CHAPTER 5: PROCEDURES ASSEMBLY LANGUAGE FOR INTEL- BASED COMPUTERS, 5 TH EDITION (c) Pearson Education, All rights reserved. You may modify and copy.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Procedure Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/27 with slides by Kip Irvine.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Irvine, Kip R. Assembly Language for x86 Processors 7/e, What's Next Linking to an External Library The Book's Link Library Stack Operations Defining.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
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.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Stack Operations Dr. Hadi AL Saadi.
Assembly Language for x86 Processors 6th Edition
Chapter 5: Procedures.
Microprocessor and Assembly Language
(The Stack and Procedures)
Stack Frames and Advanced Procedures
(The Stack and Procedures)
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Morgan Kaufmann Publishers Computer Organization and Assembly Language
The Stack and Procedures
(The Stack and Procedures)
Computer Organization and Assembly Language
Presentation transcript:

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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.

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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 Illustration of CALL and RET Calling Program Called Procedure main: A5100h: call ProcA 006A5105h: inc eax... ProcA PROC 006A5180h: MOV eax,1... RET ProcA ENDP A 00 ESP CALL pushes the return address onto the stack A 00 ESP RET pops the returned address from the stack into EIP

Irvine, Kip R. Assembly Language for x86 Processors 6/e, CALL-RET Example (1 of 2) main PROC call MySub mov eax,ebx. main ENDP MySub PROC mov eax,edx. ret MySub ENDP is the offset of the instruction immediately following the CALL instruction is the offset of the first instruction inside MySub

Irvine, Kip R. Assembly Language for x86 Processors 6/e, CALL-RET Example (2 of 2) The CALL instruction pushes onto the stack, and loads into EIP The RET instruction pops from the stack into EIP (stack shown before RET executes)

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 Ah. In addition, PROC1 starts at address h. Finally, ESP initially contains h.  (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]?

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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 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.

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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?

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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):

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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.

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, 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

Irvine, Kip R. Assembly Language for x86 Processors 6/e, E E