Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.

Slides:



Advertisements
Similar presentations
Link list/file stamps/clusters Odds and ends remaining for test 2.
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.
Assembly Language Procedures.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
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.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
CS2422 Assembly Language & System Programming October 26, 2006.
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.
Coding.
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 & System Programming September 26, 2006.
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Sahar Mosleh California State University San MarcosPage 1 JMP and Loops Memory Operand Move Instruction Array Data Related Operation and Directives.
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.
Procedure Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
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
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Arithmetic Flags and Instructions
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, PROCEDURES.
Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello.
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.
Conditional Loop Instructions, Conditional Structures
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.
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.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Assembly Language for x86 Processors 6th Edition
Chapter 5: Procedures.
Chapter 4: Instructions
Stack Frames and Advanced Procedures
Libraries and Procedures
Practical Session 4.
Assembly Language for Intel-Based Computers, 4th Edition
The Stack and Procedures
Computer Organization and Assembly Language
ICS51 Introductory Computer Organization
More on operators and procedures in the Linked
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts

Sahar Mosleh California State University San MarcosPage 2 A nested procedure call occurs when a called procedure calls another before the first procedure returns. Suppose that main calls a procedure named Sub1.While sub 1 is executing it calls the Sub2 procedure. While Sub2 is executing, it calls the Sub3 procedure.

Sahar Mosleh California State University San MarcosPage 3 When the RET instruction at the end of Sub3 executes, it pops the value at the stack[ESP]. Into the instruction pointer. This causes execution to resume at the instruction following the call Sub3 instruction. The following diagram shows the stack just before the return from Sub3 is executed.

Sahar Mosleh California State University San MarcosPage 4 After the return, ESP points to the next highest stack entry. When the RET instruction at the end of Sub2 is about to execute, the stack appears as follows:

Sahar Mosleh California State University San MarcosPage 5 Finally, when Sub1 returns, stack [ESP] is popped into the instruction pointer, and execution resumes in main: Clearly stack proves itself a useful device for remembering information, including nested procedure calls. Stack structures, in general, are in situations where program must retrace their steps in a specific order.

Sahar Mosleh California State University San MarcosPage 6 Flow Charts A flow chart is a well-established way of diagramming program logic. Each shape in a flowchart represents a single logical step, and lines with arrows connecting the shapes.

Sahar Mosleh California State University San MarcosPage 7 Text notation such as Yes and No are added next to decision symbols to show branching direction. There is no required position for each arrow connected to a decision symbol. Each process symbol can contain one or more closely related instructions. The instructions need not be syntactically correct. For example, we could add 1 to CX using either of the following process symbols: cx = cx +1 add cx,1

Sahar Mosleh California State University San MarcosPage 8 TITLE Integer Summation Program (Sum2.asm) ; This program inputs multiple integers from the user, ; stores them in an array, calculates the sum of the ; array, and displays the sum. INCLUDE Irvine32.inc.data prompt1 BYTE "Enter a signed integer: ",0 prompt2 BYTE "The sum of the integers is: ",0 IntegerCount DWORD 3; array size array DWORD IntegerCount DUP(?).code main PROC call Clrscr mov esi,OFFSET array mov ecx,IntegerCount call PromptForIntegers call ArraySum call DisplaySum exit main ENDP

Sahar Mosleh California State University San MarcosPage 9 ; PromptForIntegers PROC ; ; Prompts the user for an array of integers, and fills ; the array with the user's input. ; Receives: ESI points to the array, ECX = array size ; Returns: nothing ; pushad; save all registers L1: call WriteString; display string call ReadInt; read integer into EAX call Crlf; go to next output line mov [esi],eax; store in array add esi,4; next integer loop L1 popad; restore all registers ret PromptForIntegers ENDP

Sahar Mosleh California State University San MarcosPage 10 ; ArraySum PROC ; ; Calculates the sum of an array of 32-bit integers. ; Receives: ESI points to the array, ECX = array size ; Returns: EAX = sum of the array elements ; push esi; save ESI, ECX push ecx 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 L2: pop ecx; restore ECX, ESI pop esi ret; sum is in EAX ArraySum ENDP

Sahar Mosleh California State University San MarcosPage 11 ; DisplaySum PROC ; ; Displays the sum on the screen ; Recevies: EAX = the sum ; Returns: nothing ; push edx mov edx,OFFSET prompt2; display message call WriteString call WriteInt; display EAX call Crlf pop edx ret DisplaySum ENDP END main

Sahar Mosleh California State University San MarcosPage 12 Flow chart of Arraysum procedure

Sahar Mosleh California State University San MarcosPage 13 Uses Operator The uses operator, coupled with the directive, lets you list names of all the registers modified within a procedure. This tells the assembler to do two things: first, generate Push instructions that save the registers on the stack. The uses operator Pop instructions to restore the register values at the end of the procedure. The USES operator immediately follows PROC, and is itself followed by a list of registers on the same line separated by the space or tabs.

Sahar Mosleh California State University San MarcosPage 14 Lets modify the ArraySum procedure. It used PUSH and POP instruction to save and restore ESI and ECX because these registers were modified by the procedure. Instead, we can let the USES operator do the same thing: ; ArraySum PROC USES esi ecx ; ; Calculates the sum of an array of 32-bit integers. ; Receives: ESI points to the array, ECX = array size ; Returns: EAX = sum of the array elements ; 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; sum is in EAX ArraySum ENDP

Sahar Mosleh California State University San MarcosPage 15 Exception There is an important exception to our standing rule about saving registers that applies when a procedure uses a register to return a value. In this case, the return register should not be pushed and popped. For example, in the ArraySum procedure, if we were to push and pop EAX, the procedure return value would be lost: ArraySum PROC push esi; save ESI, ECX push ecx push eax 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 pop ecx; restore ECX, ESI pop esi pup eax ; Lost The sum ret; sum is in EAX ArraySum ENDP