Lecture Computer organization and assembly language: Chapter 24 - Dr. Safdar Hussain Bouk

pptx
Số trang Lecture Computer organization and assembly language: Chapter 24 - Dr. Safdar Hussain Bouk 48 Cỡ tệp Lecture Computer organization and assembly language: Chapter 24 - Dr. Safdar Hussain Bouk 1 MB Lượt tải Lecture Computer organization and assembly language: Chapter 24 - Dr. Safdar Hussain Bouk 0 Lượt đọc Lecture Computer organization and assembly language: Chapter 24 - Dr. Safdar Hussain Bouk 2
Đánh giá Lecture Computer organization and assembly language: Chapter 24 - Dr. Safdar Hussain Bouk
4.8 ( 10 lượt)
Nhấn vào bên dưới để tải tài liệu
Để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan

Nội dung

CSC 221 Computer Organization and Assembly Language Lecture 24: Advanced Procedures Lecture 23: Review Assembly Implementation of: • Shift and Rotate Instructions – Logical Shifts and Arithmetic Shifts – SHL and SHR Instruction – SAL and SAR Instructions – ROL and ROR Instruction – RCL and RCR Instructions – SHLD/SHRD Instructions • Shift and Rotate Applications Lecture 23: Review Assembly Implementation of: • Multiplication and Division Instructions – MUL Instruction – IMUL Instruction – DIV Instruction – Signed Integer Division (cont.) Lecture Outline • Local Variables • Stack Parameters – Register vs. Stack Parameters – INVOKE Directive – PROC Directive – PROTO Directive – Passing by Value or by Reference – Parameter Classifications – Example: Exchanging Two Integers – Trouble-Shooting Tips Lecture Outline • Stack Frames – Memory Models – Language Specifiers – Explicit Access to Stack Parameters – Passing Arguments by Reference – Creating Local Variables Terminologies • Programming languages use different terms to refer to subroutines : – In C and C++, subroutines  functions – In Java, subroutines  methods – In MASM, subroutines  procedures • Values passed to a subroutine by a calling program are called arguments. • When the values are received by the called subroutine, they are called parameters. LOCAL Directive • A local variable is created, used, and destroyed within a single procedure. • The LOCAL directive declares a list of local variables. – immediately follows the PROC directive – each variable is assigned a type • Syntax: LOCAL varlist Example: MySub PROC LOCAL var1:BYTE, var2:WORD, var3:SDWORD Local Variables Examples: LOCAL flagVals[20]:BYTE ; array of bytes LOCAL pArray:PTR WORD ; pointer to an array myProc PROC, LOCAL t1:BYTE, ; procedure ; local variables MASM-Generated Code (1 of 2) BubbleSort PROC LOCAL temp:DWORD, SwapFlag:BYTE . . . ret BubbleSort ENDP MASM generates the following code: BubbleSort PROC push ebp mov ebp,esp add esp,0FFFFFFF8h . . . mov esp,ebp pop ebp ret BubbleSort ENDP ; add -8 to ESP MASM-Generated Code (2 of 2) Diagram of the stack frame for the BubbleSort procedure: return address ESP EBP EBP temp [EBP - 4] SwapFlag [EBP - 8] Register vs. Stack Parameters • Register parameters require dedicating a register to each parameter. Stack parameters are more convenient. • Imagine two possible ways of calling the DumpMem procedure. Clearly the second is easier: pushad mov esi,OFFSET array mov ecx,LENGTHOF array mov ebx,TYPE array call ArraySum popad push push push call OFFSET array LENGTHOF array TYPE array ArraySum INVOKE Directive • The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments. • Syntax: INVOKE procedureName [, argumentList] • argumentList is an optional comma-delimited list of procedure arguments. • Arguments can be: – immediate values and integer expressions – variable names – address and ADDR expressions – register names INVOKE Examples .data byteVal BYTE 10 wordVal WORD 1000h .code ; direct operands: INVOKE Sub1,byteVal,wordVal ; address of variable: INVOKE Sub2,ADDR byteVal ; register name, integer expression: INVOKE Sub3,eax,(10 * 20) ; address expression (indirect operand): INVOKE Sub4,[ebx] ADDR Operator • Returns a near or far pointer to a variable, depending on which memory model your program uses: • Small model: returns 16-bit offset • Large model: returns 32-bit segment/offset • Flat model: returns 32-bit offset • Simple example: .data myWord WORD ? .code INVOKE mySub,ADDR myWord PROC Directive (1 of 2) • The PROC directive declares a procedure with an optional list of named parameters. • Syntax: label PROC paramList • paramList is a list of parameters separated by commas. Each parameter has the following syntax: paramName : type type must either be one of the standard ASM types (BYTE, SBYTE, WORD, etc.), or it can be a pointer to one of these types. PROC Directive (2 of 2) • Alternate format permits parameter list to be on one or more separate lines: label PROC, comma required paramList • The parameters can be on the same line . . . param-1:type-1, param-2:type-2, . . ., param-n:type-n • Or they can be on separate lines: param-1:type-1, param-2:type-2, ..., param-n:type-n AddTwo Procedure (1 of 2) • The AddTwo procedure receives two integers and returns their sum in EAX. AddTwo PROC, val1:DWORD, val2:DWORD mov eax,val1 add eax,val2 ret AddTwo ENDP PROC Examples (2 of 3) FillArray receives a pointer to an array of bytes, a single byte fill value that will be copied to each element of the array, and the size of the array. FillArray PROC, pArray:PTR BYTE, fillVal:BYTE arraySize:DWORD mov ecx,arraySize mov esi,pArray mov al,fillVal L1: mov [esi],al inc esi loop L1 ret FillArray ENDP PROC Examples Swap PROC, pValX:PTR DWORD, pValY:PTR DWORD . . . Swap ENDP ReadFile PROC, pBuffer:PTR BYTE LOCAL fileHandle:DWORD . . . ReadFile ENDP (3 of 3) RET Instruction • Pops stack into the instruction pointer (EIP or IP). Control transfers to the target address. • Syntax: – RET – RET n • Optional operand n causes n bytes to be added to the stack pointer after EIP (or IP) is assigned a value. PROTO Directive • Creates a procedure prototype • Syntax: – label PROTO paramList • Every procedure called by the INVOKE directive must have a prototype • A complete procedure definition can also serve as its own prototype PROTO Directive • Standard configuration: PROTO appears at top of the program listing, INVOKE appears in the code segment, and the procedure implementation occurs later in the program: MySub PROTO ; procedure prototype .code INVOKE MySub ; procedure call MySub PROC . . MySub ENDP ; procedure implementation PROTO Example • Prototype for the ArraySum procedure, showing its parameter list: ArraySum PROTO, ptrArray:PTR DWORD, szArray:DWORD ; points to the array ; array size Passing by Value • When a procedure argument is passed by value, a copy of a 32-bit integer is pushed on the stack. Example: .data myData DWORD 10000h .code main PROC INVOKE Sub1, myData MASM generates the following code: push myData call Sub1 Passing by Value (16-bit) • In 16-bit mode, you can also push a 16-bit integer on the stack before calling a procedure: .data myData WORD 1000h .code main PROC INVOKE Sub1, myData MASM generates the following code: push myData call Sub1 Passing by Reference • When an argument is passed by reference, its address is pushed on the stack. Example: .data myData WORD 1000h .code main PROC INVOKE Sub1, ADDR myData MASM generates the following code: push OFFSET myData call Sub1 Parameter Classifications • An input parameter is data passed by a calling program to a procedure. – The called procedure is not expected to modify the corresponding parameter variable, and even if it does, the modification is confined to the procedure itself. • An output parameter is created by passing a pointer to a variable when a procedure is called. – The procedure does not use any existing data from the variable, but it fills in a new value before it returns. • An input-output parameter is a pointer to a variable containing input that will be both used and modified by the procedure. – The variable passed by the calling program is modified. Example: Exchanging Two Integers The Swap procedure exchanges the values of two 32bit integers. pValX and pValY do not change values, but the integers they point to are modified. Swap PROC USES eax esi edi, pValX:PTR DWORD, ; pointer to first integer pValY:PTR DWORD ; pointer to second integer mov esi,pValX mov edi,pValY mov eax,[esi] xchg eax,[edi] mov [esi],eax ret Swap ENDP ; get pointers ; get first integer ; exchange with second ; replace first integer Trouble-Shooting Tips • Save and restore registers when they are modified by a procedure. – Except a register that returns a function result • When using INVOKE, be careful to pass a pointer to the correct data type. – For example, MASM cannot distinguish between a DWORD argument and a PTR BYTE argument. • Do not pass an immediate value to a procedure that expects a reference parameter. – Dereferencing its address will likely cause a generalprotection fault. Stack Frames – Memory Models – Language Specifiers – Explicit Access to Stack Parameters – Passing Arguments by Reference – Creating Local Variables Stack Frame • Also known as an activation record. • Area of the stack set aside for a procedure's return address, passed parameters, saved registers, and local variables • Created by the following steps: – Calling program pushes arguments on the stack and calls the procedure. – The called procedure pushes EBP on the stack, and sets EBP to ESP. – If local variables are needed, a constant is subtracted from ESP to make room on the stack. Memory Models • A program's memory model determines the number and sizes of code and data segments. • Real-address mode supports tiny, small, medium, compact, large, and huge models. • Protected mode supports only the flat model. Small model: code < 64 KB, data (including stack) < 64KB. All offsets are 16 bits. Flat model: single segment for code and data, up to 4 GB. All offsets are 32 bits. .MODEL Directive • .MODEL directive specifies a program's memory model and model options (language-specifier). • Syntax: .MODEL memorymodel [,modeloptions] • memorymodel can be one of the following: – tiny, small, medium, compact, large, huge, or flat • modeloptions includes the language specifier: – procedure naming scheme – parameter passing conventions Language Specifiers • C: – procedure arguments pushed on stack in reverse order (right to left) – calling program cleans up the stack • pascal – procedure arguments pushed in forward order (left to right) – called procedure cleans up the stack • stdcall – procedure arguments pushed on stack in reverse order (right to left) – called procedure cleans up the stack Explicit Access to Stack Parameters • A procedure can explicitly access stack parameters using constant offsets from EBP1. – Example: [ebp + 8] • EBP is often called the base pointer or frame pointer because it holds the base address of the stack frame. • EBP does not change value during the procedure. • EBP must be restored to its original value when a procedure returns. 1 BP in Real-address mode Stack Frame Example .data sum DWORD ? .code push 6 push 5 call AddTwo mov sum,eax AddTwo PROC push ebp mov ebp,esp . . ; ; ; ; (1 of 2) second argument first argument EAX = sum save the sum 00000006 [EBP + 12] 00000005 [EBP + 8] return address [EBP + 4] EBP EBP, ESP AddTwo Procedure (1 of 3) • Recall the AddTwo Procedure AddTwo PROC, val1:DWORD, val2:DWORD mov eax,val1 add eax,val2 ret AddTwo ENDP AddTwo Procedure (2 of 3) • MASM generates the following code when we assemble AddTwo (from the previous panel): AddTwo PROC, val1:DWORD, val2:DWORD push ebp mov ebp, esp mov add eax,val1 eax,val2 leave ret 8 AddTwo ENDP The LEAVE instruction is shorthand for: mov pop esp,ebp ebp AddSub Procedure 00000006 [EBP + 12] 00000005 [EBP + 8] return address [EBP + 4] EBP AddTwo PROC push ebp mov ebp,esp mov eax,[ebp + 12] add eax,[ebp + 8] leave ret 8 AddTwo ENDP (3 of 3) EBP, ESP ; base of stack frame ; second argument (6) ; first argument (5) ; EAX contains the sum Drill . . . • Create a procedure named Difference that subtracts the first argument from the second one. Following is a sample call: push 14 ; first argument push 30 ; second argument call Difference ; EAX = 16 Difference PROC push ebp mov ebp,esp mov eax,[ebp + 8] sub eax,[ebp + 12] pop ebp ret 8 Difference ENDP ; second argument ; first argument Passing Arguments by Reference (1 of 2) • The ArrayFill procedure fills an array with 16-bit random integers • The calling program passes the address of the array, along with a count of the number of array elements: .data count = 100 array WORD count DUP(?) .code push OFFSET array push COUNT call ArrayFill Passing Arguments by Reference (2 of 2) ArrayFill can reference an array without knowing the array's name: ArrayFill PROC push ebp mov ebp,esp pushad mov esi,[ebp+12] mov ecx,[ebp+8] . . offset(array) [EBP + 12] count [EBP + 8] return address EBP EBP ESI points to the beginning of the array, so it's easy to use a loop to access each array element. Passing Arguments by Reference .data count = 100 array WORD count DUP(?) .code start: push OFFSET array push count call ArrayFill mov esi,OFFSET array mov ecx,count mov ebx,2 . . . . (2 of 2) ArrayFill PROC push ebp mov ebp,esp pushad mov esi,[ebp+12] ; offset of array mov ecx,[ebp+8] ; array size cmp ecx,0 ; ECX < 0? jle L2 ; yes: skip over loop L1: mov [esi],0 add esi,TYPE WORD loop L1 L2: popad pop ebp ret 8 ; clean up the stack ArrayFill ENDP LEA Instruction • The LEA instruction returns offsets of both direct and indirect operands. – OFFSET operator can only return constant offsets. • LEA is required when obtaining the offset of a stack parameter or local variable. For example: CopyString PROC, count:DWORD LOCAL temp[20]:BYTE mov mov lea lea edi,OFFSET count esi,OFFSET temp edi,count esi,temp ; ; ; ; invalid operand invalid operand ok ok Creating Local Variables • To explicitly create local variables, subtract their total size from ESP. • The following example creates and initializes two 32bit local variables (we'll call them locA and locB): MySub PROC push ebp mov ebp,esp sub esp,8 mov [ebp-4],123456h mov [ebp-8],0 . . ; locA ; locB Summary • Local Variables • Stack Parameters – Register vs. Stack Parameters – INVOKE Directive – PROC Directive – PROTO Directive – Passing by Value or by Reference – Parameter Classifications – Example: Exchanging Two Integers – Trouble-Shooting Tips Summary • Stack Frames – Memory Models – Language Specifiers – Explicit Access to Stack Parameters – Passing Arguments by Reference – Creating Local Variables (cont.) Reference Most of the Slides are taken from Presentation: Chapter 8 Assembly Language for Intel-Based Computers, 4th Edition Kip R. Irvine (c) Pearson Education, 2002. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.