CS274: Computer Architecture - MIPS Iteration
Activity Goals
The goals of this activity are:- To implement iterative algorithms using the MIPS assembly language
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 iterative MIPS program
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 | . text .globl main main: # print user prompt string li $ v0 , 4 la $ a0 , input syscall guess: # 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 # loop until the user gets it right j guess correct: li $ v0 , 4 la $ a0 , right syscall 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
- What kind of loop do you see depicted in this program?
Model 2: Implementing Loops in MIPS
1 2 3 4 5 6 7 8 9 10 | int main( void ) { int sum = 0; int i = 0; for (i = 0; i < 10; i++) { sum = sum + 1; } printf ( "%d" , sum); } |
1 2 3 4 5 6 7 | int main( void ) { int num = 0; do { printf ( "Enter a number between 1 and 10\n" ); scanf ( " %d" , &num); } while (num < 1 || num > 10); } |
Questions
- Translate each of the above C code listings into a MIPS assembly program