CSCI 2400: Assembly Class 3

Arithmetic


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:

  1. Use the various arithmetic features of assembly language
  2. Translate C-level functions into assembly language

Exercises

  1. Login to hopper.slu.edu- remember you can login with:

    ssh username@hopper.slu.edu

  2. Now, download a new program for today's class:

    wget http://cs.slu.edu/~dferry/courses/csci2400/asm/arithmetic.s

  3. Take a look at this program in the editor of your choice- you'll see that I've added five functions:

    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.

  4. Set EAX to 100 and EBX to 5. Compute their sum with the addl instruction and store the result in ECX. Print out the value of ECX to verify your result.

  5. Set EAX to 100 and EBX to 5. Compute their difference with the subl instruction and store the result in ECX. Print out the value of ECX to verify your result.

  6. Set EAX to 100 and EBX to 5. Compute their product with the imull instruction and store the result in ECX. Print out the value of ECX to verify your result.

  7. Set EAX to 40. Compute EAX multiplied by four using left shifts. Put the result in EBX and print it out.

  8. Set EAX to -500. Compute EAX divided by four by using right shifts. Put the result in EBX and print it out.

  9. Set EAX to 1000. Compute EAX multiplied by 49 using only moves, shifts, and adds. Put the result in EBX and print it out. Hint: start by re-writing 49 into a few different terms you can easily achive with shifts and adds.

  10. Finally, we will write an assembly procedure to compute the C code in the following exercise. To do so we want to use the proper stack-based calling convention. This C function will have the signature:

    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).

  11. Once you are satisfied that your arguments are passing correctly, convert the following function into assembly. Place the return value in the register EAX.

    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?

  12. Email your program to the instructor when you are done. Include your answers to each question in your email or in a text file and include them with your submission. These will count for credit as a homework assignment.