CSE 2021: Computer Organization Assignment # 1: MIPS Programming Due Date: October 25, 2010 Please note that a short quiz based on Assignment 1 will be held in class on October 27, 2010 to assess your performance. It is not required that you submit your solution but you should complete the assignment in preparation for the quiz. Problems 1 to 3 are fairly trivial programs and the challenge is to complete each of them in 15 to 30 minutes or so in anticipation of the preparation for the coming midterm exam. Likewise, the theoretical questions (Problem 4) in the attached mid term exam should not, collectively, take more than 90 minutes of your time. Good luck! Problem 1: Write a program that reads a valid date (3 integers being date (d), month (m), year (y)) and determines the day of the week (Sun, Mon, Tue, etc.) using the Zeller's algorithm described below: if (m <= 2){ m = m + 12; y = y - 1; } tmp1 = (26 * (m + 1)) / 10; tmp2 = (125 * y) / 100; tmp3 = d + tmp1 + tmp2 (y / 100) + (y / 400) - 1; dow = tmp3 % 7; The resulting integer in dow (day-of-week) denotes the day. If dow equals 0, then the day is Sunday. If dow equals 1 then the day is Monday, and so on for the rest of the days in a week. Your program should output the day in words depending on the value of dow. Hint: Recall that integer division results in only the quotient as the output with the remainder being ignored such that 190/100 = 180/100 = 101/100 = 1. Problem 2: Write a MIPS assembly program that accepts the temperature in Celsius as the input and converts the input reading from Celsius to Fahrenheit. Since the answer can be a real number so use the instructions of the floatingpoint coprocessor to compute your answer. The formula to convert Celsius to Fahrenheit is given by and the program should keep running until any character is entered. Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.
Group-A For each question, write the contents of the indicated register pair (in the specified radix) after the fragment executes. For decimal radix, state the content as a signed integer. If an answer cannot be determined or the fragment has errors, write a brief explanation. A.1 addi $t1, $0, 25 addi $t2, $0, 17 div $t1, $t2 mflo $t3 A.2 addi $t1, $0, 25 addi $t2, $0, 3 bgtz $t1, here addi $t2, $0, 17 here: addi $t3, $0, 8 A.3 addi $t3, $0, 5 addi $t2, $0, 3 here: add $t3, $t3, $t3 addi $t2, $t2, -1 bne $t2, $0, here CSE2021/HR MIDTERM EXERCISES PAGE 1
A.4 main: lui $t2, 0x1 ori $t3, $t2, 0xff $t2 (in hex) = $t3 (in hex) = A.5 addi $t1, $0, 25 addi $t2, $0, 3 sll $t2, $t2, 3 and $t3, $t1, $t2 A.6.data a:.asciiz "2021" x:.byte 7,2,12.text main: addi $t2, $0, 1 lb $t3, a($t2) A.7.data a:.asciiz "2021" x:.byte 7,2,12.text main: la $t1, x lb $t2, 0($t1) lb $t3, 2($t1) CSE2021/HR MIDTERM EXERCISES PAGE 2
A.8.data a:.word 12,-5,4,0 x:.byte 5.text main: addi $t1, $0, 8 lw $t2, a($0) lw $t3, a($t1) A.9.data a:.word 12,-5,4,0 x:.byte 5, 1, -5, -1.text main: addi $t2, $0, 16 lb $t3, a($t2) A.10 addi $t1, $0, 5 sll $t2, $t1, 29 sra $t3, $t2, 29 $t2 (in hex) = $t3 (in hex) = CSE2021/HR MIDTERM EXERCISES PAGE 3
Group-B Unless stated otherwise, assume the machine is little endian, the integers in question are signed, and all operations result in values that fit in 32 bits. B.1 Write a minimal sequence of MIPS instructions that accomplishes the following: if ($t1 < $t2) { $s0 = $t2; } else { $s0 = $t1 } B.2 Write a minimal sequence of MIPS instructions that accomplishes the following: a[15] = a[14] - 15; where a is an array of words already declared and initialized in.data. CSE2021/HR MIDTERM EXERCISES PAGE 4
B.3 Write the MIPS instruction whose machine language encoding is: 0000 0011 0001 1001 0100 0000 0010 1010 Your answer must use register names (like $t0) not numbers; and must specify any immediate as a signed integer in decimal. B.4 Write a minimal sequence of MIPS instructions that accomplishes the following: while (a[$t1] == $s0) { $t1 = $t1 + $t2 } where a is an array of words already declared and initialized in.data. CSE2021/HR MIDTERM EXERCISES PAGE 5
B.5 Write a sequence of MIPS instructions to branch to address 0xFFFFFFFC if $t1 is equal to $t2. Recall that the destination of j or any branch instruction must be a label, not a constant B.6 Write a minimal sequence of MIPS instructions that accomplishes the following: $t1 = 0xABCDE; CSE2021/HR MIDTERM EXERCISES PAGE 6
B.7 Write the machine language encoding of the beq instruction in the following fragment. Express your answer in binary. addi $t0, $t3, 143 sw $s0, 165($t1) beq $s4, $sp, skip sll $s0, $s0, 4 skip: sub $t0, $t3, $s0 B.8 Write a minimal sequence of MIPS instructions that accomplishes the following: $t3 = 35 * ($t1 / $t2) + 14 You can assume $t2 is not zero and that all integers are signed and small enough that the results of all operations do fit in 32 bits. CSE2021/HR MIDTERM EXERCISES PAGE 7
B.9 Write a minimal sequence of MIPS instructions that makes the least-significant bit in $a0 the same as its most-significant bit without changing any other bit in it. B.10 Write a minimal sequence of MIPS instructions that sets $v0 to the number of characters in a null-terminated string whose starting address is in $a0. Do not count the null as part of the string. CSE2021/HR MIDTERM EXERCISES PAGE 8
B.11. Write the MIPS method that receives in $a0 a positive integer X and returns in $v0 the largest integer M that satisfies the following: 1 + 4 + 9 + 16 +... + M 2 <= X In other words, M is the largest integer such that the sum of the squares of the first M integers is not more than X. For example, if X=79 then M would be 5 because the sum 1+4+9+16+25 is 55, and had we added one more term (36), the sum (91) would have exceeded X. You can assume that the integer in $a0 is positive, and that all integers are signed and small enough that the individual squares and the final sum do fit in 32 bits. B.12. Write a MIPS program that reads an integer from the user and outputs 1 if the integer is magical and outputs 0 otherwise. To determine if the integer is magical we will invoke a method called "ismagical" from the main program by writing "jal ismagical". We do not know what makes the integer magical, in other words we have not inspected the method "ismagical" although it has already been written and its code will be placed at the end of our MIPS code as shown on the next page. However, the communication between the main program and the method is known to us and will be done through the stack, not via registers as in most of our examples in class. When the method receives control, that is, at the instant we jump to the method from the main program, the stack will have three words on it: the first contains the address of a word in memory in which the user-entered integer is stored, the second contains the address in memory of a byte where the answer (0 or 1) should be stored, and the third contains the return address (the method will return to it when it ends, not to $ra). As an example let us assume that the stack pointer ($sp) is equal to 400 when the method receives control, and that the stack content is as shown in the diagram to the right. In that case the address of the user-entered integer is 800, the method should store the answer in the byte at address 600, and when the method ends it should jump to the instruction at address 12000. 396 400 12000 404 600 408 800 Write the main program on the next page. The main program should be general and not depend on any specific example numbers. CSE2021/HR MIDTERM EXERCISES PAGE 9
B.12, continued..data.text main: addi $v0, $0, 5 syscall # read an int into $v0 #----------------------------------------------- # if $v0 is magical set $s0 = 1 else set $s0 = 0 #----------------------------------------------- #----------------------------------------------- add $a0, $s0, $0 addi $v0, $0, 1 syscall # print s0 jr $ra # end the program ismagical: #----------------------------------------------- already written.. CSE2021/HR MIDTERM EXERCISES PAGE 10
Group-C C.1 Why does MIPS not have a subtract immediate instruction? C.2 Recall that according to the IEEE-754 standard a single precision real number is represented in one word using 1+8+23 bits. How many such numbers are there between 2.0 and 3.0, and how many are between 1000002.0 and 1000003.0? Approximate answers are acceptable but you must show your work. C.3 Suppose that a company conducts surveys whose answers always have three possibilities, yes, no, and do not know. The company wants to store these surveys in a new kind of memory in which the smallest unit (similar to a bit) is known as a trit and can be 0, 1 or 2. How many trits are required to hold a 6 bit number? Give an expression for the number of trits needed to hold n bits. C.4 What are pseudo instructions and what is their purpose? Give examples. C.5 MIPS has added a shift-right-arithmetic instruction so that signed two's complement numbers can be divided by two whenever shifted to the right. Why is there no corresponding shift-left-arithmetic instruction? CSE2021/HR MIDTERM EXERCISES PAGE 11