Converting a C function to MIPS assembly - c

I'm currently learning MIPS Assembly and I am attempting to convert the following C function into MIPS Assembly:
int count (int a[], int n, int x)
{
int res = 0;
int i = 0;
int j = 0;
int loc[];
for(i = 0; i != n; i++)
if(a[i] == x)
{
res = res + 1;
loc [j] = i;
j = j + 1;
}
return res, loc;
}
I've succeeded in converting most of it, and I believe I have successfully returned res (a value of 1), though I'm uncertain about returning loc (I also get a value of 1, and I don't think that's correct). However, I am having difficulty with this program and I am unsure as to how I can ensure that loc is returning the correct value or how to even code it to do so.
Here is my Assembly code:
.data
a: .word 5,6,7,8,9,10
n: .word
x: .word
res: .word 0
i: .word 0
jj: .word 0
loc: .space 40
.text
main:
la $s0, a
lw $s1, res
lw $s2, x
lw $t0, i
lw $t1, jj
lw $t2, n
la $s3, loc
li $t4, 6
start:
sll $t3, $t0, 2
add $t5, $t3, $s0
lw $t4, 0($t5)
beq $t0, $t4, start
addi $t0, $t0, 1
beq $t0, $t2, exit
addi $s1, $s1, 1
sll $t7, $t1, 2
add $t6, $s3, $t7
sw $t0, 0($t6)
addi $t1, $t1, 1
exit:
li $v0, 1
add $a0, $s1, $zero
syscall
li $v0, 1
add $a1, $s3, $zero
syscall
Any help, pointers, or suggestions would be very much appreciated.
EDIT: I've revised my code and now receive 0 for the res return and "268501028" for loc. Not sure where this number is coming from.
.data
a: .word 5,6,7,8,9,10
n: .word #n
x: .word #x
res: .word 0
i: .word 0
jj: .word 0
loc: .space 40
.text
main:
la $s0, a
lw $s1, res
lw $s2, x
lw $t0, i
lw $t1, jj
lw $t2, n
la $s3, loc
li $t4, 6
start:
beq $t0, $t2, exit #for(i = 0; i != n; i++)
bne $s0, $s2, else #if(a[i] == x)
j start
else:
addi $s1, $s1, 1 #res = res + 1;
sw $t0, ($t1) #loc [j] = i;
addi $t1, $t1, 1 #j = j+1
addi $t0, $t0, 1 #Increment i
addi $s3, $s3, 4 #Setting next element for loc
addi $s0, $s0, 4 #Setting next element for a
j start
exit:
li $v0, 1
move $a0, $s1
syscall
li $v0, 1
move $a0, $s3
syscall

