410 likes | 444 Views
This example showcases the program structure using Y86 assembly language, starting at address 0x0 with stack at 0x100. Learn about initializing arrays, function calls, stack setup, and more.
E N D
asum.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 .align 4 # Program data array: ... Main: # Main function ... call Sum ... ret Sum: # Length function ... ret .pos 0x100 # Place stack Stack:
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.
5. call main • %esp • %esp • %ebp • %esp 0x11 0x11 is the address of “6: halt”.
15. Main: pushl %ebp • %ebp • %esp • %esp 0x11 • %esp 0x100
16. rrmovl %esp,%ebp • %ebp • %ebp 0x11 • %ebp • %esp 0x100
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).
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.
21.call Sum • %ebp • %esp • %esp • %esp 0x3d 0x3d is the address of “22: rrmovl %ebp,%esp”.
27.Sum: pushl %ebp 28. rrmovl %esp,%ebp • %ebp • %ebp • %esp • %esp • %esp • %ebp 0xf8
29.mrmovl 8(%ebp),%ecx # ecx = Start 30. mrmovl 12(%ebp),%edx # edx = Count
33.je End It’s like a if statement for checking. Why need line 32 and 33? How to improve them?
34.Loop: mrmovl (%ecx),%esi# get *Start Note that the parenthesis of(%ecx) is necessary.
35. addl %esi,%eax# add to sum 36. irmovl $4,%ebx #
37. addl %ebx,%ecx# Start++ 38. irmovl $-1,%ebx #
39. addl %ebx,%edx# Count-- 40. jne Loop # Stop when 0
34. Loop: mrmovl (%ecx),%esi# get *Start 35. addl %esi,%eax# add to sum
36. irmovl $4,%ebx # 37. addl %ebx,%ecx# Start++
38. irmovl $-1,%ebx # 39. addl %ebx,%edx# Count--
34. Loop: mrmovl (%ecx),%esi# get *Start 35. addl %esi,%eax# add to sum
36. irmovl $4,%ebx # 37. addl %ebx,%ecx # Start++
38. irmovl $-1,%ebx # 39. addl %ebx,%edx # Count--
34. Loop: mrmovl (%ecx),%esi# get *Start 35. addl %esi,%eax# add to sum
36. irmovl $4,%ebx # 37. addl %ebx,%ecx# Start++
38. irmovl $-1,%ebx # 39. addl %ebx,%edx# Count--
42. popl %ebp • %ebp • %esp • %esp • %esp • %ebp • %ebp
0x7c 43. ret • %ebp • %esp • %esp • %esp 0x3d 0x3d
22. rrmovl %ebp,%esp • %esp • %esp • %esp
23. popl %ebp • %ebp • %esp • %esp • %esp • %ebp • %ebp
0x41 24. ret • %esp • %esp • %esp 0x11 0x11 0x11
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
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)