1 / 39

a sum.ys A Y86 Programming Example

a sum.ys A Y86 Programming Example. Y86 Sample Program Structure. Program starts at address 0x0 Stack starts at address 0x100 Initialize the array (data). . pos 0 init : # Initialization ... call Main halt

dferrante
Download Presentation

a sum.ys A Y86 Programming Example

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. asum.ys A Y86 Programming Example

  2. Y86 Sample Program Structure Program starts at address 0x0 Stack starts at address 0x100 Initialize the array (data) .pos 0 init: # Initialization ... call Main halt .align 4 # Program data array: ... Main: # Main function ... call Sum ... ret Sum: # Length function ... ret .pos 0x100 # Place stack Stack:

  3. 3.init: irmovl Stack, %esp# Set up stack pointer 4. irmovl Stack, %ebp# Set up base pointer • %esp • %ebp Because line 46 and line 47 makes the label “Stack” at address 0x100, So the irmovls make %esp == 0x100 and %ebp == 0x100 now.

  4. 5. call main • %esp • %esp • %ebp • %esp 0x11 0x11 is the address of “6: halt”.

  5. 15. Main: pushl %ebp • %ebp • %esp • %esp 0x11 • %esp 0x100

  6. 16. rrmovl %esp,%ebp • %ebp • %ebp 0x11 • %ebp • %esp 0x100

  7. 17.irmovl $4,%eax 18.pushl %eax# Push 4 0x11 • %ebp • %esp • %esp 0x100 • %esp 4 4 is the value of count (4 elements in the array).

  8. 19.irmovl array,%edx 20.pushl %edx • %ebp • %esp • %esp • %esp 0x14 0x14 is the first element’s address of the array. Here we finished storing the arguments to be passed.

  9. 21.call Sum • %ebp • %esp • %esp • %esp 0x3d 0x3d is the address of “22: rrmovl %ebp,%esp”.

  10. 27.Sum: pushl %ebp 28. rrmovl %esp,%ebp • %ebp • %ebp • %esp • %esp • %esp • %ebp 0xf8

  11. 29.mrmovl 8(%ebp),%ecx # ecx = Start 30. mrmovl 12(%ebp),%edx # edx = Count

  12. 31.xorl %eax,%eax# sum = 0

  13. 32.andl %edx,%edx# Set condition codes

  14. 33.je End It’s like a if statement for checking. Why need line 32 and 33? How to improve them?

  15. 34.Loop: mrmovl (%ecx),%esi# get *Start Note that the parenthesis of(%ecx) is necessary.

  16. 35. addl %esi,%eax# add to sum 36. irmovl $4,%ebx #

  17. 37. addl %ebx,%ecx# Start++ 38. irmovl $-1,%ebx #

  18. 39. addl %ebx,%edx# Count-- 40. jne Loop # Stop when 0

  19. 34. Loop: mrmovl (%ecx),%esi# get *Start 35. addl %esi,%eax# add to sum

  20. 36. irmovl $4,%ebx # 37. addl %ebx,%ecx# Start++

  21. 38. irmovl $-1,%ebx # 39. addl %ebx,%edx# Count--

  22. 40. jne Loop # Stop when 0

  23. 34. Loop: mrmovl (%ecx),%esi# get *Start 35. addl %esi,%eax# add to sum

  24. 36. irmovl $4,%ebx # 37. addl %ebx,%ecx # Start++

  25. 38. irmovl $-1,%ebx # 39. addl %ebx,%edx # Count--

  26. 30. jne Loop # Stop when 0

  27. 34. Loop: mrmovl (%ecx),%esi# get *Start 35. addl %esi,%eax# add to sum

  28. 36. irmovl $4,%ebx # 37. addl %ebx,%ecx# Start++

  29. 38. irmovl $-1,%ebx # 39. addl %ebx,%edx# Count--

  30. 40. jne Loop # Stop when 0

  31. 41. End: rrmovl %ebp,%esp

  32. 42. popl %ebp • %ebp • %esp • %esp • %esp • %ebp • %ebp

  33. 0x7c 43. ret • %ebp • %esp • %esp • %esp 0x3d 0x3d

  34. 22. rrmovl %ebp,%esp • %esp • %esp • %esp

  35. 23. popl %ebp • %ebp • %esp • %esp • %esp • %ebp • %ebp

  36. 0x41 24. ret • %esp • %esp • %esp 0x11 0x11 0x11

  37. 6. halt

  38. A small question How to change line 32 and line 33 so that if count <= 0 the loop will not execute? 32: andl %edx, %edx 33: je End rrmovl %edx, %ebx # use %ebx as temporary place subl %eax, %ebx # here %eax == 0, so calculate %ebx - 0 jle End

  39. Some Takeaways In the called function: Fun: pushl %ebp rrmovl %esp,%ebp # Set up the stack space Before ret operation: rrmovl %ebp,%esp popl %ebp Use conditional jumps to implement if statement and loops call operation: push the address of next instruction onto the stack ret operation: pop stack top value to PC (program counter)

More Related