I'm trying to convert a C Code to MIPS code.
int main() {
int a;
int b;
int result = 0;
if (a != b)
result = test(a, b);
else
result = a + b;
return result;
}
int test(int a, int b) {
if (a > b)
return multiply(a, b)
else
return subtract(a, b)
}
int multiply(int a, int b) {
return a * b;
}
int subtract(int a, int b) {
return a - b;
}
This code contains nested function calls inside test function.
I have put the return address of test function into stack and trying to return the subtracted or multiplied value to the main.
But in my case, my code executes both subtract and multiply functions.
I'm trying to put my result to s0. After running s0 always shows subtraction of values. If I put multiply result to s1, s1 shows true value.
I think subtract function overrides value at s0. But when case is multiply, why subtract method is called? I have an if/else block, but this part seems to be not working.
Here is my MIPS Code, what am I doing wrong?
.data
numberA: .word 4
numberB: .word 2
.text
.globl main
main:
addi $s0, $0, 0 # result = 0
lw $a0, numberA
lw $a1, numberB
bne $a0, $a1, L1
add $s0, $a0, $a1
L1: jal test
add $s0, $v0, $0
li $v0,10
syscall
test:
addi $sp, $sp, -4
sw $ra, 0($sp)
slt $s1,$a1,$a0
bne $s1, 1, ELSE
jal multiply
ELSE: jal subtract
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra
subtract:
sub $s0, $a0, $a1
jr $ra
multiply:
mult $a0, $a1
mflo $s0
jr $ra
The problem is, that after you return from multiply, you still call subtract in the next line. You have to return from the function test after calling multiply.
However, since the function calls are both the last instruction of your function, you can use the following shortcut:
test:
slt $s1,$a1,$a0
bne $s1, 1, ELSE
j multiply
ELSE: j subtract
This way, you do not have to store the $ra in your stack, but you can directly use it to jump back to the caller of test in the jr $ra of subtract and multiply. This way it should work as intended.
Alternatively, skip over jal subtract after returning from multiply:
jal multiply
j OUT
ELSE: jal subtract
OUT: ...
Related
Hi I need help in coding in MIPS. I have to find the Floor Square root of the n input. I have the C version of the code..
uint32_t isqrt(uint32_t n) {
if(n<2) return n;
uint32_t s = isqrt(n >> 2) << 1;
uint32_t l = s + 1;
if (l * l > n)
return s;
else
return l;
}
apparently it recurses itself..
Right now my function looks something like. Im not really sure what I am doing wrong
#isqrt
isqrt:
#prologue
subu $sp, $sp, 12
sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
#if(n<2) n Branch if Greater Than 2
blt $a0, 2, lt2 #if(n<2) return n;
#else uint32_t small = isqrt(n >> 2) << 1;
srl $s0, $a0, 2 # small = isqrt(n >> 2)
jal isqrt
sll $s0, $s0, 1 # then << 1
add $s1, $s0, 1 # large = small + 1
li $s3, 0
mul $s3, $s1, $s1 # large = large * large\
#if large * large > n return small else return large
bgt $s3, $s0, small # if l * l > n return small
move $v0, $t1 # else return large
lt2:
move $v0, $a0;
j end
small:
move $v0, $s0
j end
end:
lw $ra, 8($sp)
lw $s0, 4($sp)
lw $s1, 0($sp)
addi $sp, $sp, 12
jr $ra
Good effort, but many, many, many errors. Suggest trying a more methodological approach. Check your choices of registers, you register numbers, and prologue/epilogue code for proper handling of the various register kinds.
That code is doing if (n>=2) return n; — the exact opposite of the C code.
The called function isqrt expects a formal parameter in $a0, but that code is passing the actual argument in $t0, so there's a mismatch.
The called function isqrt provides the return value in $v0, but when calling that recursively, expects it to have been provided in $t0, another mismatch.
The function uses $s0, but fails to ensure its value is call-preserved as it should have been.
The following code:
li $t0, 2 # small = isqrt(n >> 2) << 1
srl $t0, $t0, 2
generates 2>>2, which is clearly different from the n>>2 in the C code.
The following code:
equal:
move $v0, $t1
j end
is doing return l*l; where the C code wants return l;
I'd suggest starting with an analysis of what registers to use for what variables — this analysis requires examining liveness across the (recursive) function call. Start that analysis on the C version initially. When you're finished with that, you'll have a mapping of register numbers for all the C variables. Then write prologue/epilogue that handle those registers as required. Then translate the C code line by line into assembly paying careful attention to what variables are in what registers, and what expressions you're trying to recreate in assembly.
I'm implementing the following code from c to mips assembly
/*
Given an array arr terminated by entry -1, transform the array by adding
to each entry the sum of the remainder of the array after that entry.
That is, if initially arr == [a_0, a_1, ..., a_n, -1], then finally
arr == [b_0, b_1, ..., b_n, -1], where b_j = sum(a_i for i in j..n-1).
*/
uint32_t reverse_prefix_sum(uint32_t *arr) {
uint32_t r;
if(*arr == -1) return 0;
r = reverse_prefix_sum(arr+1) + (uint32_t)*arr;
*arr = r; /* may discard MSB */
return(r);
}
My current program is the following:
reverse_prefix_sum: # Let the address of array arr be in $a0
# adjust the stack pointer to save $a0 and $ra
addi $sp,$sp,-8
sw $a0,0($sp)
sw $ra,4($sp)
# Load *arr
lw $t0,0($a0)
beq $t0,-1,exit1 # r is in $v0
# *arr = *arr + 1
addu $a0,$a0,4 # recursive call
jal reverse_prefix_sum
addu $v0,$v0,$t0
# restore $a0
lw $a0,0($sp)
#restore $ra
lw $ra,4($sp)
add $sp,$sp,8
sw $v0,0($a0)
jr $ra
exit1:
# restore $a0
lw $a0,0($sp)
#restore $ra
lw $ra,4($sp)
addi $sp,$sp,8
# return 0
li $v0,0
jr $ra
With input 1 2 3 4 -1, I'm getting output -4 -3 -2.
I'm wondering where this could be improved to fix the output being incorrect.
The problem is that t0 isn’t preserved across the function call. Reload it from 0(a0) after restoring a0 after the function returns.
I am trying to call a function declared in a C file from an assembly file, but I am getting a "Illegal Instruction" error.
My C code:
#include <stdio.h>
int BubbleSort(int *v){
return 13;
}
int Run(int *, int *);
int main(){
int vetor[] = {1, 3, 5, 4, 10}, numTrocas = 0, res = 0;
numTrocas = Run(vetor, &res);
printf("numTrocas= %d\nf= %d\n", numTrocas, res);
return 0;
}
My Assembly code:
.data
.text
.globl Run
Run:
addi $sp,$sp,-8
sw $ra,0($sp)
sw $a0,4($sp)
move $t4, $a1 #$t4 = $a1; address of variable res in C file
move $t6, $a0 #$t6 = address of the first vector element
lw $a0, ($t6)
add $t6, $t6, 4
lw $a1, ($t6)
add $t6, $t6, 4
lw $a2, ($t6)
add $t6, $t6, 4
lw $a3, ($t6)
add $t6, $t6, 4
lw $t3, ($t6)
jal PolyCalc
lw $a0,4($sp)
jal BubbleSort #-> Illegal Instruction
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra
PolyCalc: #This function calculates the result of the expression 5(V[0] + V[1])(V[2] − 3xV[3]xV[4])
li $s0,5 #$s0 = 5
li $s1,3 #$s1 = 3
add $t1,$a0,$a1 #t1=(x1+x2)
mul $t1,$t1,$s0 #t1=5*(x1+x2)
mul $t2,$a3,$t3 #$t2 = x4*x5
mul $t2,$t2,$s1 #$t2 = 3*x4*x5
sub $t2,$a2,$t2 #$t2 = (x3-3x4*x5)
mul $t1,$t1,$t2
sw $t1, ($t4) #Save the result on the address of $t4
jr $ra
When I comment the line jal BubbleSort and add a random value to $v0 as a return of the BubbleSort function I stop getting that error and the program works fine.
Can someone find the error in my code?
Thanks
For good measure, in your asm file, you should probably add:
.extern BubbleSort
Then, the jal BubbleSort should get relocated correctly.
But, the linker may place it too far away for the [limited] range of jal, so you may have to replace it with:
la $t0,BubbleSort
jalr $t0
The error you are experiencing is because the assembly code is not able to find the label called BubbleSort to jump. I think you should be using a different approach. You can embed assembly code in C using the asm function.
Something like:
#include <stdio.h>
int BubbleSort(int *v){
asm("lw $a0, ($t6)"
"lw $a1, ($t6)");
}
int Run(int *, int *);
int main(){
int vetor[] = {1, 3, 5, 4, 10}, numTrocas = 0, res = 0;
numTrocas = Run(vetor, &res);
printf("numTrocas= %d\nf= %d\n", numTrocas, res);
return 0;
}
You can find more information about this function in the GCC documentation: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
I was trying to convert this piece of code into MIPS instruction. Lets say that a is in $a0, b is in $a1, n is in $a2, the result is in $v0, and to end the
program, call “jr $ra” to return to the subroutine caller
int fib_iter(int a, int b, int n) {
if (n == 0)
return b;
else
return fib_iter(a+b, a, n-1);
For the simplicity, we just ignore the stack frame for this one
And this is the MIPS code I converted:
bne $a1, $zero, ELISEIF // if b != 0 go to ELSEIF
lw $v0, $0($a1) // load b to result if n == 0
j DONE // done
ELSEIF:
lw $at, $0($a0) // temp = a
add $a0, $a0, $a1 // a = a + b
add $a1, $zero, $zero // clear b
lw $a1, $0($at) // b = a
sub $a2, $a2, $1 // n = n - 1
jr $ra // call the subroutine caller
Done:
what to put??
Please point out my errors(there might be a a lot since I am new to this)
Thanks for your time for helping me and I appreciate that
lw $v0 $0($a1) will do $v0 = $a1[0] instead of $v0 = $a1. To do the latter, use mv $v0 $a1.
Also $at is reserved for pseudoinstructions in MIPS. I means they get modified by pseudoinstructions. So, do not use it unless you are sure that you have not used any pseudoinstruction. $t1 to $t7 are temporary registers. Use any one of them.
Here is the correct code
FIB:
bne $a2, $zero, ELSE // if n != 0 go to ELSE
mv $v0, $a1 // load b to result if n == 0
jr $ra // end of recursion, so call the subroutine caller
ELSE:
mv $t0, $a0 // temp = a
add $a0, $a0, $a1 // a = a + b
mv $a1, $t0 // b = a
addi $a2, $a2, -1 // n = n - 1
j FIB // call FIB recursively
I'm converting the following recursive java program to MIPS asm. The algorithm computes all the possible ordering/permutations of the numbers. But the recursive call is in the for loop. I need to preserve the variable 'i' in my MIPS version but I don't know exactly where to add that. My algorithm is correct, it's just that my $t0 (which is 'i') never gets reset to 0. I just can't figure out how/where to preserve it on the stack or when to take it off the stack. Any help appreciated.
import java.util.Arrays;
public class Test
{
private static void swap(int[] v, int i, int j)
{
int t = v[i];
v[i] = v[j];
v[j] = t;
}
public void permute(int[] v, int n)
{
if (n == 1)
System.out.println(Arrays.toString(v));
else
{
for (int i = 0; i < n; i++)
{
permute(v, n-1);
if (n % 2 == 1)
swap(v, 0, n-1);
else
swap(v, i, n-1);
}
}
}
public static void main(String[] args)
{
int[] ns = {1, 2, 3, 4};
new Test().permute(ns, ns.length);
}
}
and the mips function
Note: I am permutating Strings, not integers but the algorithm is the same.
#----------------------------------------------
# anagram - Prints all the permutations of
# the given word
# a0 - the word to compute the anagrams
# s0 - n, the length of the word
# a1 - n - 1 (length-1)
#----------------------------------------------
anagram:
addi $sp, $sp, -16
sw $a0, 0($sp)
sw $a1, 4($sp)
sw $s0, 8($sp)
sw $ra, 12($sp)
add $s0, $a1, $zero # this is n
addi $a1, $s0, -1 # n-1
beq $s0, 1, printS
init: move $t0, $zero # t0 = i = 0
logic: slt $t1, $t0, $s0 # Set t1 = 1 if t0 < length
beqz $t1, endAnagram # if it's zero, it's the end of the loop
jal anagram
li $t2, 2
div $s0, $t2
mfhi $t3
beqz $t3, even # if even branch to even, otherwise it will go to odd
odd: # swap the n-1 char with the first
add $t4, $a0, $zero
add $t5, $a0, $a1
lb $t6, 0($t4) # first char
lb $t7, 0($t5) # n-1 char
sb $t7, 0($t4) # swap the two
sb $t6, 0($t5)
j inc # skip the even section
even: # swap the ith char with n-1 char
add $t4, $a0, $t0 # ith char
add $t5, $a0, $a1 # n-1 char
lb $t6, 0($t4) # ith char
lb $t7, 0($t5) # n-1 char
sb $t7, 0($t4) # swap the two
sb $t6, 0($t5)
inc: addi $t0, $t0, 1 # t0++;
j logic
endAnagram:
# reset stack pointers
lw $a0, 0($sp)
lw $a1, 4($sp)
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16 # adjust stack
jr $ra
printS: # print string and jump to return
jal printString # calls printString function which prints the string
j endAnagram
$t0 is not preserved accross subroutine calls according to convention, and you seem to follow that convention. As such, you have two choices:
you either store i in a register that is preserved, in which case
you need to preserve the register yourself in the prologue/epilogue. You already do this for $s0.
or you save $t0 yourself on the stack, around the subroutine call
In both cases, you will need additional space for your locals, so change addi $sp, $sp, -16 to addi $sp, $sp, -20 (along with the matching code in the epilogue too). If you choose option #1, use for example $s1 to store i. Add code to save and restore $s1 just like you do for $s0. If you choose option #2, add code around the jal anagram that writes $t0 to stack before the jal, and reloads it after.