51
Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value , 20 atod value

Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Write codes

Please write a code segments to implement the following function: Read a double word from keyboard

input value , 20 atod value

Page 2: 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 (递归)

Page 3: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 4: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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 (过程体)

Page 5: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 6: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

PUSH

PUSH source

source : register16/32, segment register, word/doubleword memory , immediate byte/word/double

Function:1. ESP=ESP-sizeof (source)2. [ESP]=source;

Page 7: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value
Page 8: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value
Page 9: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

POPPOP destination

destination : register16/32, segment register(except CS), word/doubleword memory

Function:1. destination=[ESP];2. ESP=ESP+sizeof (source)

Page 10: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value
Page 11: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

STACKax=83B5

ESP:00600200 PUSH ax

83B5 ESP:006001FE

PUSH -240

FF

FF

FF

10 ESP:006001FA

POP ecx

ecx=???FFFFFF10

Page 12: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 13: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 14: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 15: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 16: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 17: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 18: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 19: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Example:Initialize PROC NEAR32

MOV Count1 , 0MOV Count2 , 0

MOV Total1 , 0MOV Total2 , 0MOV ebx , 0

RETInitialize ENDP

Page 20: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 21: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 22: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 23: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 24: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 25: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 26: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Assemble and link

Assemble procedure.asm Assemble main.asmLink procedure.obj+main.obj

Procedure.objMain.obj

Executable file

Page 27: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 28: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Parameters

Formal parameters 形式参数

Actual parameters 实际参数

Page 29: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 30: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 31: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 32: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 33: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 34: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 35: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 36: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 37: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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]

Page 38: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Procedure gcd

POPFPOP EDXMOV ESP, EBP ; undo the effects of corresponding ;subtraction in the entry codePOP EBPRET 8

GCD ENDP

Page 39: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Entry code for a procedure

PUSH EBP ; establish stack frameMOV EBP, ESPSUB ESP, N ;n bytes of local variables spacePUSH ……PUSH …PUSHF ; save flags

Page 40: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Exit code for a procedure

POPF ; restore flagsPOP… ; restore registers…POP …MOV ESP, EBP; restore ESP if local variables usedPOP EBP ; restore EBPRET ; return

Page 41: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 42: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 43: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 44: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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.

Page 45: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 46: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

Solution to Towers of Hanoi

Move(NbrDisks : integer; Source, Dest, Spare : character) parameters are passed in words on the sta

ck

Page 47: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 48: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 49: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 50: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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

Page 51: Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value

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