1 / 42

Introduction to Assembly Language IA32-IV

Introduction to Assembly Language IA32-IV. Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University. Objectives. Defining Procedures Using stack. Procedures. The part of the program that is needed to be done many times is defined in the procedure

Download Presentation

Introduction to Assembly Language IA32-IV

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Assembly Language IA32-IV Summer2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

  2. Objectives • Defining Procedures • Using stack Introduction

  3. Procedures • The part of the program that is needed to be done many times is defined in the procedure • Each procedure is identified by a name • The procedure is defined as a label but after the execution of the procedure, the execution returns to the same place from where it has been called when ret (return) statement is executed • The procedure may flow along multiple labels as well Introduction

  4. syntax Proc_name: statements statements ret Introduction

  5. example movecx, '4' sub ecx, '0' movedx, '5' sub edx, '0' call sum ;calling the sum procedure mov [res], eax sum: ; this is where the procedure is defined moveax, ecx add eax, edx add eax, '0' ret Introduction

  6. Example 2 _start: ;tell linker entry point moveax, 5 movebx, 4 call display moveax, 2 movebx, 3 call display mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel display: add eax, ebx add eax, '0' mov [res], eax moveax, 4 movebx, 1 movecx, res movedx, 1 int 80h ret Introduction

  7. Example 3 • To calculate the factorial of three proc_fact: cmpbl, 1 jgdo_calculation mov ax, 1 ret do_calculation: decbl call proc_fact incbl mulbl ;ax = al * bl ret Introduction

  8. stack • Stack plays an important role when we use the procedures • When a program starts executing, a certain contiguous section of memory is set aside for the program called the stack. The stack implementation has some special features, which are : • The stack can only hold wordsor doublewords, not a byte. • The stack grows in the reverse direction, i.e., toward the lower memory address • The top of the stack points to the last item inserted in the stack; it points to the lower byte of the last word inserted. • The stack pointer is the register that contains the top of the stack. Introduction

  9. Pointers • There are two pointers related to stack • Stack pointer – ESP – Points to the top of the stack • Stack base pointer – EBP – points to the base of the stack Introduction

  10. eax eax eax 0x123 0x123 0x123 edx edx edx 0 0x123 0 esp esp esp 0x108 0x108 0x104 Stack Illustration • Figure 3.5 Illustration of stack operation Initially pushleax popledx Stack “bottom” Stack “bottom” Stack “bottom” • • • • • • • • • 0x108 0x108 0x108 0x123 0x123 Stack “top” 0x104 Introduction Stack “top” Stack “top”

  11. Stack Pointer • pushl %eaxis equivalent to subl $4, %esp movl %eax, (%esp) • popl %edxis equivalent to movl (%esp), %edx addl $4, %esp Introduction

  12. Carnegie Mellon Using Simple Addressing Modes • • • swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %eax, (%edx) movl %ebx, (%ecx) popl %ebx popl %ebp ret Set Up void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Offset from ebp yp: &y 12 xp: &x 8 Rtn adr 4 Body Stack (in memory) Old ebp 0 ebp Old ebx -4 esp Finish Prepared by the caller function espinitially Introduction

  13. Carnegie Mellon Understanding Swap • • • void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Stack (in memory) Offset from %ebp yp: &y 12 Prepared by the caller function xp: &x 8 Rtn adr 4 Old %ebp 0 %ebp How to use the variables? Old %ebx -4 %esp Register Value %edx xp %ecx yp %ebx t0 %eax t1 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

  14. Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer: esp Stack “Top” Carnegie Mellon IA32 Stack • Region of memory managed with stack discipline • Grows toward lower addresses • Register %esp contains lowest stack address • address of “top” element

  15. Stack Pointer: esp -4 Stack “Top” Carnegie Mellon IA32 Stack: Push Stack “Bottom” • pushlSrc • Fetch operand at Src • Decrement %esp by 4 • Write operand at address given by esp Increasing Addresses Stack Grows Down

  16. +4 Carnegie Mellon IA32 Stack: Pop Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer: esp Stack “Top”

  17. Carnegie Mellon Procedure Control Flow • Use stack to support procedure call and return • Procedure call:calllabel, consisting of • Push return address on stack • Jump to label How to jump? What does “jump” really mean? • Return address: • Address of the next instruction right after call • Example from disassembly call 8048b90 <main> push %eax • Return address = 0x8048553 • Procedure return:ret, consisting of • Pop address from stack • Jump to address

  18. Carnegie Mellon Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 push eax Push … Jump … call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x104 %eip 0x804854e %eip 0x8048b90 %eip: the name of program counter (PC)

  19. Carnegie Mellon Procedure Return Example 8048591: c3 ret Pop … Jump … ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x108 %eip 0x8048591 %eip 0x8048553 %eip: program counter

  20. Carnegie Mellon Stack-Based Languages • Languages that support recursion • e.g., C, Pascal, Java • Code must be “Reentrant” • Multiple simultaneous instantiations of single procedure • Need some place to store state of each instantiation • Arguments (i.e., paramenters) • Local variables • Return pointer • Stack discipline • State for given procedure needed for limited time • From when called to when return • Callee returns before caller does • Stack allocated in Frames • Frame: the portion of the stack allocated for a single procedure instantiation

  21. Carnegie Mellon Call Chain Example Example Call Chain yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } who amI amI amI(…) { • • amI(); • • } amI amI Procedure amI() is recursive

  22. Carnegie Mellon Stack Frames • Contents • Local variables • Return information • Temporary space • Management • Space allocated when enter procedure • “Set-up” code • Deallocated when return • “Finish” code Frame Pointer: ebp Stack Pointer: esp Stack “Top”

  23. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who amI amI amI amI

  24. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } who amI amI amI amI

  25. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } amI(…) { • • amI(); • • } who amI amI amI amI

  26. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } amI(…) { • • amI(); • • } who amI(…) { • • amI(); • • } amI amI amI amI

  27. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } amI(…) { • • amI(); • • } who amI(…) { • • amI(); • • } amI amI amI(…) { • • amI(); • • } amI amI

  28. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } amI(…) { • • amI(); • • } who amI(…) { • • amI(); • • } amI amI amI amI

  29. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } amI(…) { • • amI(); • • } who amI amI amI amI

  30. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } who amI amI amI amI

  31. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } amI(…) { • • amI(); • • } who amI amI amI amI

  32. ebp esp Carnegie Mellon Example Stack yoo yoo(…) { • • who(); • • } yoo who(…) { • • • amI(); • • • amI(); • • • } who amI amI amI amI

  33. ebp esp Carnegie Mellon Example Stack yoo yoo yoo(…) { • • who(); • • } who amI amI amI amI

  34. Carnegie Mellon IA32/Linux Stack Frame • Current Stack Frame (“Top” to Bottom) contains • “Argument build:” Parameters for another function about to call • Local variables If can’t keep in registers • Saved register context • Old frame pointer • Caller Stack Frame contains • Return address • Pushed by call instruction • Arguments for this call Caller Frame Arguments Parameters for the callee Frame pointer%ebp Return Addr Old ebp Saved Registers + Local Variables Current Frame Argument Build Stack pointer %esp

  35. Carnegie Mellon Revisiting swap Calling swap from call_swap int course1 = 15213; int course2 = 18243; void call_swap() { swap(&course1, &course2); } call_swap: • • • subl $8, %esp movl $course2, 4(%esp) movl $course1, (%esp) call swap • • • • • • Resulting Stack void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } %esp &course2 subl &course1 %esp call Rtn adr %esp

  36. Carnegie Mellon Revisiting swap swap: pushl %ebp movl %esp, %ebp pushl %ebx movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %eax, (%edx) movl %ebx, (%ecx) popl %ebx popl %ebp ret Set Up void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Finish

  37. Carnegie Mellon swap Setup #1 Entering Stack Resulting Stack %ebp %ebp • • • • • • &course2 yp &course1 xp %esp Rtn adr Rtn adr Old ebp %esp swap: pushl%ebp movl %esp, %ebp push %ebx

  38. Carnegie Mellon swap Setup #2 Entering Stack Resulting Stack %ebp • • • • • • &course2 yp &course1 xp %esp Rtn adr Rtn adr %ebp Old %ebp %esp swap: push %ebp mov %esp,%ebp push %ebx

  39. Carnegie Mellon swap Setup #3 Entering Stack Resulting Stack %ebp • • • • • • &course2 yp &course1 xp %esp Rtn adr Rtn adr Old ebp %ebp Old ebx %esp swap: push ebp movl%esp, %ebp push %ebx

  40. Carnegie Mellon swap Body Entering Stack Resulting Stack ebp • • • • • • Offset relative to ebp &course2 yp 12 &course1 8 xp esp Rtn adr 4 Rtn adr Old %ebp %ebp Old %ebx %esp movl 8(%ebp),%edx # get xp movl 12(%ebp),%ecx # get yp . . .

  41. • • yp xp Rtn adr Old %ebp %ebp Old %ebx %esp Carnegie Mellon swap Finish Stack Before Finish Resulting Stack %ebp • • • popl %ebx popl %ebp ret yp xp Rtn adr %esp • Observation • Saved and restored register %ebx • Not so for %eax, %ecx, %edx

  42. Carnegie Mellon IA32/Linux+Windows Register Usage • %eax, %edx, %ecx • Caller saves prior to call if values are used later • %eax • also used to return integer value • %ebx, %esi, %edi • Callee saves if wants to use them • %esp, %ebp • special form of callee save • Restored to original values upon exit from procedure %eax Caller-Save Temporaries %edx %ecx %ebx Callee-Save Temporaries %esi %edi %esp Special %ebp

More Related