Okay, there were a few bugs. I've annotated the source and added "BUG:" to hightlight them. I then created a cleaned up and corrected version
Here's your original code--no bug fixes, just annotations [please pardon the gratuitous style cleanup]:
# int
# count(int a[], int n, int x)
# {
# int res = 0;
# int i = 0;
# int j = 0;
# int loc[n];
#
# for (i = 0; i != n; i++) {
# if (a[i] == x) {
# res = res + 1;
# loc[j] = i;
# j = j + 1;
# }
# }
#
# return res, loc;
# }
.data
a: .word 5,6,7,8,9,10
n: .word
x: .word
res: .word 0
i: .word 0
jj: .word 0
loc: .space 40
nl: .asciiz "\n"
.text
.globl main
main:
la $s0,a
lw $s1,res
lw $s2,x
lw $t0,i
lw $t1,jj
lw $t2,n
la $s3,loc
li $t4,6 # BUG: extraneous (gets trashed below)
start:
sll $t3,$t0,2 # get i << 2
add $t5,$t3,$s0 # get &a[i]
lw $t4,0($t5) # fetch it
# BUG: we're comparing a[i] against i but we want to compare against x
# _and_ we want to flip the sense of the branch
beq $t0,$t4,start # is it a match? if yes, loop
addi $t0,$t0,1 # increment i
beq $t0,$t2,exit # i == n? if no, loop. if yes, exit
# BUG: the indexing here is wrong
addi $s1,$s1,1 # j += 1
sll $t7,$t1,2 # get jj << j
add $t6,$s3,$t7 # &loc[jj << j] (BUG: we want &loc[j])
sw $t0,0($t6) # set it to i
addi $t1,$t1,1 # jj += 1
# BUG: we should loop here and _not_ fall through
exit:
# print j (with newline)
li $v0,1
add $a0,$s1,$zero
syscall
li $v0,4
la $a0,nl
syscall
# print _address_ of loc[0]
# BUG: if we care to print anything, we should print the _values_ of the
# whole array
li $v0,1
# BUG: this should be a0 and _not_ a1
###add $a1,$s3,$zero
add $a0,$s3,$zero
syscall
li $v0,4
la $a0,nl
syscall
li $v0,10 # exit program
syscall
Here's the cleaned up and corrected version. I had to do a bit of restructuring and simplification to make it work, so it may seem a bit "alien" at first. However, I tried to retain your register usage where possible.
I also increased the size of the a array and added a user prompt for the x value:
# int
# count(int a[], int n, int x)
# {
# int i = 0;
# int j = 0;
# int loc[n];
#
# for (i = 0; i != n; i++) {
# if (a[i] == x) {
# loc[j] = i;
# j += 1;
# }
# }
#
# return j, loc;
# }
.data
a: .word 5,6,7,8,9,10
.word 5,6,7,8,9,10
.word 5,6,7,8,9,10
.word 5,6,7,8,9,10
.word 5,6,7,8,9,10
ae:
loc: .space 1000
prompt: .asciiz "Enter x value: "
msgnl: .asciiz "\n"
msgj: .asciiz "j: "
msgloc: .asciiz "loc: "
.text
# main -- main program
#
# RETURNS [sort of as this is a main program]:
# s1 -- j value (count of elements in "loc")
# loc -- filled in indexes into "a" array of matches to x
#
# registers:
# s0 -- a (base address of "a" array)
# t2 -- n (number of elements in "a" array)
#
# s2 -- x (value to match)
# t0 -- i (current index into "a" array)
# s3 -- loc (base address of "loc" array)
# s1 -- j (current index into "loc" array)
#
# t6 -- quick temporary [reusable]
# t7 -- used in array offset/index calculations [reusable]
.globl main
main:
# prompt for x value
li $v0,4 # syscall: print string
la $a0,prompt
syscall
# read in x value
li $v0,5 # syscall: read integer
syscall
move $s2,$v0
# get address of "a" array and compute length
la $s0,a # get &a[0]
la $t2,ae # get address of &a[n]
sub $t2,$t2,$s0 # get number of bytes in a
srl $t2,$t2,2 # get number of words in a (i.e. n)
li $t0,0 # i = 0
li $s1,0 # j = 0
la $s3,loc # base address of loc array
# main matching loop
loop:
sll $t7,$t0,2 # get i << 2
add $t7,$t7,$s0 # get &a[i]
lw $t6,0($t7) # fetch from it
bne $t6,$s2,next # a[i] == x? if no, advance to next element
# add new "i" value to loc array
sll $t7,$s1,2 # get j << 2
add $t7,$s3,$t7 # &loc[j << 2]
sw $t0,0($t7) # store i into loc
addi $s1,$s1,1 # j += 1
next:
addi $t0,$t0,1 # i += 1
blt $t0,$t2,loop # i < n? if yes, loop (or, we're done)
# done with calculation/fill loop
done:
la $s6,msgj # get prefix string
move $s7,$s1 # get j
jal prtnum # pretty print the number
blez $s1,exit # bug out if _no_ values in loc
# prepare to print all values of loc
la $t6,loc # base address of "loc"
li $t7,0 # initial index
# loop and print all values of loc
prtlocloop:
la $s6,msgloc # prefix string
lw $s7,($t6) # get loc[...]
jal prtnum # pretty print the number
add $t6,$t6,4 # increment address
add $t7,$t7,1 # increment index
blt $t7,$s1,prtlocloop # done? if no, loop
exit:
li $v0,10 # exit program
syscall
# prtnum -- print a number with a prefix string on a single line
#
# arguments:
# s6 -- prefix string
# s7 -- value to print
#
# registers:
# v0 -- syscall number [trashed]
# a0 -- syscall argument [trashed]
prtnum:
li $v0,4 # syscall: print string
move $a0,$s6 # string to print
syscall
li $v0,1 # syscall: print integer
move $a0,$s7 # value to print
syscall
li $v0,4 # syscall: print string
la $a0,msgnl
syscall
jr $ra # return
UPDATE:
What exactly is the difference between print and prtnum?
print is the label for the top of the loop that prints the values in loc. prtnum is subroutine/function that does the printing of a single number.
I added prtnum to demonstrate the use of a function and to avoid needless replication of some code.
Can they not be properly merged?
Sure, with some caveats. I did a slight/cosmetic edit to try to make things clearer. In particular, I renamed print: to prtlocloop: to try and make its role clearer.
The syscall(1) for "print integer" just prints the integer but does not add any whitespace or newline to separate them (i.e. it's exactly like printf("%d",a0)). So, we need something.
Originally, I just had the syscall(print_integer). With that, we get one "very long" number. Then, I added syscall(4) to print a newline. This was fine except the output was a bit confusing as to which value was j and which were the loc values.
(1) So, I added the "prefix" string. So, that became three syscalls for each number.
(2) This was used in two places: To print j and to print the loc values.
Same code in two or more places. That's the standard criterion for "split out code to function" in any language. It's a design/style choice [so there is no absolute answer].
So, with (1) and (2), I moved it to the prtnum function. Actually, I wrote the prtnum function first because I already knew the structure, and added the prefix argument after the output "looked ugly" [to me].
When I first coded it, I used "j: " for j and used a " " prefix for loc. It still looked a little funky. So, I changed the prefix to "loc: " to be consistent.
Could it be inlined? Sure. But, in addition to printing the number itself, we still have to add a separater. So, we need two syscalls per number to do it.
The separater could be a space if we want to put all numbers on the same output line. Fine for short vectors. This would require a slight change to the code as it exists now and we'd have to add a final output of newline to close the line. For longer arrays [that might not fit on a single line], one per line is [probably] tidier.
We only had to print j and loc. If the problem stated that we had to print a, then j, and then loc, I would have gone the other way.
I would have changed prtlocloop into another function (e.g. prtarray), that would loop on the given array and call prtnum for each element.
The first step was getting the calculation loop correct. The second was the printing. But, sometimes, they have to be done together. (i.e.) How can you debug something that you can't see?
So, with calculation correct, you are free to recode the output printing in any way you choose. The prtnum was just my way. But, it is by no means the only way.
Beyond the basic mechanics of working with the asm instructions, the choices are just like in any other language [notably C]. Comment well, choose the simplest and most effective way to architect/split the code, use descriptive variable names, etc. Comments should show "intent", the "what/why". The asm instructions are the "how".
Side note: Some OPs have had serious difficulty understanding how sll [which you already understand] works. They just didn't "get" the fact that a left shift by 2 was like a multiply by 4 and converts an index value into byte/address offset. So, you may already be ahead of the game ...
Yesterday, I gave an answer for a mips question where I went the other way and recommended inlining two functions. The problem was to calculate sin(x) using a Taylor series expansion [summation of terms] of the form: x**(2n)/factorial(2n-1).
With inlining, it was possible to reuse partial results from the previous term in the series without having to recalculate each term from scratch. This would not have been [conveniently] possible with multiple functions.
I didn't write the mips code, but I wrote the C/pseudo-code: mips program to calculate sin(x) The resulting mips code would [probably] have been simpler and would definitely run faster.

Related

Translating C function with args to MIPS: loop over an int array and count negatives

My question is about procedures in MIPS and using arguments.
I'm trying to translate this small C Function to MIPS and I wasn't sure if I was in the right track. This is the C function:
0 int countNegatives(int table[] , int n) {
1 int count = 0;
2 int i;
3
4 for (i=0; i<n; i++) {
5 if (table[i] <0) {
6 count++;
7 }
8 }
9
10 return count;
11 }
And this what I have on MIPS
main:
jal countNegatives
countNegatives:
li $t0, 0 #count = 0
li $t1, 0 #i = 0
loop:
bge $t1, $a1, endloop
sll $t2, $t1, 2 #$t2 = 4*i
add $t2, $a0, $t2 #$t2 = &table[i]
lw $t3, 0($t2) #temp = table[i]
bge $t3, $zero, endif
addi $t0, $t0, 1 #counter++
endif:
addi $t1, $t1, 1 #i++
endloop:
jr $ra
My code doesn't really run on QTSpim, and so I'm also trying to know if I'm missing any MIPS convention, and if I'm using the arguments in the procedure in a correct manner.
Thanks in advance if anyone can check the code out and see if something is wrong.
Except for some missing boilerplate, you were very close. Here's a version annotated with the bugs:
main:
# NOTE/BUG: a0/a1 are _not_ set up for the call
jal countNegatives
# NOTE/BUG: we just fall through into countNegatives again [which is bad]
countNegatives:
li $t0,0 # count = 0
li $t1,0 # i = 0
loop:
bge $t1,$a1,endloop
sll $t2,$t1,2 # $t2 = 4*i
add $t2,$a0,$t2 # $t2 = &table[i]
lw $t3,0($t2) # temp = table[i]
bge $t3,$zero,endif
addi $t0,$t0,1 # counter++
endif:
addi $t1,$t1,1 # i++
# NOTE/BUG: we need to loop here
endloop:
jr $ra
Here's a working version [with the added boilerplate]:
.data
arr: .word 10 20 -5 7 -6 0 1 -1 37
.text
.globl main
main:
la $a0,arr # point to array
li $a1,9 # array count
jal countNegatives
move $a0,$v0
li $v0,1
syscall
li $v0,10
syscall
# countNegatives -- count number of negatives
#
# RETURNS:
# v0 -- number of negative numbers found
#
# arguments:
# a0 -- pointer to array
# a1 -- array count
#
# temporaries:
# t1 -- index variable "i"
# t2 -- array offset / &table[i]
# t3 -- temp (value of table[i])
countNegatives:
li $v0,0 # count = 0
li $t1,0 # i = 0
loop:
bge $t1,$a1,endloop # i >= count? if yes, fly
sll $t2,$t1,2 # $t2 = 4*i
addu $t2,$a0,$t2 # $t2 = &table[i]
lw $t3,0($t2) # temp = table[i]
bge $t3,$zero,endif
addi $v0,$v0,1 # counter++
endif:
addi $t1,$t1,1 # i++
j loop
endloop:
jr $ra
Here's a just for fun version that uses slt instead of a conditional branch [and eliminates an extra jump inside the loop]:
.data
arr: .word 10 20 -5 7 -6 0 1 -1 37
.text
.globl main
main:
la $a0,arr # point to array
li $a1,9 # array count
jal countNegatives
move $a0,$v0
li $v0,1
syscall
li $v0,10
syscall
# countNegatives -- count number of negatives
#
# RETURNS:
# v0 -- number of negative numbers found
#
# arguments:
# a0 -- pointer to array
# a1 -- array count
#
# temporaries:
# t1 -- index variable "i"
# t2 -- array offset / &table[i]
# t3 -- temp (value of table[i])
countNegatives:
li $v0,0 # count = 0
li $t1,0 # i = 0
j loop_start # start the loop
loop:
sll $t2,$t1,2 # $t2 = 4*i
addu $t2,$a0,$t2 # $t2 = &table[i]
lw $t3,0($t2) # temp = table[i]
slt $t3,$t3,$zero # temp = (temp < 0)
add $v0,$v0,$t3 # counter += temp
addi $t1,$t1,1 # i++
loop_start:
blt $t1,$a1,loop # i < count? if yes, fly
jr $ra
Here's another version that uses pointer arithmetic instead of index variables.
Note that under the mips ABI, only the s* regs must be preserved by callee, so a0 and a1 are used as temporaries.
Also note that when adding addresses/pointers, as good practice, we want to use the unsigned versions of the add instructions (i.e. addu and addiu) to prevent [the unlikely possibility of] an overflow exception.
.data
arr: .word 10 20 -5 7 -6 0 1 -1 37
.text
.globl main
main:
la $a0,arr # point to array
li $a1,9 # array count
jal countNegatives
move $a0,$v0
li $v0,1
syscall
li $v0,10
syscall
# countNegatives -- count number of negatives
#
# RETURNS:
# v0 -- number of negative numbers found
#
# arguments:
# a0 -- pointer to array (ptr)
# a1 -- array count
#
# temporaries:
# a1 -- array limit (endp)
# t3 -- temp (value of table[i])
countNegatives:
li $v0,0 # count = 0
sll $a1,$a1,2 # get byte offset
addu $a1,$a0,$a1 # endp = &arr[count]
j loop_start # start the loop
loop:
lw $t3,0($a0) # temp = *ptr
slt $t3,$t3,$zero # temp = (temp < 0)
add $v0,$v0,$t3 # counter += temp
addiu $a0,$a0,4 # ptr += 4
loop_start:
bne $a0,$a1,loop # ptr != endp? if yes, fly
jr $ra
So, the final asm version, translated back into C would look something like this:
int
countNegatives(int *table, int n)
{
int *endp;
int count = 0;
endp = &table[n];
for (; table != endp; ++table)
count += (*table < 0);
return count;
}
It is not too difficult I think to see what decent compiler does:
https://godbolt.org/g/PiR8Ds
And you will have all the call conventions and another stuff toking by themselves.
#include <stdio.h>
int __attribute__((noinline)) countNegatives(int table[] , int n) {
int count = 0;
int i;
for (i=0; i<n; i++) {
if (table[i] <0) {
count++;
}
}
return count;
}
volatile int x[] = {454,-3,-5343,-3434,4534};
int main(void)
{
printf("%d\n",countNegatives((int *)x, sizeof(x)/sizeof(x[0])));
}
countNegatives:
blez $5,$L6
sll $5,$5,2
addu $5,$4,$5
move $2,$0
$L5:
lw $3,0($4)
addiu $4,$4,4
slt $3,$3,0
bne $4,$5,$L5
addu $2,$2,$3
j $31
nop
$L6:
j $31
move $2,$0
$LC0:
.ascii "%d\012\000"
main:
lui $4,%hi(x)
addiu $sp,$sp,-32
li $5,5 # 0x5
sw $31,28($sp)
jal countNegatives
addiu $4,$4,%lo(x)
lui $4,%hi($LC0)
move $5,$2
jal printf
addiu $4,$4,%lo($LC0)
lw $31,28($sp)
move $2,$0
j $31
addiu $sp,$sp,32
x:
.word 454
.word -3
.word -5343
.word -3434
.word 4534

MIPS array update

I have a problem in converting my C code to MIPS code. The original C code is below:
int A[4];
int i;
int diff;
for(i=0; i<3; i++){
diff = A[i+1] - A[i];
if (diff > 0)
A[i] = 5*A[i];
else
A[i+1] = -5*A[i];
}
And here is my MIPS Code below:
### MIPS PROJECT PART 1: ARRAY USING FOR LOOPS
.data
# allocate 16 bytes memory for 4 integer array
A: .space 16
.text
# Store array values in registers
addi $s0,$zero,2
addi $s1,$zero,4
addi $s2,$zero,6
addi $s3,$zero,8
# Index = $t0
addi $t0,$zero,0
# Store the first index and then store others by increasing $t0 by 4 bytes
sw $s0,A($t0)
addi $t0,$t0,4
sw $s1,A($t0)
addi $t0,$t0,4
sw $s2,A($t0)
addi $t0,$t0,4
sw $s3,A($t0)
main:
li $s4,0 # counter i = $s4
li $t2,3 # constant $t2
loop:
beq $s4, $t2, end # if t1 == 3 we are done
# diff = A[i+1] - A[i]
lw $t3,A($zero) # $t3 = A[i]
addi $t6, $zero,4 # next element A[i+1]
lw $t4,A($t6) # $t4 = A[i+1]
sub $t5,$t4,$t3 # dif = $t5 = A[i+1] - A[i]
# if (diff > 0)
bltz $t5, Else
# A[i] = 5*A[i]
add $t5,$zero,$t5
add $t5,$zero,$t5
add $t5,$zero,$t5
add $t5,$zero,$t5
sw $t5 A($zero)
Else:
# else A[i+1] = -5 * A[i];
addi $zero, $zero,4 #next element A[i+1]
#Loop body
addi $s4, $s4, 1 # add 1 to $s4
j loop # jump back to the top
end:
li $v0,10
syscall
The thing is that program does not update array values. Additionally I am restricted to use multiplication commands (mult or mull), that is why I am trying just to add it 5 times in control statement.
Your program has some bugs and is incomplete. I've created two versions. One with annotations for the bugs and a refactored one. Please pardon the gratuitous style cleanup.
Here's your asm program with annotations:
### MIPS PROJECT PART 1: ARRAY USING FOR LOOPS
.data
A: .space 16
# allocate 16 bytes memory for 4 integer array
.text
# Store array values in registers
addi $s0,$zero,2
addi $s1,$zero,4
addi $s2,$zero,6
addi $s3,$zero,8
# Index = $t0
addi $t0,$zero,0
# Store the first index and then store others by increasing $t0 by 4 bytes
sw $s0,A($t0)
addi $t0,$t0,4
sw $s1,A($t0)
addi $t0,$t0,4
sw $s2,A($t0)
addi $t0,$t0,4
sw $s3,A($t0)
# NOTE/BUG: this should go after the .text as the above code may be skipped
main:
li $s4,0 # counter i = $s4
li $t2,3 # constant $t2
loop:
beq $s4,$t2,end # if t1 == 3 we are done
# diff = A[i+1] - A[i]
# NOTE/BUG: this always loads from A[0]
lw $t3,A($zero) # $t3 = A[i]
# NOTE/BUG: this always loads from A[1]
addi $t6,$zero,4 # next element A[i+1]
lw $t4,A($t6) # $t4 = A[i+1]
sub $t5,$t4,$t3 # dif = $t5 = A[i+1] - A[i]
# if (diff > 0)
bltz $t5,Else
# NOTE/BUG: this is _not_ A[i] but is diff
# NOTE/BUG: this is _not_ 5* but higher (see below)
# A[i] = 5*A[i]
add $t5,$zero,$t5 # diff*2
add $t5,$zero,$t5 # diff*4
add $t5,$zero,$t5 # diff*8
add $t5,$zero,$t5 # diff*16
# NOTE/BUG: this always stores to A[0]
sw $t5,A($zero)
# NOTE/BUG: this falls through to the else case
Else:
# else A[i+1] = -5 * A[i];
# NOTE/BUG: this is just a nop because a $zero as destination reg does
# nothing
addi $zero,$zero,4 # next element A[i+1]
# Loop body
addi $s4,$s4,1 # add 1 to $s4
# NOTE/BUG: this stores nothing
j loop # jump back to the top
end:
li $v0,10
syscall
I rewrote the C code slightly to make the asm easier:
int A[4] = { 2, 4, 6, 8 };
void
calc(int *arr,int cnt)
{
int tmp;
int diff;
int *endp;
endp = &arr[cnt];
for (; arr < endp; arr += 1) {
tmp = arr[0];
diff = arr[1] - tmp;
if (diff > 0) {
tmp = 5 * tmp;
arr[0] = tmp;
}
else {
tmp = -5 * tmp;
arr[1] = tmp;
}
}
}
Here's the refactored asm code:
### MIPS PROJECT PART 1: ARRAY USING FOR LOOPS
.data
# allocate 16 bytes memory for 4 integer array
A: .word 2,4,6,8
space: .asciiz " "
nl: .asciiz "\n"
.text
main:
la $a0,A
li $a1,4
jal print
la $a0,A
li $a1,3
jal calc
la $a0,A
li $a1,4
jal print
li $v0,10
syscall
# calc -- calculate results
#
# arguments:
# a0 -- pointer to array
# a1 -- count of array
calc:
# get endp
sll $a1,$a1,2 # convert int count to byte count
addu $a1,$a1,$a0 # add in array base
j calc_start # start loop
calc_loop:
lw $t3,0($a0) # tmp = A[i]
lw $t4,4($a0) # A[i + 1]
sub $t5,$t4,$t3 # get diff
bltz $t5,calc_neg # < 0
add $t5,$t3,$t3 # tmp = A[i]*2
add $t5,$t5,$t5 # tmp = A[i]*4
add $t5,$t5,$t3 # tmp = A[i]*5
sw $t5,0($a0)
b calc_next
calc_neg:
neg $t3,$t3
add $t5,$t3,$t3 # tmp = A[i]*2
add $t5,$t5,$t5 # tmp = A[i]*4
add $t5,$t5,$t3 # tmp = A[i]*5
sw $t5,4($a0)
calc_next:
addiu $a0,$a0,4
calc_start:
bne $a0,$a1,calc_loop # more to do? if yes, fly
jr $ra # return
# print -- print results
#
# arguments:
# a0 -- pointer to array
# a1 -- count of array
#
# temporaries:
# a2 -- array pointer
# a3 -- array count
print:
move $a2,$a0
move $a3,$a1
j print_next
print_loop:
li $v0,4
la $a0,space
syscall
li $v0,1
lw $a0,0($a2) # get current value
syscall
addiu $a2,$a2,4
addi $a3,$a3,-1
print_next:
bnez $a3,print_loop
li $v0,4
la $a0,nl
syscall
jr $ra # return

C to MIPS - Instruction references undefined QTSpim

I was trying to do an assignment translating from C to MIPS but I get a instruction reference error in jal main. Here is what I have to translate:
void swap (int a, int b)
{
int temp=a;
a=b;
b=temp;
}
int distance (int a, int b)
{
if (b > a)
swap (a,b);
return (a-b)
}
And here is what I wrote:
.data #declare the variables
var1: .word 4, 7, 12, 5
var2: .word 15, 3, 6, 14
result: .space 4
.text
main:
la $t0, var1 #load address 'var1' into $t0
la $t1, var2 #load address 'var2' into $t1
la $t2, result #load address 'result' into $t2
li $t3, 0 # load imm (i=0)
for_loop:
bgt $t3, 4, for_done #when i>4 do not meet condition, exit
lw $t4, 0($t4) #result[i] = tmp
jal distance
addi $t3, $t3, 1 #i++
j for_loop
for_done:
la $t2, distance
ori $v0, $0, 4
syscall
distance:
blt $t0, $t1, exit
jal swap
sub $t5, $t0, $t1
jr $t5
swap:
lw $t6, 0($t0)
lw $t7, 0($t1)
sw $t6, 0($t1)
sw $t7, 0($t0)
exit:
I actually don't know what am I doing, just the basics of Assembly. I hope some of you could help me. :)
I'm sorry but your asm code had [at least] 15 bugs.
I've created two versions: one with the bugs annotated and a second with the bugs fixed and a working program
Note that due to the vagueness of the C code, I had to guess the true intent of the program and take [considerable] poetic license.
Here's the unchanged version with the bugs annotated [please pardon the gratuitous style cleanup]:
.data # declare the variables
var1: .word 4, 7, 12, 5
var2: .word 15, 3, 6, 14
# NOTE/BUG: this only reserves 4 bytes instead of the 16 we need to hold all
# four values
result: .space 4
.text
main:
la $t0,var1 # load address 'var1' into $t0
la $t1,var2 # load address 'var2' into $t1
la $t2,result # load address 'result' into $t2
li $t3,0 # load imm (i=0)
for_loop:
# NOTE/BUG: this goes one too far (i.e. we want i>=4)
bgt $t3,4,for_done # when i>4 do not meet condition, exit
# NOTE/BUG: $t4 is never intialized to anything, so this instruction will fault
# (e.g. equivalent to dererencing a null pointer in C)
lw $t4,0($t4) # result[i] = tmp
jal distance
# NOTE/BUG: the index variable 'i' is incremented, but distance does _not_ use
# (i.e.) distance will always use var1[0] and var2[0] on each iteration
addi $t3,$t3,1 # i++
j for_loop
# NOTE/BUG: what do we want to do here? -- print the result vector presumably
# NOTE/BUG: syscall 4 is to print a string -- it would require setting up $a0
# and _not_ $t2 but, even then, using 'distance' is wrong as distance is the
# function name and _not_ a string so we'd get garbage
# NOTE/BUG: we probably wouldn't even get that far because QtSpim would
# probably fault because distance is in the .text segment and not the .data
# segment
for_done:
la $t2,distance
ori $v0,$0,4
syscall
distance:
# NOTE/BUG: this is comparing _addresses_ instead of _values_ (i.e.) this
# compares (&var1[i] > &var2[i]) instead of var1[i] > var2[i])
# NOTE/BUG: this test is _reversed_, because this guarantees negative numbers
blt $t0,$t1,exit
# NOTE/BUG: jal is calling swap as a function, but swap is merely a label here
jal swap
# NOTE/BUG: based on the mips ABI, return values go into $v0
sub $t5,$t0,$t1
# NOTE/BUG: when 'jal distance' is called, the return address goes into $ra
# and to return to the place in main that called us, we want to do 'jr $ra'
# NOTE/BUG: this 'jr' should be at exit:
jr $t5
# NOTE/BUG: this actually swaps var1[i] and var2[i] -- would this be correct to
# modify the original arrays???
swap:
lw $t6,0($t0)
lw $t7,0($t1)
sw $t6,0($t1)
sw $t7,0($t0)
# NOTE/BUG: this is where the 'jr' should go
exit:
Here's the cleaned up and working version. I decided that it should store the distance in the result vector and then show all three vectors:
.data
# NOTE: lw/sw must be four byte aligned so keep these first
var1: .word 4, 7, 12, 5
var2: .word 15, 3, 6, 14
result: .space 16
msg_var1: .asciiz "var1:"
msg_var2: .asciiz "var2:"
msg_result: .asciiz "dist:"
msg_space: .asciiz " "
msg_nl: .asciiz "\n"
.text
main:
la $s0,var1 # load address of 'var1'
la $s1,var2 # load address of 'var2'
la $s2,result # load address of 'result'
li $s3,4 # number of elements in a given vector
li $s4,0 # load imm (i=0)
for_loop:
bge $s4,$s3,for_done # i <= count? if no, fly
jal distance
addi $s4,$s4,1 # i++
j for_loop
for_done:
la $a0,msg_var1
la $a1,var1
jal show
la $a0,msg_var2
la $a1,var2
jal show
la $a0,msg_result
la $a1,result
jal show
# exit program
li $v0,10
syscall
# distance -- calculate distance between two numbers in two vectors
#
# RETURNS:
# stores into 'result' vector
#
# global registers:
# s0 -- pointer to var1
# s1 -- pointer to var2
# s2 -- pointer to result
# s4 -- array index
#
# registers:
# t0 -- address and value of var1[i]
# t1 -- address and value of var2[i]
# t2 -- temp value
# t7 -- byte offset corresponding to index 'i'
distance:
sll $t7,$s4,2 # convert index to byte offset
addu $t0,$s0,$t7 # get &var1[i]
lw $t0,0($t0) # fetch var1[i]
addu $t1,$s1,$t7 # get &var2[i]
lw $t1,0($t1) # fetch var2[i]
bge $t0,$t1,distance_done # swap a/b to get abs val? if no, fly
# swap a/b
move $t2,$t0 # temp = a
move $t0,$t1 # a = b
move $t1,$t2 # b = temp
distance_done:
sub $v0,$t0,$t1 # get distance (i.e.) abs(a-b)
addu $t2,$s2,$t7 # get &result[i]
sw $v0,0($t2) # result[i] = distance
jr $ra # return
# show -- show vector
#
# arguments:
# a0 -- vector name
# a1 -- pointer to vector
#
# registers:
# t3 -- array remaining count
#
# clobbers:
# v0
show:
li $v0,4 # syscall to print string
syscall
move $t3,$s3 # get number of elements in vector
show_loop:
blez $t3,show_done # more to do? if no, fly
li $v0,4
la $a0,msg_space # output a space
syscall
# output vector[i]
li $v0,1 # syscall to output value
lw $a0,0($a1) # get vector value
syscall
addiu $a1,$a1,4 # advance pointer to next array element
addi $t3,$t3,-1 # bump down count
j show_loop
show_done:
# output newline
la $v0,4
la $a0,msg_nl
syscall
jr $ra # return

Jump to Address after Branching in For Loop in MIPS

I am trying to code a program that checks if the 16 bits in an integer is a one or zero. I chose to implement this by shifting right one bit 15 times and checking if the first bit in each shift is a zero or non zero. Then, if the first bit is a 1, I increment an integer.
I made some code in C that represents a non-user input version of my code.
int j = 100;
int checker = 0;
int count = 0;
for (i=0; i<16; i++) {
checker = j & 0x1;
if (checker > 0)
count++;
j = (j >> 1);
}
My code in MIPS:
.data
userprompt: .asciiz "Enter positive integer: "
newline: .asciiz "\n"
.text
.globl main
main:
li $v0, 4 # System call: Display string
la $a0, userprompt # Load string userprompt for output
syscall
li $v0, 5 # System call: Read integer
syscall
move $s0, $v0 # Store integer from v0 to s0
move $s1, $s0 # s1 = s0
li $t0, 0 # t0 = 0
jal chk_zeros # Run function: chk_zeroes
li $v0, 1 # System call: Read integer
move $a0, $t2 # Store integer from t2 to a0
syscall
li $v0, 10 # System call: quit
syscall
chk_zeros:
bgt $t0, 15, exitchk # t0 <= 15
addi $t0, $t0, 1 # Add one to t0
andi $t1, $s1, 0x1 # Check if first bit is non-zero, store in t1
bgtz $t1, chk_zerosadd # If t1 >= 0
j chk_zeros
chk_zerosadd:
addi $t2, $t2, 1 # Add one to t2
jr $ra # Return to after the if statement (does not work!)
exitchk:
jr $ra
What I am having trouble with is making chk_zerosadd return to after the branching statement. jr $ra seems to return me to my main function in chk_zerosadd.
bgtz doesn't place the next PC address into the return address register, so jr $ra won't return to the instruction after the branching statement. You can either use bgtzal (branch if greater than zero and link), which will give you the behaviour you are looking for, or else you can re-arrange your code so that you branch over the add, instead of branching to it, like this:
andi $t1, $s1, 0x1 # Check if first bit is non-zero, store in t1
beq $t1, chk_zerosskipadd # Jump if $t1 is zerp
addi $t2, $t2, 1 # Add one to t2
chk_zerosskipadd:
# continue execution...
srl $s1, $s1, 1 # j = (j >> 1);
j chk_zeros

C Programming to MIPS Assembly (for Loops)

I'm Trying to convert this C code to MIPS assembly and I am unsure if it is correct. Can someone help me? Please
Question : Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds the base address of the array D
C Code :
for(i=0; i<a; i++)
for(j=0; j<b; j++)
D[4*j] = i + j;
My Attempt at MIPS ASSEMBLY
add $t0, $t0, $zero # i = 0
add $t1, $t1, $zero # j = 0
L1 : slt $t2, $t0, $s0 # i<a
beq $t2, $zero, EXIT # if $t2 == 0, Exit
add $t1, $zero, $zero # j=0
addi $t0, $t0, 1 # i ++
L2 : slt $t3, $t1, $s1 # j<b
beq $t3, $zero, L1, # if $t3 == 0, goto L1
add $t4, $t0, $t1 # $t4 = i+j
muli $t5, $t1, 4 # $t5 = $t1 * 4
sll $t5, $t5, 2 # $t5 << 2
add $t5, $t5, $s2 # D + $t5
sw $t4, $t5($s2) # store word $t4 in addr $t5(D)
addi $t0, $t1, 1 # j ++
j L2 # goto L2
EXIT :
add $t0, $t0, $zero # i = 0 Nope, that leaves $t0 unmodified, holding whatever garbage it did before. Perhaps you meant to use addi $t0, $zero, 0?
Also, MIPS doesn't have 2-register addressing modes (for integer load/store), only 16-bit-constant ($reg). $t5($s2) isn't legal. You need a separate addu instruction, or better a pointer-increment.
(You should use addu instead of add for pointer math; it's not an error if address calculation crosses from the low half to high half of address space.)
In C, it's undefined behaviour for another thread to be reading an object while you're writing it, so we can optimize away the actual looping of the outer loop. Unless the type of D is _Atomic int *D or volatile int *D, but that isn't specified in the question.
The inner loop writes the same elements every time regardless of the outer loop counter, so we can optimize away the outer loop and only do the final outer iteration, with i = a-1. Unless a <= 0, then we must skip the outer loop body, i.e. do nothing.
Optimizing away all but the last store to every location is called "dead store elimination". The stores in earlier outer-loop iterations are "dead" because they're overwritten with nothing reading their value.
You normally want to put the loop condition at the bottom of the loop, so the loop branch is a bne $t0, $t1, top_of_loop for example. (MIPS has bne as a native hardware instruction; blt is only a pseudo-instruction unless the 2nd register is $zero.) So we want to optimize j<b to j!=b because we know we're counting upward.
Put a conditional branch before the loop to check if it might need to run zero times. e.g. blez $s0, after_loop to skip the inner loop body if b <= 0.
An idiomatic for(i=0 ; i<a ; i++) loop in asm looks like this in C (or some variation on this).
if(a<=0) goto end_of_loop;
int i=0;
do{ ... }while(++i != a);
Or if i isn't used inside the loop, then i=a and do{}while(--i). (i.e. add -1 and use bnez). Although MIPS can branch just as efficiently on i!=a as it can on i!=0, unlike most architectures with a FLAGS register where counting down saves a compare instruction.
D[4*j] means we stride by 16 bytes in a word array. Separately using a multiply by 4 and a shift by 2 is crazy redundant. Just keep a pointer in a separate register an increment it by 16 every iteration, like a C compiler would.
We don't know the type of D, or any of the other variables for that matter. If any of them are narrow unsigned integers, we might need to implement 8 or 16-bit truncation/wrapping.
But your implementation assumes they're all int or unsigned, so let's do that.
I'm assuming a MIPS without branch-delay slots, like MARS simulates by default.
i+j starts out (with j=0) as a-1 on the last outer-loop iteration that sets the final value. It runs up to j=b-1, so the max value is a-1 + b-1.
Simplifying the problem down to the values we need to store, and the locations we need to store them in, before writing any asm, means the asm we do write is a lot simpler and easier to debug.
You could check the validity of most of these transformations by doing them in C source and checking with a unit test in C.
# int a: $s0
# int b: $s1
# int *D: $s2
# Pointer to D[4*j] : $t0
# int i+j : $t1
# int a-1 + b : $t2 loop bound
blez $s0, EXIT # if(a<=0) goto EXIT
blez $s1, EXIT # if(b<=0) goto EXIT
# now we know both a and b loops run at least once so there's work to do
addiu $t1, $s0, -1 # tmp = a-1 // addu because the C source doesn't do this operation, so we must not fault on signed overflow here. Although that's impossible because we already excluded negatives
addu $t2, $t1, $s1 # tmp_end = a-1 + b // one past the max we store
add $t0, $s2, $zero # p = D // to avoid destroying the D pointer? Otherwise increment it.
inner: # do {
sw $t1, ($t0) # tmp = i+j
addiu $t1, $t1, 1 # tmp++;
addiu $t0, $t0, 16 # 4*sizeof(*D) # could go in the branch-delay slot
bne $t1, $t2, inner # }while(tmp != tmp_end)
EXIT:
We could have done the increment first, before the store, and used a-2 and a+b-2 as the initializer for tmp and tmp_end. On some real pipelined/superscalar MIPS CPUs, that might be better to avoid putting the increment right before the bne that reads it. (After moving the pointer-increment into the branch-delay slot). Of course you'd actually unroll to save work, e.g. using sw $t1, 16($t0) and 32($t0) / 48($t0).
Again on a real MIPS with branch delays, you'd move some of the init of $t0..2 to fill the branch delay slots from the early-out blez instructions, because they couldn't be adjacent.
So as you can see, your version was over-complicated to say the least. Nothing in the question said we have to transliterate each C expression to asm separately, and the whole point of C is the "as-if" rule that allows optimizations like this.
This similar C code compiles and translates to MIPS:
#include <stdio.h>
main()
{
int a,b,i,j=5;
int D[50];
for(i=0; i<a; i++)
for(j=0; j<b; j++)
D[4*j] = i + j;
}
Result:
.file 1 "Ccode.c"
# -G value = 8, Cpu = 3000, ISA = 1
# GNU C version cygnus-2.7.2-970404 (mips-mips-ecoff) compiled by GNU C version cygnus-2.7.2-970404.
# options passed: -msoft-float
# options enabled: -fpeephole -ffunction-cse -fkeep-static-consts
# -fpcc-struct-return -fcommon -fverbose-asm -fgnu-linker -msoft-float
# -meb -mcpu=3000
gcc2_compiled.:
__gnu_compiled_c:
.text
.align 2
.globl main
.ent main
main:
.frame $fp,240,$31 # vars= 216, regs= 2/0, args= 16, extra= 0
.mask 0xc0000000,-4
.fmask 0x00000000,0
subu $sp,$sp,240
sw $31,236($sp)
sw $fp,232($sp)
move $fp,$sp
jal __main
li $2,5 # 0x00000005
sw $2,28($fp)
sw $0,24($fp)
$L2:
lw $2,24($fp)
lw $3,16($fp)
slt $2,$2,$3
bne $2,$0,$L5
j $L3
$L5:
.set noreorder
nop
.set reorder
sw $0,28($fp)
$L6:
lw $2,28($fp)
lw $3,20($fp)
slt $2,$2,$3
bne $2,$0,$L9
j $L4
$L9:
lw $2,28($fp)
move $3,$2
sll $2,$3,4
addu $4,$fp,16
addu $3,$2,$4
addu $2,$3,16
lw $3,24($fp)
lw $4,28($fp)
addu $3,$3,$4
sw $3,0($2)
$L8:
lw $2,28($fp)
addu $3,$2,1
sw $3,28($fp)
j $L6
$L7:
$L4:
lw $2,24($fp)
addu $3,$2,1
sw $3,24($fp)
j $L2
$L3:
$L1:
move $sp,$fp # sp not trusted here
lw $31,236($sp)
lw $fp,232($sp)
addu $sp,$sp,240
j $31
.end main

Resources