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

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Computer Organization And Assembly Language
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Practical Session 3 Computer Architecture and Assembly Language.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
CS2422 Assembly Language & System Programming October 24, 2006.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 10 S. Dandamudi.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Strings, Procedures and Macros
Arithmetic Flags and Instructions
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Stack and Procedures Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa
CSC 221 Computer Organization and Assembly Language
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
(The Stack and Procedures)
CS 301 Fall 2002 Control Structures
Chapter 4: Instructions
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
MIPS Procedure Calls CSE 378 – Section 3.
Practical Session 4.
(The Stack and Procedures)
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Multi-modules programming
X86 Assembly Review.
(The Stack and Procedures)
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation 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 6 Procedures (过程) Contents: The 80x86 Stack (堆栈) Procedure Body, Call and Return Parameters and Local Variables Recursion (递归)

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 follow the rule of “ FILO ” (先进后出) Two registers ESP & EBP Two instructions will manually manage 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;

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

STACK ax=83B5 ESP: PUSH ax 83 B5 ESP:006001FE PUSH -240 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 registers push ecx push edx … pop edx ; resume registers pop 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 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.

Example: Initialize PROC NEAR32 MOV Count1, 0 MOV Count2, 0 MOV Total1, 0 MOV Total2, 0 MOV ebx, 0 RET Initialize 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.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

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

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

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

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 value PUSH ECX ; second argument value CALL ADD2 ; call procedure to find sum ADD 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 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

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 EBP MOV EBP, ESP SUB ESP, 4; reserve space for one local double word PUSH EDX PUSHF

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]

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

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

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

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

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

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

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

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

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

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

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 Write codes to exchange the value of EAX and EBX using POP and PUSH instructions. P211. Exercises ,2 P222. Exercises ,2,3 P227. Exercises