Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.

Slides:



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

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
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, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
Assembly Language for Intel-Based Computers Chapter 15: BIOS-Level Programming (c) Pearson Education, All rights reserved. You may modify and.
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, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Symbolic Constants Equal-Sign Directive Calculating.
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.
Assembly Language for Intel-Based Computers
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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, 4th Edition
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
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.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 23: Finite State Machines, WHILE operator (c) Pearson Education, All rights reserved.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
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.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (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.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals Assembling, Linking and Running Programs Example Programs.
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.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
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.
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 for Intel-Based Computers, 4 th Edition Unpacked and Packed Integers (c) Pearson Education, All rights reserved. You may modify.
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.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 18 Linking to External Library The Book’s Link Library Stack Operations.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
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.
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 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
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.
Assembly Language for x86 Processors 6th Edition
Chapter 5: Procedures.
Assembly Language for Intel-Based Computers, 4th Edition
Stack Frames and Advanced Procedures
Assembly Language for Intel-Based Computers, 4th Edition
The Stack and Procedures
Computer Organization and Assembly Language
Presentation transcript:

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (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. Chapter corrections (Web)Assembly language sources (Web)Chapter correctionsAssembly language sources Slides prepared by Kip R. Irvine Revision date: 08/22/2002 Modified by Dr. Nikolay Metodiev Sirakov Kip R. Irvine

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures Documenting Procedures Example: SumOf Procedure CALL and RET Instructions Nested Procedure Calls Local and Global Labels Procedure Parameters Flowchart Symbols USES Operator

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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++ function Following is an assembly language procedure named sample: sample PROC. ret sample ENDP

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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 having been satisfied, the procedure's creator makes no promise that it will work.

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, CALL and RET Instructions The CALL instruction calls a procedure pushes offset of next instruction on the stack copies the address of the called procedure into EIP The RET instruction returns from a procedure pops top of stack into EIP

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Nested Procedure Calls By the time Sub3 is called, the stack contains all three return addresses:

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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.

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Procedure Parameters (2 of 3) ArraySum PROC mov esi,0; array index mov eax,0; set the sum to zero 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?

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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:

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Flowchart Symbols The following symbols are the basic building blocks of flowcharts: (Includes two symbols not listed on page 166 of the book.)

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Flowchart for the ArraySum Procedure

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Your turn... Draw a flowchart that expresses the following pseudocode: input exam grade from the user if( grade > 70 ) display "Pass" else display "Fail" endif

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, (Solution)

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Your turn... Modify the flowchart in the previous slide to allow the user to continue to input exam scores until a value of –1 is entered

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, USES Operator Lists the registers that will be saved ArraySum PROC USES esi ecx mov eax,0; set the sum to zero etc. MASM generates the following code: ArraySum PROC push esi push ecx. pop ecx pop esi ret ArraySum ENDP

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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 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):

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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.

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, Structure Chart (3 of 4) gray indicates library procedure View the stub programstub program View the final programfinal program

Web siteWeb site ExamplesExamples March , 3PM-4:15PM Irvine, Kip R. Assembly Language for Intel-Based Computers, 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