CS 2422 Assembly Language and System Programming Procedures
CS 2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University
Assembly Language for Intel. Based Computers, 5 th Edition CS 2422 Assembly Language and System Programming Kip Irvine Chapter 5: Procedures Slides prepared by the author Revision date: June 4, 2006 (c) Pearson Education, 2006 -2007. 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.
Chapter Overview u u u Linking to an External Library The Book's Link Library Stack Operations Defining and Using Procedures Program Design Using Procedures Emphasize on Sections 5. 4 and 5. 5. Also browse Sections 5. 1 to 5. 3. 2
Runtime Stack u Different from the “stack” data structure that you created for your program l u Your application program manages that stack explicitly Runtime stack: l l Created and managed automatically by the system (e. g. assembler, OS, CPU) Occupies a portion of the memory Managed through two registers: SS (stack segment) and ESP (stack pointer) * Your program only needs to access it with special ASM instructions; you do not need to create or manage it * SP in real-address mode 3
PUSH Operation u A 32 -bit push operation decrements stack pointer by 4 and then copies a value into the location pointed to by the stack pointer The stack grows downward. The area below ESP is always available (unless the stack has overflowed). 4
POP Operation u u Copies value at stack[ESP] into a register or variable. Adds n to ESP, where n is either 2 or 4. l depends on the operand receiving the data 5
PUSH and POP Instructions u PUSH syntax: l l l u PUSH r/m 16 PUSH r/m 32 PUSH imm 32 POP syntax: l l POP r/m 16 POP r/m 32 r/m meaning register/memory 6
Using PUSH and POP u Save and restore registers when they contain important values: push esi push ecx push ebx ; push registers mov esi, OFFSET dword. Val ; starting OFFSET mov ecx, LENGTHOF dword. Val ; number of units mov ebx, TYPE dword. Val ; size of doubleword call Dump. Mem ; display memory pop ebx pop ecx pop esi ; opposite order 7
Example: Nested Loop u When creating a nested loop, push the outer loop counter before entering the inner loop: mov ecx, 100 L 1: push ecx ; set outer loop count ; begin the outer loop ; save outer loop count mov ecx, 20 L 2: ; ; loop L 2 ; set inner loop count ; begin the inner loop pop ecx loop L 1 ; repeat the inner loop ; restore outer loop count ; repeat the outer loop 8
Related Instructions u PUSHFD and POPFD l u PUSHAD pushes the set of 32 -bit generalpurpose registers on the stack l u push and pop the EFLAGS register order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI POPAD pops the same set of registers off the stack in reverse order l PUSHA and POPA do the same for 16 -bit registers 9
What’s Next u u u Linking to an External Library The Book's Link Library Stack Operations Defining and Using Procedures Program Design Using Procedures 10
Creating Procedures u u A procedure is the ASM equivalent of a Java or C++ function Following is an assembly language procedure named sample: sample PROC. . ret sample ENDP 11
CALL and RET Instructions u The CALL instruction calls a procedure l l u pushes offset of next instruction on the stack copies the address of the called procedure into EIP (Note: IP=Instruction Pointer) The RET instruction returns from a procedure l pops top of stack into EIP 12
CALL-RET Example (1/2) 0000025 is the offset of the instruction immediately following the CALL instruction main PROC 00000020 call My. Sub 00000025 mov eax, ebx. . main ENDP 00000040 is the offset of the first instruction inside My. Sub PROC 00000040 mov eax, edx. . ret My. Sub ENDP 13
CALL-RET Example (2/2) CALL pushes 00000025 onto stack, and loads 00000040 into EIP RET pops 00000025 from stack into EIP Could POP wrong address? 14
Nested Procedure Calls By the time Sub 3 is called, the stack contains all three return addresses: 15
Local and Global Labels u u Local label: visible only to statements inside the same procedure Global label: visible everywhere main PROC jmp L 2 L 1: : exit main ENDP sub 2 PROC L 2: jmp L 1 ret sub 2 ENDP ; error! ; global label ; local label ; ok 16
Procedure Parameters (1/2) u Calculating sum of an array, making two references to specific variable names: Array. Sum PROC mov esi, 0 ; array index mov eax, 0 ; set the sum to zero L 1: add eax, my. Array[esi] ; add each to sum add esi, 4 ; point to next integer loop L 1 ; repeat for array size mov the. Sum, eax ; store the sum ret Array. Sum ENDP What if to calculate sum of 2 or 3 arrays using same program? 17
Procedure Parameters (2/2) u This version returns sum of any doubleword array whose address is in ESI: Array. Sum PROC ; Receives: ESI points to an array of doublewords ; ECX = number of array elements ; Returns: EAX = sum ; ----------------------mov eax, 0 ; set the sum to zero L 1: add eax, [esi] ; add each integer to sum add esi, 4 ; point to next integer loop L 1 ; repeat for array size ret What should the caller Array. Sum ENDP procedure do? 18
How to Call the Procedure? main PROC mov esi, OFFSET my. Array mov ecx, LENTHOF my. Array call Array. Sum mov the. Sum, eax exit main ENDP Array. Sum PROC mov eax, 0 ; set the sum to zero L 1: add eax, [esi] ; add each # to sum add esi, 4 ; point to next # loop L 1 ; repeat for array size ret 19 Array. Sum ENDP
What Happen in Hardware? Processor EAX EBX ECX ESI Register my. Array_size … ESP When ‘call Array. Sum’ is executed. . . Memory my. Array. . . Data Segment the. Sum main PROC call Array. Sum mov the. Sum, eax main ENDP Array. Sum PROC mov eax, 0 ret Array. Sum ENDP EFLAGS EIP ALU Code Segment … Stack Segment 20
What Happen in Hardware? Processor EAX EBX ECX ESI Register my. Array_size … ESP When ‘mov eax, 0’ is executed. . . Memory my. Array. . . Data Segment the. Sum main PROC call Array. Sum mov the. Sum, eax main ENDP Array. Sum PROC mov eax, 0 ret Array. Sum ENDP EFLAGS EIP ALU Code Segment … Addr ‘mov the. Sum, eax’ Stack Segment Procedure can see everything in CPU 21
USES Operator u Lists the registers that will be saved Array. Sum PROC USES esi ecx mov eax, 0 ; set the sum to zero. . . MASM generates the following code: Array. Sum PROC push esi push ecx. . pop ecx pop esi ret Array. Sum ENDP 22
Summary of Procedures u u Procedure definition: proc_name PROC RET proc_name ENDP Procedure invocation: CALL-RET l u Through stack Parameter passing: l Through stack or registers 23
What's Next u u u Linking to an External Library The Book's Link Library Stack Operations Defining and Using Procedures Program Design Using Procedures 24
Link Library Overview u A file containing procedures that have been compiled into machine code l u constructed from one or more OBJ files To build a library, l l start with one or more ASM source files assemble each into an OBJ file create an empty library file (extension. LIB) add the OBJ file(s) to the library file, using the Microsoft LIB utility (Take a quick look at Irvine 32. asm that comes from the author. ) 25
Calling a Library Procedure u u u Call a library procedure using CALL. Some procedures require input arguments. The INCLUDE directive copies in the procedure prototypes (declarations). Displaying "1234" on the console: INCLUDE Irvine 32. inc. code mov eax, 1234 h ; input argument call Write. Hex ; show hex number call Crlf ; end of line 26
Linking to a Library u u Your programs link to Irvine 32. lib using the linker command inside a batch file named make 32. bat. Note the LIB files: Irvine 32. lib and kernel 32. lib l The latter is part of Microsoft Win 32 Software Development Kit 27
Library Procedures of Textbook u u u u Clrscr - Clear console & put cursor at upper left corner Crlf - Write end-of-line to standard output Delay - Pause program execution for a specified n millisecond interval Dump. Mem - Write a block of memory to standard output in hexadecimal Dump. Regs – Dump register contents and flags Get. Commandtail - Copy program’s commandline arguments (i. e. command tail) into an array Gotoxy - Locate cursor at row and column on the console 28
Library Procedures of Textbook u u u u Random 32 - Generate a 32 -bit pseudorandom integer in the range 0 to FFFFh Randomize - Seed random number generator Read. Int - Read a 32 -bit signed decimal integer from standard input, terminated by the Enter key Read. String - Read a string from standard input, terminated by the Enter key Wait. Msg - Display message, wait for Enter key Write. Int - Write a signed 32 -bit integer to standard output in decimal format Write. String - Write a null-terminated string to std output 29
Example 1 u Clear the screen, delay the program for 500 milliseconds, and dump the registers and flags. code call Clrscr mov eax, 500 call Delay call Dump. Regs u Sample output: EAX=00000613 EBX=0000 ECX=000000 FF EDX=0000 ESI=0000 EDI=00000100 EBP=0000091 E ESP=000000 F 6 EIP=00401026 EFL=00000286 CF=0 SF=1 ZF=0 OF=0 30
Example 2 u Display a null-terminated string and move the cursor to the beginning of the next screen line . data str 1 BYTE "Assembly language is easy!", 0. code mov edx, OFFSET str 1 call Write. String call Crlf 31
Example 3 u Input a string from the user l l EDX: points to the string ECX: max. # of characters permitted to enter. data file. Name BYTE 80 DUP(0). code mov edx, OFFSET file. Name mov ecx, SIZEOF file. Name – 1 call Read. String A null byte is automatically appended to the string. 32
Example 4 u Display a null-terminated string with yellow characters on a blue background. . data str 1 BYTE "Color output is easy!", 0. code mov eax, yellow + (blue * 16) call Set. Text. Color mov edx, OFFSET str 1 call Write. String call Crlf l The background color is multiplied by 16 before being added to the foreground color. 33
Summary u u Procedure: named block of executable code Runtime stack: LIFO structure l l l u holds return addresses, parameters, local variables PUSH: add value to stack POP: remove value from stack Want to learn more? l Study the Irvine 32 library source code for all standard I/O and data conversion 34
- Slides: 35