1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine
Outline Stack OperationsStack Operations Defining and Using ProceduresDefining and Using Procedures
Stack Operations n Why Stacks? Recall data structures class. Recall how procedure (functions) used. Procedure calling is a stack operation. We use stack to keep track of return addresses. Parameters and local variables are also put on the stack when calling some subroutines.
Stack Operations n Concepts A stack is a LIFO (last-in, first-out) structure The runtime stack is a memory that is managed directly by the CPU, using two registers: SS and ESP SS holds a segment descriptor and is not modified by user program ESP holds a 32-bit offset into some location on the stack An Intel stack grows downward from high memory to low memory
Format: PUSH r/m16/m32/imm16/imm32 Stack Operations n PUSH Operation Immediate values are 32-bit in protected mode and 16-bit in real mode A 16-bit/32-bit operand causes ESP to be decremented by 2 and 4, respectively. Push AX Push BX Offset Stack AB AX BX AB ESP XX YY ESP ESP AB 01
Format: POP r/m16/m32 Stack Operations n POP Operation POP BX POP AX Offset Stack AB AX BX ? 0000? XX YY ESP AB 01 ESP 01AB 0024 ESP The area of the stack above ESP is logically empty, and will be overwritten
Stack Operations n Additional Stack Operations PUSHF and POPF Push and pops the Flag register. There are no operands PUSHAD and POPAD Pushes registers on the stack in this order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI and pops them in reverse order PUSHA and POPA The same except they work with 16-bit registers
Outline Stack OperationsStack Operations Defining and Using ProceduresDefining and Using Procedures
Procedures n Concepts Procedure: blocks of code that are called and must be returned from A procedure begins with itsname proc and terminate with itsname endp To end a procedure other that the program startup procedure (main), use ret instruction Use call itsname to call the procedure. It is a highly desirable to preserve registers when writing a procedure. Save at beginning and restore before returning
Procedures n CALL and RET Instructions CALL: directs the processor to begin execution at a new memory location Pushes the return address on the stack Copies the called procedure address into the EIP. RET: brings the processor back to the point in the program where the procedure was called Popes the return address from the stack into the EIP.
??? Procedures n Examples main PROC Call MyProc MOV eax, ebx … MyProc PROC MOV eax, edx … ret MyProc Endp ESP EIP ??? ESP EIP
Procedures n Nested Procedures ESP (return to sub2) (return to sub1) (return to main) Low high
Procedures n Local and Global Labels Global labels are followed by two colons, making them visible to the whole program By default, a code label (followed by a single colon) has local scope, making it visible only to statements inside its enclosing procedure main PROC Sub PROC JMP L2 L2: L1:: JMP L1 main endp ret … ?
Procedures n Passing Parameter In registers - Fastest In global variables - Hard to reuse, poor programming practice On the stack - Used by high level languages
Procedures n Examples Calculating the sum of an integer array It is better to pass the offset of an array to the procedure than to include references to specific variable names inside the procedure
Save and restore registers that are modified by a procedure The return register should not be pushed and popped
Procedures n Design using Procedures Divide the specification into tasks Understand the specification of the program Design each task as a sub-procedure Design the main procedure (the program start up procedure) to call all sub- procedures
Linking to External Library n Link Library A file containing procedures that have been assembled into machine code Linker command In your program, these procedure could be included and called The assembler would leave the target address of the Call instruction blank, which will be filled by the linker link32 %1.obj libraryName.lib
Outline Stack OperationsStack Operations Defining and Using ProceduresDefining and Using Procedures