CS274: Computer Architecture - MIPS Conditionals
Activity Goals
The goals of this activity are:- To implement
if
andelse
statements using MIPS assembly.
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: An if
/else
statement in MIPS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | . text .globl main main: # print user prompt string li $ v0 , 4 la $ a0 , input syscall # read integer - will be copied to v0 li $ v0 , 5 syscall # our secret number is 7! li $ s0 , 7 # if v0 == our secret number s0 beq $ s0 , $ v0 , correct # incorrect code li $ v0 , 4 la $ a0 , wrong syscall # but skip the correct code! j exit correct: li $ v0 , 4 la $ a0 , right syscall # What would you add here to print "The secret number was: " followed by the integer 7, followed by a newline? exit: # exit li $ v0 , 10 syscall . data input: . asciiz "Can you guess my secret number between 1 and 10? Enter it below\n" wrong: . asciiz "Not quite!\n" right: . asciiz "You guessed it!\n" |
Questions
- Translate the above code into a C, Java, or Python program. What does it do?
- What would happen if the
j exit
line was omitted? - How would you modify this program to print the secret number using a syscall?
- Translate the
beq
instruction to machine language. - Translate the
j
instruction to machine language. - What is the farthest you can move the program counter with a branch instruction? How about a jump instruction?
- Suppose you wanted to jump to a location that caused the upper four bits of the program counter to be modified. Using a combination of branch and jump instructions, perform this jump.
Model 2: Implementing MIPS Conditionals
1 2 3 4 5 6 7 8 9 10 11 | int main( void ) { int grade = 93; if (grade > 90) { printf ( "A\n" ); } else if (grade > 80) { printf ( "B\n" ); } else { printf ( "C or lower\n" ); } } |
Questions
- Translate the above C code into a MIPS assembly program