Download presentation
Presentation is loading. Please wait.
1
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine
2
Outline Stack OperationsStack Operations Defining and Using ProceduresDefining and Using Procedures
3
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.
4
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
5
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 0024 01AB AX BX 0024 000001AB ESP 00001000 00001001 00001002 00001005 00001004 00001003 XX YY ESP 24 00 ESP AB 01
6
Format: POP r/m16/m32 Stack Operations n POP Operation POP BX POP AX Offset Stack 0024 01AB AX BX ? 0000? 00001000 00001001 00001002 00001005 00001004 00001003 XX YY 24 00 ESP AB 01 ESP 01AB 0024 ESP The area of the stack above ESP is logically empty, and will be overwritten
7
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
8
Outline Stack OperationsStack Operations Defining and Using ProceduresDefining and Using Procedures
9
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
10
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.
11
??? Procedures n Examples main PROC 00000020 Call MyProc 00000025 MOV eax, ebx … MyProc PROC 00000040 MOV eax, edx … ret MyProc Endp 00000025 ESP 00000040 EIP ??? 00000025 ESP 00000025 EIP
12
Procedures n Nested Procedures ESP (return to sub2) (return to sub1) (return to main) Low high
13
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 … ?
14
Procedures n Passing Parameter In registers - Fastest In global variables - Hard to reuse, poor programming practice On the stack - Used by high level languages
15
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
16
Save and restore registers that are modified by a procedure The return register should not be pushed and popped
17
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
18
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
19
Outline Stack OperationsStack Operations Defining and Using ProceduresDefining and Using Procedures
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.