I have the following C Code that I have translated into MIPS.
// TEST 1, comment out when running with TEST2
int sx[5] = {1, 2, 3, 4, 5};
int sy[5] = {5, 4, 3, 2, 1};
// TEST 2, uncomment when running with TEST2
//int sx[5] = {1, 2, 3, 4, 5};
//int sy[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
void g(int sa, int incx, int incy)
{
int i, ix, iy;
if (incx == 1 && incy == 1) {
for (i=0; i<5; i++)
sy[i] = sy[i] + sa*sx[i];
}
else {
ix = 0, iy = 0;
for (i=0; i<5; i++) {
sy[iy] = sy[iy] + sa*sx[ix];
ix = ix + incx;
iy = iy + incy;
}
}
}
main()
{
g(2, 1, 1); // TEST1
//g(2, 1, 2); // TEST2
}
Here is the following MIPS code. Note that the output is correct when compared to output of C code. But were calling conventions used correctly? For academic plagiarism purposes, I am leaving out most of the arithmetic instructions that is done inside the if-then / for-loops.
I also would like to know whether the .end g
at the end of my translation is in the right spot. I lost track of it and now I have no clue where it should go (I assume after the nop
in g?)
.global main
.data
## TEST 1
sx: .word 1, 2, 3, 4, 5
sy: .word 5, 4, 3, 2, 1
## TEST 2
#sx: .word 1, 2, 3, 4, 5
#sy: .word 1, 0, 1, 0, 1, 0, 1, 0, 1, 0
.text
.ent main
main:
## TEST 1
li $a0, 2 # int sa
li $a1, 1 # int incx
li $a2, 1 # int incy
## TEST 2
#li $a0, 2 # int sa
#li $a1, 1 # int incx
#li $a2, 2 # int incy
jal g # function call to g
loop:
j loop
nop
.end main
.ent g
## void g(int sa, int incx, int incy)
g:
li $s0, 0 # int i = 0
li $s1, 0 # int ix = 0
li $s2, 0 # int iy = 0
## if (incx == 1 && incy == 1) {
# for (i=0; i<5; i++)
# sy[i] = sy[i] + sa*sx[i];
## if either incx or incy are not equal to 1, branch to L2
bne $a1, 1, L2
nop
bne $a2, 1, L2
nop
## if-body
L1:
slti $t0, $s0, 5 # if $s0 (index i) is less than 5, set $t0 to 1
beqz $t0, loop # if $t0 = 0 (meaning i >= 5), exit if-block
nop
### arithmetic code
sw $t7, 0($t4) # loads $t7 into sy[i]
addi $s0, $s0, 1 # i++
j L1
nop
## else-body
L2:
slti $t0, $s0, 5 # $s0 (index i) is less than 5, set $t0 to 1
beqz $t0, loop # if index i reaches max, exit to loop
nop
#### arithmetic code
sw $t7, 0($t4) # $t7 (arithmetic) stored into address for sy[iy]
addi $s0, $s0, 1 # i++
j L2
nop
.end g