




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This homework is an ungraded assignment designed to familiarize you with translating C code to MIPS. We will release solutions on Sunday, Feb 22nd, ...
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!
This homework is an ungraded assignment designed to familiarize you with translating C code to MIPS. We will release solutions on Sunday, Feb 22nd, so that you may use them to study for the exam.
In this section, weโll take the same problem (that of printing a string) and approach it using different C constructs. This should allow you to see how various C constructs are translated into MIPS. Suppose that we have a print function, but that this function only takes one character and prints it to the screen. It expects the character to be in the lower 8 bits of $a0.
A) Translate into MIPS, while preserving the while loop structure:
void string_print(char print_me) { while (print_me != โ\0โ) { print(*print_me); print_me++; } }
Solution:
s t r i n g p r i n t :
addiu $sp , $sp , โ 8 sw $ra , 0 ( $sp ) sw $s0 , 4 ( $sp )
addiu $s0 , $a0 , 0 # copy a0 t o s0 so we don โ t have t o back i t up l bu $a0 , 0 ( $s0 ) # l o a d c h a r a c t e r f o r f i r s t i t e r a t i o n
Loop : beq $a0 , $0 , Ret # break out o f l o o p i f l o a d e d c h a r a c t e r i s n u l l t e r m i n a t o r j a l p r i n t # c a l l p r i n t ( t h i s i s why we l o a d e d t o a0 )
addiu $s0 , $s0 , 1 # i n c r e m e n t p o i n t e r l bu $a0 , 0 ( $s0 ) # l o a d next c h a r a c t e r j Loop
Ret :
lw $s0 , 4 ( $sp ) lw $ra , 0 ( $sp ) addiu $sp , $sp , 8 j r $ra
B) Translate into MIPS, while preserving the for loop structure (your function is given the string length):
void string_print(char print_me, int slen) { for (int i = 0; i < slen; i++) { print((print_me+i)); } }
Solution:
s t r i n g p r i n t :
addiu $sp , $sp , โ 16 sw $ra , 0 ( $sp ) sw $s0 , 4 ( $sp ) sw $s1 , 8 ( $sp ) sw $s2 , 1 2 ( $sp )
addiu $s0 , $a0 , 0 # copy a0 t o s0 so we don โ t have t o back i t up addiu $s1 , $a1 , 0 # copy a1 t o s1 so we don โ t have t o back i t up addiu $s2 , $0 , 0 # i n i t i a l i z e l o o p c o u n t e r
Loop : beq $s2 , $s1 , Ret # break out o f l o o p i f i == s l e n addu $a0 , $s2 , $s l b u $a0 , 0 ( $a0 ) # g e t f i r s t char j a l p r i n t # c a l l p r i n t ( t h i s i s why we l o a d e d t o a0 ) addiu $s2 , $s2 , 1 # i n c r e m e n t l o o p var j Loop
Ret :
j r $ra
Convert the following recursive implementation of Fibonacci to MIPS. Do not convert it to an iterative solution.
int fib(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } return fib(n-1) + fib(n-2); }
Solution:
f i b : addiu $sp , $sp , โ 12 sw $ra , 0 ( $sp ) #backup r a f o r r e c u r s i v e c a l l s sw $a0 , 4 ( $sp ) #backup a0 f o r r e c u r s i v e c a l l s sw $s0 , 8 ( $sp ) #backup s0 s i n c e we use i t
beq $a0 , $0 , ReturnZero addiu $t0 , $0 , 0 s l t i $t0 , $a0 , 2 # you can a l s o beq with one bne $t0 , $0 , ReturnOne
addiu $a0 , $a0 , โ 1 j a l f i b move $s0 , $v lw $a0 , 4 ( $sp ) addiu $a0 , $a0 , โ 2 j a l f i b add $v0 , $v0 , $s
lw $s0 , 8 ( $sp ) lw $ra , 0 ( $sp ) addiu $sp , $sp , 12
j r $ra
ReturnZero :
lw $s0 , 8 ( $sp ) lw $ra , 0 ( $sp ) addiu $sp , $sp , 12 l i $v0 , 0 j r $ra
ReturnOne :
lw $s0 , 8 ( $sp ) lw $ra , 0 ( $sp ) addiu $sp , $sp , 12 l i $v0 , 1 j r $ra
Now, modify your recursive Fibonacci implementation to memoize results. For the sake of simplicity, you can assume that the array given to you (memolist) is at least n elements long for any n. Additionally, the array is initialized to all zeros.
int fib(int n, int* memolist) { if (n == 0) { return 0; } else if (n == 1) { return 1; } if (memolist[n]) { return memolist[n]; } memolist[n] = fib(n-1, memolist) + fib(n-2, memolist); return memolist[n]; }
Solution:
f i b :
addiu $sp , $sp , 12 l i $v0 , 0 j r $ra
ReturnOne :
lw $s0 , 8 ( $sp ) lw $ra , 0 ( $sp ) addiu $sp , $sp , 12 l i $v0 , 1 j r $ra
RetMemo : # r e t u r n s v a l u e a l r e a d y i n v
lw $s0 , 8 ( $sp ) lw $ra , 0 ( $s0 ) addiu $sp , $sp , 12 j r $ra
Write a MIPS function that performs identically to this code when called many times in a row, but does not store the static variable in the static segment (or even the heap or stack):
short nextshort() { static short a = 0; return a++; }
Tips/Hints:
Solution:
n e x t s h o r t : addiu $v0 , $0 , 0 l a $t0 , n e x t s h o r t lw $t1 , 0 ( $t 0 ) addiu $t3 , $0 , 0xFFFF
and $t2 , $t1 , $ t 3 beq $t2 , $t3 , H a n d l e S p e c i a l addiu $t1 , $t1 , 1 sw $t1 , 0 ( $ t0 ) # s t o r e i n c r e m e n t e d i n s t r u c t i o n j r $ra # r e t v a l u e i s a l r e a d y i n v H a n d l e S p e c i a l : # here , handle t h e o v e r f l o w c a s e l a $t6 , b a c k u p i n s t lw $t1 , 0 ( $t 6 ) sw $t1 , 0 ( $ t0 ) j r $ra # r e t v a l u e i s a l r e a d y i n v
b a c k u p i n s t : addiu $v0 , $0 , 0