1 / 15

Nested Procedure calls and Flowcharts

Nested Procedure calls and Flowcharts. 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.

jdibella
Download Presentation

Nested Procedure calls and Flowcharts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nested Procedure calls and • Flowcharts

  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.

  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.

  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:

  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.

  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.

  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: add cx,1 cx = cx +1

  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

  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

  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

  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

  12. Flow chart of Arraysum procedure

  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.

  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

  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

More Related