write codes please write a code segments to implement the following function: read a double word...
Post on 22-Dec-2015
220 views
TRANSCRIPT
Write codes
Please write a code segments to implement the following function: Read a double word from keyboard
input value , 20 atod value
Chapter 6Procedures (过程)
Contents:The 80x86 Stack (堆栈)Procedure Body, Call and ReturnParameters and Local VariablesRecursion (递归)
Procedure/subprogram/routine
Procedure is a subprogram that is almost a self-contained unit.
Procedure can be called by other program. Procedures can be reused.
Procedures help divide programs into manageable tasks.
Related topics
What is stack and how to use it. How to define, call procedures and return
back (返回) to the calling program. How to pass arguments (参数) How to implement local variables (局部变
量) in a procedure body (过程体)
The 80x86 Stack Stack is a size of memory which follo
w the rule of “FILO” (先进后出) Two registers
ESP & EBP Two instructions will manually manag
e stack PUSH POP
PUSH
PUSH source
source : register16/32, segment register, word/doubleword memory , immediate byte/word/double
Function:1. ESP=ESP-sizeof (source)2. [ESP]=source;
POPPOP destination
destination : register16/32, segment register(except CS), word/doubleword memory
Function:1. destination=[ESP];2. ESP=ESP+sizeof (source)
STACKax=83B5
ESP:00600200 PUSH ax
83B5 ESP:006001FE
PUSH -240
FF
FF
FF
10 ESP:006001FA
POP ecx
ecx=???FFFFFF10
Use of stack
Stack can be used to save the contents of a register or memory temporarily on the stack.
PUSH and POP instructions are often used in pairs.
push edx cdq idiv Divisor pop edx
Sequence of push and pop
Save registers and then resume them.
push ebx ; save registerspush ecx
push edx…pop edx ; resume registerspop ecx
pop ebx
Other PUSH &POP instructions
PUSHF &POPF PUSHFD & POPFD
Push or pop flag register PUSHAD & POPAD
Push or pop EAX, ECX, EDX, EBX, ESP, EBP , ESI and EDI
PUSHA & POPA Push or pop AX, CX, DX, BX, SP, BP ,
SI and DI
PUSHF/POPF
PUSHF push FLAGS on stack.
POPF Pop from stack to FLAGS
PUSHFD push EFLAGS on stack
POPFD Pop from stack to EFLAGS
;EFLAGS->EAX pushfd pop eax
PUSHA/POPA
PUSH EAX PUSH ECX PUSH EDX PUSH EBX PUSH ESP PUSH EBP PUSH ESI PUSH EDI
PUSHAD POP EDI POP ESI POP EBP ADD ESP+sizeof(register) POP EBX POP EDX POP ECX POP EAX
POPAD
Procedure define (过程定义) Procedure body always follows
a .CODE directive. Procedure body is bracketed
by two directives, PROC and ENDP
Label gives the name of the procedure.
PROC and ENDP
Label PROC [attributes] . . ;procedure body . .Label ENDP
NEAR32
NEAR32: the procedure will be located in the same segment as the calling code and that 32-bit addresses are being used.
Example:Initialize PROC NEAR32
MOV Count1 , 0MOV Count2 , 0
MOV Total1 , 0MOV Total2 , 0MOV ebx , 0
RETInitialize ENDP
Procedure return
Transfer control from the procedure back to the caller.
Normally is the last instruction.
Return address is stored in stack.RET
•Pop [ESP] •Assign the EIP &ECS by the address stored in stack.RET count •Pop [ESP]
•Assign the EIP &ECS by the address stored in stack.•ESP +count
Procedure call Invoke procedure by CALL statement.
CALL label
Example:
CALL Initialize
•Push the address of the next instruction in the stack;•Assign the start address of the procedure to EIP &ECS
Procedure example
.CODEInitialize PROC NEAR32
MOV Count1 , 0MOV Count2 , 0
MOV Total1 , 0MOV Total2 , 0MOV ebx , 0
RETInitialize ENDP _start: CALL Initialize ; other codes
Start point
Call procedure
Define procedure
Procedures in separate files
When building blocks for large programs, it is often to assemble procedures and calling programs in separate files.
In this case, should use PUBLIC and EXTERN directives
PUBLIC and EXTERN
PUBLIC symbol1 [,symbol2]…Make procedure names visible outside the file containing them.
EXTERN symbol1:type [,symbol2: type]…Gives the calling program information about external symbols.
PUBLIC proccedure1, procedure2.CODEProcedure1 PROC NEAR32 …Procedure1 ENDPProcedure2 PROC NEAR32 …Procedure2 ENDP END
EXTERN proccedure1:near332, procedure2:near32.CODE Call Procedure1 … Call Procedure2 … END
Procedure.asm
;main.as
m
Assemble and link
Assemble procedure.asm Assemble main.asmLink procedure.obj+main.obj
Procedure.objMain.obj
Executable file
Parameters & Local Variables
Parameters Pass-by-value / in parameters (传值) Pass-by-location / In-out /variable parame
ters (传地址) We will discuss a common techinque fo
r passing parameters. Pass values for in parameters Pass addresses of data for in-out paramet
er
Parameters
Formal parameters 形式参数
Actual parameters 实际参数
Methods for passing parameters
Registers Use the same registers in
procedures and caller program. Very simple
Stack Used to store local variables Used to store parameters
Simple example
Procedure Add2 will add two double word size integers, returning the sum in EAX.
The calling program passes two integers on the stack.
Pass parameters in calling program
Calling program passes these parameters by pushing them on the stack.
Such as:
PUSH VALUES ; first argument valuePUSH ECX ; second argument valueCALL ADD2 ; call procedure to find
sumADD ESP , 8 ; remove parameters from
stack
Why need “ADD ESP , 8”
Function: remove parameters from the stack
Otherwise: Repeated procedure calls might exhaust the
stack space; When the calls are nested , the left parameters
by inside calls will make the outside return can not find the correct return address on the stack.
Code “ Return 8 “ in procedure can replace this code
Dealing in procedure Add2 Add2 will retrieve the two
parameter values from the stack. It uses relative based addressing
mode, one of the memory addressing mode.
Use EBP register because theses parameters are on stack.
Procedure ADD2
Add2 PROC NEAR32 PUSH EBP ; save EBPMOV EBP ,ESP; establish stack frameMOV EAX, [EBP+8] ; copy second parameter value
ADD EAX , [EBP+12] ; add first parameter valuePOP EBP ; returnRETAdd2 ENDP
Store local variables (局部变量) on stack Design a procedure for
computing the greatest common divisor of two integers. gcd :=number1;
remainder :=number2;
until (remainder =0) loop dividend :=gcd; gcd :=remainder; remainder :=dividend mod gcd; end until;
See figure 6.14
Procedure GCD
gcd is stored on the stack until it is time to return thae value in EAX.
PUSH EBPMOV EBP, ESPSUB ESP, 4; reserve space for one local double wordPUSH EDXPUSHF
Procedure gcdMOV EAX, [EBP+8]MOV [EBP-4], EAX ; GCD=NUMBERMOV EDX, [EBP+8];UNTIL0: MOV EAX, [EBP-4]MOV [EBP-4], EDX;MOV EDX,0DIV DWORD PTR [EBP-4]CMP EDX , 0JNZ UNTIL0
MOV EAX, [EBP-4]
Procedure gcd
POPFPOP EDXMOV ESP, EBP ; undo the effects of corresponding ;subtraction in the entry codePOP EBPRET 8
GCD ENDP
Entry code for a procedure
PUSH EBP ; establish stack frameMOV EBP, ESPSUB ESP, N ;n bytes of local variables spacePUSH ……PUSH …PUSHF ; save flags
Exit code for a procedure
POPF ; restore flagsPOP… ; restore registers…POP …MOV ESP, EBP; restore ESP if local variables usedPOP EBP ; restore EBPRET ; return
Passing the address of argument to the procedure
When we need to pass large parameters such as array, a character string, or a record, we would like to pass the address of the argument rather than the value of the argument to the procedure.
That means the procedure and calling share the same memory.
Procedure can store its local variable in data segment.
Procedure MinimumMINIMUM PROC NEAR32 PUSH EBP MOV EBP, ESP PUSHAD PUSHF MOV EBX , [EBP+14] MOV ECX , 0 MOV CX , [EBP+12] MOV EAX , 7FFFFFFFH JECXZ ENFFORCOUNT FORCOUNT: CMP [EBX] , EAX JNL ENDIFLESS
MOV EAX , [EBX]ENDIFLESS: ADD EBX , 4 LOOP FORCOUNTENDFORCOUNT:MOV EBX , [EBP+8]MOV [EBX] , EAXPOPFPOPADPOP EBPRET MINIMUM ENDP
Calling code
LEA EAX , ARRAY; address of arrayPUSH EAX PUSH COUNT ; value of countLEA EAX , MIN ; address of minPUSH EAXCALL MINIMUMADD ESP , 10 ; discard parameters
Recursion
Recursive procedure or function is one the calls itself, either directly or indirectly.
When program a recursive procedure, we should correctly store the variables on stack for each calling.
Towers of Hanoi puzzleProcedure Move(NbrDisks, Source, Destination, Spare); begin if NbrDisks=1 then display “Move disk from”, Source, “to”, Destination else Move(NbrDisks-1, Source, Spare, Destination); Move(1, Source , Destination, Spare); Move(NbrDisks-1, Spare, Destination, Source); end ifEnd procedure Move; begin{main program} prompt for and input Number;Move(Number, ‘A’,’B’,’C’);end
Solution to Towers of Hanoi
Move(NbrDisks : integer; Source, Dest, Spare : character) parameters are passed in words on the sta
ck
Move procedurepush ebp mov ebp,esp push eax ; save registerspush ebxcmp WORD PTR [ebp+14],1 ; NbrDisks = 1?jne elseMore ; skip if more than 1mov bx,[ebp+12] ; Sourcemov source,bl ; copy character to outputmov bx,[ebp+10] ; destinationmov dest,bl ; copy character to output
output message ; print linejmp endIfOne ; returnelseMore: mov ax,[ebp+14] ; get NbrDisks dec ax ; NbrDisks - 1 push ax ; parameter 1: NbrDisks-1 pushw [ebp+12] ; parameter 2: source does not change pushw [ebp+8] ; parameter 3: old spare is new destination pushw [ebp+10] ; parameter 4: old destination is new spare call Move ; Move(NbrDisks-1,Source,Spare,Destination) add esp,8 ; remove parameters from stack
push ax ; parameter 1: NbrDisks-1 pushw [ebp+8] ; parameter 2: source is original spare pushw [ebp+10] ; parameter 3: original destination pushw [ebp+12] ; parameter 4: original source is spare call Move ; Move(NbrDisks-1,Spare,Destination,Source) add esp,8 ; remove parameters from stackendIfOne: pop ebx ; restore registers pop eax pop ebp ; restore base pointer ret ; return
Main program_start: output prompt ; ask for number of disks input number,16 ; read ASCII characters atoi number ; convert to integer push ax ; argument 1: Number mov al,'A' ; argument 2: ' A' push ax mov al,'B' ; argument 3: ' B' push ax mov al,'C' ; argument 4: ' C' push ax call Move ; Move(Number,Source,Dest,Spare) add esp,8 ; remove parameters from stack
Exercises
P200. Exercises 6.1-- 2 Write codes to exchange the
value of EAX and EBX using POP and PUSH instructions.
P211. Exercises 6.2-- 1,2 P222. Exercises 6.3-- 1,2,3 P227. Exercises 6.4-- 2