1 / 13

Tutorial 1

Tutorial 1. Question 6 Done by Chuan Huey Ling. Assembly Code. Initial Stack Assumption. We assume that the base pointer of the caller is at 8000 and the stack pointer is at 7000 “.global g .type g,@function” refers to g is global and is a function type. pushl %ebp.

eliot
Télécharger la présentation

Tutorial 1

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. Tutorial 1 Question 6 Done by Chuan Huey Ling

  2. Assembly Code

  3. Initial Stack Assumption • We assume that the base pointer of the caller is at 8000 and the stack pointer is at 7000 • “.global g .type g,@function” refers to g is global and is a function type.

  4. pushl %ebp • The base pointer, also known as the frame pointer, is push onto the stack so that it can be loaded when the function returns to the caller.

  5. movl %esp, %ebp • Change the value of ebp to the current esp.

  6. subl $8, %esp • Subtract 8 bytes from esp to move downwards. The space is used to store local and temporary variables if any.

  7. subl $12, %esp • Further subtract another 12 bytes of address from esp.

  8. pushl $4 ……. pushl $0 • The arguments of the function are pushed in reverse order onto the stack.

  9. call f • The return address of the next instruction which is “addl $32, %esp” is push to the stack.

  10. How is the f value return • The return value from f is store in the %eax register (4 bytes) since the return value is an integer. • Normally return value more than 4 bytes will return the address of the value in the stack. • And char or short will be store in the AL and AX register respectively.

  11. addl $32, %esp • Move the esp up 32 bytes to clear the arguments and clean the stack.

  12. movl %eax, -4(%ebp) • The value in the eax are being push on to the stack 4 bytes below ebp.

  13. leave & ret • What leave does is “movl %ebp, %esp” and “popl %ebp”. This is to move the esp back to the base pointer address and pop the caller %ebp value and load it into the base pointer register. From the previous example, ebp becomes 8000 again. • Return will pop the address of the next instruction of the caller.

More Related