In: Computer Science
Show the machine code corresponding to the Y86 version of the following code. Assume that the code starts at location 0x050. For each statement, list address: code bytes.
int sumInts ( long int n) { /* Add the integers from 1..n. */ long int i; long int sum = 0; for ( i = 1; i <= n; i++ ) { sum += i; } return sum; }
Assembly code :
sumInts:
1 irmovl $0,eax
# sum = 0
2 irmovl $1,ebx
# i = 1
Loop:
3 rrmovl %esp,%edx
# %esp = n; counter(%edx) = n ; to compare with
i for looping
4 addl %ebx,%eax
# sum = sum + i
5 irmovl $1,%ecx
6 addl %ecx,%ebx
# i = i+1
7 subl %ebx,%edx
# counter = counter - i ; it
becomes negative when i = n+1
8 jl Loop
# if counter >= 0, goto Loop
9 ret
# return when counter < 0
Corresponding machine code by line numbers as mentioned above
assuming code starts at 0x050 :
1 0x050 : 30F000000000
2 0x056 : 30F100000001
3 0x05C : 2042
4 0x05F : 6010
5 0x061 : 30F200000001
6 0x067 : 6021
7 0x069 : 6113
8 0x06B : 720000005C
9 0x070 : 90