Nested Procedure Call and Flowchart

advertisement
Nested Procedure calls and
Flowcharts
Sahar Mosleh
California State University San Marcos
Page
1
• 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 Marcos
Page
2
• 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 Marcos
Page
3
• 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 Marcos
Page
4
• 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 Marcos
Page
5
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 Marcos
Page
6
• 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
Sahar Mosleh
add cx,1
California State University San Marcos
Page
7
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 Marcos
Page
8
;----------------------------------------------------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
call ReadInt
call Crlf
mov [esi],eax
add esi,4
loop L1
popad
ret
PromptForIntegers ENDP
Sahar Mosleh
; display string
; read integer into EAX
; go to next output line
; store in array
; next integer
; restore all registers
California State University San Marcos
Page
9
;----------------------------------------------------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 esi,4
loop L1
; add each integer to sum
; point to next integer
; repeat for array size
L2:
pop ecx
pop esi
ret
ArraySum ENDP
Sahar Mosleh
; restore ECX, ESI
; sum is in EAX
California State University San Marcos
Page 10
;----------------------------------------------------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 Marcos
Page 11
Flow chart of Arraysum procedure
Sahar Mosleh
California State University San Marcos
Page 12
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 Marcos
Page 13
• 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 esi,4
loop L1
ret
ArraySum ENDP
Sahar Mosleh
; add each integer to sum
; point to next integer
; repeat for array size
; sum is in EAX
California State University San Marcos
Page 14
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
push ecx
push eax
mov eax,0
L1:
add eax,[esi]
add esi,4
loop L1
pop ecx
pop esi
pup eax
ret
ArraySum ENDP
Sahar Mosleh
; save ESI, ECX
; set the sum to zero
; add each integer to sum
; point to next integer
; repeat for array size
; restore ECX, ESI
; Lost The sum
; sum is in EAX
California State University San Marcos
Page 15
Download