Download presentation
Presentation is loading. Please wait.
1
asum.ys A Y86 Programming Example
Zhen Peng 11/14/2017
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 # Program data array: Main: # Main function call Sum ret Sum: # Length function .pos 0x # Place stack Stack:
3
3. init: irmovl Stack, %esp # Set up stack pointer
irmovl Stack, %ebp # Set up base pointer PC 0xc ZF SF OF 1 address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0x100 %ebp %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 PC 0x24 ZF SF OF 1 address stack
address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0xfc %ebp 0x100 %esp %esp %ebp %esp 0x11 0x11 is the address of “6: halt”.
5
15. Main: pushl %ebp %ebp %esp %esp %esp PC 0x26 ZF SF OF 1 address
address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0xf8 %ebp 0x100 %ebp %esp %esp 0x11 %esp 0x100
6
16. rrmovl %esp,%ebp %ebp %ebp %esp %ebp PC 0x28 ZF SF OF 1 address
address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0xf8 %ebp %ebp %ebp 0x11 %esp %ebp 0x100
7
17. irmovl $4,%eax 18. pushl %eax # Push 4 %esp %esp %ebp %esp PC 0x30
ZF SF OF 1 address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax 4 %ecx %edx %ebx %esi %edi %esp 0xf4 %ebp 0xf8 0x11 %esp %esp %ebp 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 PC 0x38 ZF SF
OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0xec register %eax 4 %ecx %edx 0x14 %ebx %esi %edi %esp 0xf0 %ebp 0xf8 %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 PC 0x42 ZF SF OF 1 address stack
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec register %eax 4 %ecx %edx 0x14 %ebx %esi %edi %esp 0xec %ebp 0xf8 %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
PC 0x46 ZF SF OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 4 %ecx %edx 0x14 %ebx %esi %edi %esp 0xe8 %ebp %ebp %ebp %esp %esp %esp %ebp 0xf8
11
29. mrmovl 8(%ebp),%ecx # ecx = Start
mrmovl 12(%ebp),%edx # edx = Count PC 0x52 ZF SF OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 4 %ecx 0x14 %edx %ebx %esi %edi %esp 0xe8 %ebp
12
31. xorl %eax,%eax # sum = 0 PC 0x54 ZF SF OF 1 address stack 0x100
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp
13
32. andl %edx,%edx # Set condition codes
PC 0x56 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp
14
33. je End PC 0x5b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp Why need line 32 and 33? How to improve them? It’s like a if statement for checking.
15
34. Loop: mrmovl (%ecx),%esi # get *Start
PC 0x61 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi 0xd %edi %esp 0xe8 %ebp Note that the parenthesis of (%ecx) is necessary.
16
35. addl %esi,%eax # add to sum 36. irmovl $4,%ebx #
PC 0x69 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xd %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp
17
37. addl %ebx,%ecx # Start++ 38. irmovl $-1,%ebx #
PC 0x71 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xd %ecx 0x18 %edx 4 %ebx -1 %esi %edi %esp 0xe8 %ebp
18
39. addl %ebx,%edx # Count-- 40. jne Loop # Stop when 0
PC 0x5b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xd %ecx 0x18 %edx 3 %ebx -1 %esi %edi %esp 0xe8 %ebp
19
34. Loop: mrmovl (%ecx),%esi # get *Start
addl %esi,%eax # add to sum PC 0x63 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x18 %edx 3 %ebx -1 %esi 0xc0 %edi %esp 0xe8 %ebp
20
37. addl %ebx,%ecx # Start++
36. irmovl $4,%ebx # 37. addl %ebx,%ecx # Start++ PC 0x6b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x1c %edx 3 %ebx 4 %esi 0xc0 %edi %esp 0xe8 %ebp
21
39. addl %ebx,%edx # Count--
38. irmovl $-1,%ebx # 39. addl %ebx,%edx # Count-- PC 0x73 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x1c %edx 2 %ebx -1 %esi 0xc0 %edi %esp 0xe8 %ebp
22
40. jne Loop # Stop when 0 PC 0x5b ZF SF OF address stack 0x100 0xfc
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x1c %edx 2 %ebx -1 %esi 0xc0 %edi %esp 0xe8 %ebp
23
34. Loop: mrmovl (%ecx),%esi # get *Start
addl %esi,%eax # add to sum PC 0x63 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x1c %edx 2 %ebx -1 %esi 0xb00 %edi %esp 0xe8 %ebp
24
37. addl %ebx,%ecx # Start++
irmovl $4,%ebx # addl %ebx,%ecx # Start++ PC 0x6b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x20 %edx 2 %ebx 4 %esi 0xb00 %edi %esp 0xe8 %ebp
25
39. addl %ebx,%edx # Count--
irmovl $-1,%ebx # addl %ebx,%edx # Count-- PC 0x73 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x20 %edx 1 %ebx -1 %esi 0xb00 %edi %esp 0xe8 %ebp
26
30. jne Loop # Stop when 0 PC 0x5b ZF SF OF address stack 0x100 0xfc
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x20 %edx 1 %ebx -1 %esi 0xb00 %edi %esp 0xe8 %ebp
27
34. Loop: mrmovl (%ecx),%esi # get *Start
addl %esi,%eax # add to sum PC 0x63 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x20 %edx 1 %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp
28
37. addl %ebx,%ecx # Start++
irmovl $4,%ebx # addl %ebx,%ecx # Start++ PC 0x6b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx 1 %ebx 4 %esi 0xa000 %edi %esp 0xe8 %ebp
29
39. addl %ebx,%edx # Count--
irmovl $-1,%ebx # addl %ebx,%edx # Count-- PC 0x73 ZF SF OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp
30
40. jne Loop # Stop when 0 PC 0x78 ZF SF OF 1 address stack 0x100 0xfc
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp
31
41. End: rrmovl %ebp,%esp PC 0x7a ZF SF OF 1 address stack 0x100 0xfc
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp
32
42. popl %ebp %ebp %esp %esp %esp %ebp %ebp PC 0x7c ZF SF OF 1 address
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xec %ebp 0xf8 %ebp %esp %esp %esp %ebp %ebp
33
43. ret %ebp %esp %esp %esp PC ZF SF OF 1 0x7c address stack 0x100
0x7c address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xec %ebp 0xf8 %ebp %esp %esp %esp 0x3d 0x3d
34
22. rrmovl %ebp,%esp %esp %esp %esp PC 0x3f ZF SF OF 1 address stack
rrmovl %ebp,%esp address stack 0x100 0xfc 0x11 0xf8 %ebp 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xf8 %ebp %esp %esp %esp
35
23. popl %ebp %ebp %esp %esp %esp %ebp %ebp PC 0x41 ZF SF OF 1 address
address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xfc %ebp 0x100 %ebp %esp %esp %esp %ebp %ebp
36
24. ret %esp %esp %esp PC ZF SF OF 1 0x41 address stack 0x100 %ebp
0x41 address stack 0x100 %ebp 0xfc 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0x100 %ebp %esp %esp %esp 0x11 0x11 0x11
37
6. halt PC ZF SF OF 1 address stack 0x100 %esp, %ebp 0xfc 0x11 0xf8
address stack 0x100 %esp, %ebp 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0x100 %ebp
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.