Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Write codes Please write a code segments to implement the following function: Read a double word from keyboard input value, 20 atod value."— Presentation transcript:

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

2 Chapter 6 Procedures (过程) Contents: The 80x86 Stack (堆栈) Procedure Body, Call and Return Parameters and Local Variables Recursion (递归)

3 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.

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

5 The 80x86 Stack Stack is a size of memory which follow the rule of “ FILO ” (先进后出) Two registers ESP & EBP Two instructions will manually manage stack PUSH POP

6 PUSH PUSH source source : register16/32, segment register, word/doubleword memory, immediate byte/word/double Function: 1.ESP=ESP-sizeof (source) 2.[ESP]=source;

7

8

9 POP POP destination destination : register16/32, segment register(except CS), word/doubleword memory Function: 1. destination=[ESP]; 2. ESP=ESP+sizeof (source)

10

11 STACK ax=83B5 ESP:00600200 PUSH ax 83 B5 ESP:006001FE PUSH -240 FF 10 ESP:006001FA POP ecx ecx=??? FFFFFF10

12 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

13 Sequence of push and pop Save registers and then resume them. push ebx ; save registers push ecx push edx … pop edx ; resume registers pop ecx pop ebx

14 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

15 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

16 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

17 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.

18 PROC and ENDP LabelPROC [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.

19 Example: Initialize PROC NEAR32 MOV Count1, 0 MOV Count2, 0 MOV Total1, 0 MOV Total2, 0 MOV ebx, 0 RET Initialize ENDP

20 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

21 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

22 Procedure example.CODE Initialize PROC NEAR32 MOV Count1, 0 MOV Count2, 0 MOV Total1, 0 MOV Total2, 0 MOV ebx, 0 RET Initialize ENDP _start: CALL Initialize ; other codes Start point Call procedure Define procedure

23 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

24 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.

25 PUBLIC proccedure1, procedure2.CODE Procedure1 PROC NEAR32 … Procedure1 ENDP Procedure2 PROC NEAR32 … Procedure2 ENDP END EXTERN proccedure1:near332, procedure2:near32.CODE Call Procedure1 … Call Procedure2 … END Procedure.asm ;main.asm

26 Assemble and link Assemble procedure.asm Assemble main.asm Link procedure.obj+main.obj Procedure.obj Main.obj Executable file

27 Parameters & Local Variables Parameters Pass-by-value / in parameters (传值) Pass-by-location / In-out /variable parameters (传地址) We will discuss a common techinque for passing parameters. Pass values for in parameters Pass addresses of data for in-out parameter

28 Parameters Formal parameters 形式参数 Actual parameters 实际参数

29 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

30 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.

31 Pass parameters in calling program Calling program passes these parameters by pushing them on the stack. Such as: PUSH VALUES ; first argument value PUSH ECX ; second argument value CALL ADD2 ; call procedure to find sum ADD ESP, 8 ; remove parameters from stack

32 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

33 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.

34 Procedure ADD2 Add2 PROC NEAR32 PUSH EBP ; save EBP MOV EBP,ESP; establish stack frame MOV EAX, [EBP+8] ; copy second parameter value ADD EAX, [EBP+12] ; add first parameter value POP EBP ; return RET Add2 ENDP

35 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

36 Procedure GCD gcd is stored on the stack until it is time to return thae value in EAX. PUSH EBP MOV EBP, ESP SUB ESP, 4; reserve space for one local double word PUSH EDX PUSHF

37 Procedure gcd MOV EAX, [EBP+8] MOV [EBP-4], EAX ; GCD=NUMBER MOV EDX, [EBP+8]; UNTIL0: MOV EAX, [EBP-4] MOV [EBP-4], EDX; MOV EDX,0 DIV DWORD PTR [EBP-4] CMP EDX, 0 JNZ UNTIL0 MOV EAX, [EBP-4]

38 Procedure gcd POPF POP EDX MOV ESP, EBP ; undo the effects of corresponding ;subtraction in the entry code POP EBP RET 8 GCD ENDP

39 Entry code for a procedure PUSH EBP ; establish stack frame MOV EBP, ESP SUB ESP, N ;n bytes of local variables space PUSH … … PUSH … PUSHF ; save flags

40 Exit code for a procedure POPF ; restore flags POP … ; restore registers … POP … MOV ESP, EBP; restore ESP if local variables used POP EBP; restore EBP RET; return

41 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.

42 Procedure Minimum MINIMUM 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 FORCOUNT ENDFORCOUNT: MOV EBX, [EBP+8] MOV [EBX], EAX POPF POPAD POP EBP RET MINIMUM ENDP

43 Calling code LEA EAX, ARRAY; address of array PUSH EAX PUSH COUNT ; value of count LEA EAX, MIN ; address of min PUSH EAX CALL MINIMUM ADD ESP, 10 ; discard parameters

44 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.

45 Towers of Hanoi puzzle Procedure 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 if End procedure Move; begin{main program} prompt for and input Number; Move(Number, ‘ A ’, ’ B ’, ’ C ’ ); end

46 Solution to Towers of Hanoi Move(NbrDisks : integer; Source, Dest, Spare : character) parameters are passed in words on the stack

47 Move procedure push ebp mov ebp,esp push eax ; save registers push ebx cmp WORD PTR [ebp+14],1 ; NbrDisks = 1? jne elseMore ; skip if more than 1 mov bx,[ebp+12] ; Source mov source,bl ; copy character to output mov bx,[ebp+10] ; destination mov dest,bl ; copy character to output

48 output message ; print line jmp endIfOne ; return elseMore: 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

49 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 stack endIfOne: pop ebx ; restore registers pop eax pop ebp ; restore base pointer ret ; return

50 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

51 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


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

Similar presentations


Ads by Google