15
Sahar Mosleh California State University San Marcos Page 1 Nested Procedure calls and Flowcharts

Nested Procedure calls and Flowcharts

  • Upload
    esben

  • View
    84

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 1

Nested Procedure calls and Flowcharts

Page 2: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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.

Page 3: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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.

Page 4: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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:

Page 5: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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.

Page 6: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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.

Page 7: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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

Page 8: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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

.dataprompt1 BYTE "Enter a signed integer: ",0prompt2 BYTE "The sum of the integers is: ",0IntegerCount DWORD 3 ; array sizearray DWORD IntegerCount DUP(?)

.codemain PROC

call Clrscrmov esi,OFFSET arraymov ecx,IntegerCountcall PromptForIntegerscall ArraySumcall DisplaySumexit

main ENDP

Page 9: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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 stringcall ReadInt ; read integer into EAXcall Crlf ; go to next output linemov [esi],eax ; store in arrayadd esi,4 ; next integerloop L1popad ; restore all registersret

PromptForIntegers ENDP

Page 10: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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, ECXpush ecxmov eax,0 ; set the sum to zero

L1:add eax,[esi] ; add each integer to sumadd esi,4 ; point to next integerloop L1 ; repeat for array size

L2:pop ecx ; restore ECX, ESIpop esiret ; sum is in EAX

ArraySum ENDP

Page 11: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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

Page 12: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 12

Flow chart of Arraysum procedure

Page 13: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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.

Page 14: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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 sumadd esi,4 ; point to next integerloop L1 ; repeat for array sizeret ; sum is in EAX

ArraySum ENDP

Page 15: Nested Procedure calls and  Flowcharts

Sahar Mosleh California State University San Marcos Page 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 PROCpush esi ; save ESI, ECXpush ecxpush eaxmov eax,0 ; set the sum to zero

L1:add eax,[esi] ; add each integer to sumadd esi,4 ; point to next integerloop L1 ; repeat for array sizepop ecx ; restore ECX, ESIpop esi

pup eax ; Lost The sumret ; sum is in EAX

ArraySum ENDP