The fundamental purpose of a CPU is to perform computations on data. The way we interpret this data allows us to listen to music, draw images, watch movies, and play games. But, at the low level, we are still just doing computations. Understanding how a processor performs computations is necessary to understanding how these high-level tasks are achieved at a low level.
In today's class you will:
ssh username@hopper.slu.edu
wget http://cs.slu.edu/~dferry/courses/csci2400/asm/arithmetic.s
print_eax
print_ebx
print_ecx
print_edx
print_all
You should be able to call these functions from any point in your program in order to print out the values of the respective registers. Try this now- use the move instruction to place some values in each register and print the values out.
addl
instruction and store the result in
ECX. Print out the value of ECX to verify your result.
subl
instruction and store the result in
ECX. Print out the value of ECX to verify your result.
imull
instruction and store the result in
ECX. Print out the value of ECX to verify your result.
int function ( int a, int b, int c );
Notice that this function takes three arguments and returns one argument. For this exercise we will implement just that interface. According to the x86 calling convention, the calling function is responsible for pushing the arguments onto the stack from right to left. Then it calls the call-ee function, which pushes the previous instruction pointer (%eip) and the previous stack base (%ebp) pointer. Thus, assuming each argument is 4-bytes wide, the call-ee function can find the leftmost argument at EBP+8, the second argument at EBP+12, and the third argument at EBP+16.
For a refresher on all of the above, see this website under the
section "Calling a __cdecl
function" but be warned that this site uses the Intel syntax. Remember that the stack starts
at high memory and grows downward (to lower memory addresses).
For your new function, first copy the preamble to the main procedure into your function:
pushl %ebp
movl %esp, %ebp
Then copy the procedure exit code:
leave
ret
Finally, copy argument A into EAX, argument B into EBX, and argument C into ECX. Print out the values in your function to verify you have done this
correctly. We will return the result of this function through the register EAX, so move the value zero into EAX at the end of your function (before
leave
).
int function( int a, int b, int c){
temp d;
a = a + 2*b;
d = b * c;
c = c*c;
return a + b + c + d;
}
If you call this function with the values A=1, B=5, and C=10 you will get the return value of 166. Call your function with the values A=4, B=8, and C=16. What value is returned then?