Homework #1 Solution



Your submission should be comprised of two files: a .pdf file containing written answers to the questions and one .c file containing C code. You do not need to submit your assembly code. Legible, handwritten solutions are acceptable for undergraduate students (472). The handwritten solutions must be electronically scanned and submitted as a PDF file. Graduate students (572) must compose their solutions in MS Word, LaTeX, or some other word processor and submit the results as a PDF file.

  1. (8 pts) Assume that the signed numbers in this question utilize the two’s complement convention.

    1. What is the 12 bit binary representation of -142 (decimal)?

    1. Perform the following equation in 12 bit binary (show your work): 119 + (-142)

    1. What is the smallest number of bits that can correctly represent the signed binary number corresponding to 56841 (decimal)?

    1. What is the unsigned hexadecimal representation of 56841?

  1. (7 pts) Using the 32 bit IEEE 754 standard:

    1. What is the binary representation of 572.375?

    1. What is the most-negative floating point number that can be represented in 32 bit IEEE 754 format (ignoring negative infinity)?

    1. Show the binary representation of the number in part (2b) above.

  1. (4 pts) Assuming the decimal number: 472.2

    1. Why is it not possible to 100% accurately represent the number 472.2 in 32 bit IEEE 754 format?

    1. Can this number be represented with 100% accuracy in IEEE 754 double precision format (64 bits)?

  1. (4 pts) For these questions, assume that the initial register values are as follows:

$t1 = 0x0004, $s0 = 0x01A7, $a1 = 0x6B20, $sp = 0x8034

Now assume that the following MIPS code is executed:


$sp, $sp, -4


$s0, 0($sp)


$s0, $zero, $zero


$t1, $s0, $a1

After execution, what are the values of $t1, $s0, $a1, and $sp?

9/27/18 Clarification: provide each value in hexadecimal.

  1. Note: For this problem you must connect to flip.engr.oregonstate.edu using an SSH client. For help, see the links:

Windows Instructions Mac/Linux Instructions

A properly submitted, correctly operational C file is worth 20 pts.

  1. Create a file named prime_number.c and insert the starter code below. You will then implement an algorithm (of your choosing) in C to determine whether a provided number is prime.


Short program to determine whether a provided positive number is prime.


int main() {

  • do not change this section

  • you MUST retain the following two variable names: int num = 59;

int is_prime;

  • your code goes in between the following markers

  • <————->

  • <————->

  • should return 0 if “num” was not prime

  • should return 1 if “num” was prime

return is_prime;


  1. Compile your code and ensure that the algorithm works by executing the following commands at the terminal prompt:

# compile the code

gcc -std=c11 ./prime_number.c

      • print the return value and see if it is 1 or 0 a.out; echo $?

  1. Try changing the value of num (and recompiling the code) to make sure that your program properly detects prime numbers.

  1. Once your program is working properly, run the following command to generate the x86 assembly code. The output will be stored in a file named prime_number_x86.s

    • generate x86 assembly code

gcc -fno-asynchronous-unwind-tables -std=c11 -Og -S -o ./prime_number_x86.s ./prime_number.c

  1. (3 pts) x86 assembly uses 8 32-bit registers. They are: eax, ebx, ecx, edx, esi, edi, ebp, and esp. Which of these registers are used in the assembly code generated in step (5d)?

  1. Run the following command to generate MIPS assembly code. The output will stored in a file named prime_number_mips.s

# generate MIPS assembly code

mips64-linux-gnu-gcc -fno-asynchronous-unwind-tables -std=c11 -Og -S -o ./prime_number_mips.s ./prime_number.c

  1. (4 pts) As noted in the textbook, MIPS has 32 32-bit registers (see figure 2.1 in the textbook). Which of these registers are used in the assembly code generated in step (5f)?