CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.

Slides:



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

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.
Computer Organization And Assembly Language
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.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
CS2422 Assembly Language & System Programming October 24, 2006.
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
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
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.
CSC 221 Computer Organization and Assembly Language Lecture 12: Addressing Modes in Assembly.
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
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
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 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
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/11/13
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.
Compiler Construction Code Generation Activation Records
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, 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.
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.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
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.
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 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.
CSC 221 Computer Organization and Assembly Language
Stack Operations Dr. Hadi AL Saadi.
Assembly Language for x86 Processors 6th Edition
Assembly Language for x86 Processors 7th Edition
Chapter 5: Procedures.
Chapter 4 Data Movement Instructions
Assembly Language for Intel-Based Computers, 5th Edition
Stack Frames and Advanced Procedures
Practical Session 4.
Assembly Language for Intel-Based Computers, 4th Edition
The Stack and Procedures
Low-Level Thread Dispatching on the x86
Computer Organization and Assembly Language
Presentation transcript:

CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures

Lecture 15: Review Elements are only added/removed to/from the top. LIFO (Last In First Out) structure. Managed by the CPU, using two registers: –SS:ESP PUSH and POP Instructions PUSH syntax: –PUSH r/m16 –PUSH r/m32 –PUSH imm32 POP syntax: –POP r/m16 –POP r/m32 Top Bottom

Lecture 15: Review (cont.) PUSHFD and POPFD –push and pop the EFLAGS register PUSHAD pushes the 32-bit general-purpose registers on the stack –order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI POPAD pops the same registers off the stack in reverse order –PUSHA and POPA do the same for 16-bit registers

Lecture Outline

Creating Procedures Large problems can be divided into smaller tasks to make them more manageable. A procedure is the Assembly equivalent of a Java or C++ function. Following is an assembly language procedure named sample: sample PROC. ret sample ENDP

Documenting Procedures Suggested documentation for each procedure: –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.

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

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

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

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)

Nested Procedure Calls By the time Sub3 is called, the stack contains all three return addresses:

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.

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

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?

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:

Flowchart for the ArraySum Procedure Begin push esi, ecx eax = 0 add eax,[esi] add esi,4 ecx = ecx - 1 ecx > 0? pop ecx, esi End push esi push ecx mov eax,0 AS1: add eax,[esi] add esi,4 loop AS1 pop ecx pop esi YesNo

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

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):

Summary Creating Procedures Documenting Procedures Example: SumOf Procedure CALL and RET Instructions The CALL instruction calls a procedure –pushes offset of next instruction on the stack and copies the address of the called procedure into EIP The RET instruction returns from a procedure sample PROC. ret sample ENDP

Summary Nested Procedure Calls Local and Global Labels Procedure Parameters Flowchart Symbols USES Operator (cont.)

Reference Most of the Slides are taken from Presentation: Chapter 5 Assembly Language for Intel-Based Computers, 4 th Edition Kip R. Irvine (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.