tailieunhanh - Lecture Computer organization and assembly language - Lecture 25: Advanced Procedures

In this lecture, students will be able to understand: Computer organization, data representation, integer arithmetic, binary representation, floating point representation, machine instruction characteristics, instruction cycles, types of operands, pentium and power PC data types, microporessor bus structure, address, data. | CSC 221 Computer Organization and Assembly Language Lecture 25: Advanced Procedures in Assembly Lecture 24: Review LOCAL flagVals[20]:BYTE ; array of bytes LOCAL pArray:PTR WORD ; pointer to an array myProc PROC, ; procedure LOCAL t1:BYTE, ; local variables LOCAL Directive Examples: MASM-Generated Code (1 of 2) BubbleSort PROC LOCAL temp:DWORD, SwapFlag:BYTE . . . ret BubbleSort ENDP BubbleSort PROC push ebp mov ebp,esp add esp,0FFFFFFF8h ; add -8 to ESP . . . mov esp,ebp pop ebp ret BubbleSort ENDP MASM generates the following code: MASM-Generated Code (2 of 2) Diagram of the stack frame for the BubbleSort procedure: 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 . | CSC 221 Computer Organization and Assembly Language Lecture 25: Advanced Procedures in Assembly Lecture 24: Review LOCAL flagVals[20]:BYTE ; array of bytes LOCAL pArray:PTR WORD ; pointer to an array myProc PROC, ; procedure LOCAL t1:BYTE, ; local variables LOCAL Directive Examples: MASM-Generated Code (1 of 2) BubbleSort PROC LOCAL temp:DWORD, SwapFlag:BYTE . . . ret BubbleSort ENDP BubbleSort PROC push ebp mov ebp,esp add esp,0FFFFFFF8h ; add -8 to ESP . . . mov esp,ebp pop ebp ret BubbleSort ENDP MASM generates the following code: MASM-Generated Code (2 of 2) Diagram of the stack frame for the BubbleSort procedure: 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 ADDR Operator .data myWord WORD ? .code INVOKE mySub,ADDR myWord 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: PROC Directive 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. AddTwo Procedure (1 of 2) AddTwo PROC, val1:DWORD, val2:DWORD mov eax,val1 add eax,val2 ret AddTwo ENDP The AddTwo procedure receives two integers and returns their sum in EAX. PROC Examples (2 of 3) 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 .