I just started to learn the basics of MIPS. While reading a book about MIPS I thought about the next question:
I have the following code:
while (x) {...}
What's between brackets isn't important (just put dots on the right place on the code). It is know that x contains a Boolean statement. Let reg $t0 contain x. I am trying to convert this C while loop into a MIPS code. How to do so?
You can do it with a branch and a label. For example, the statement
while (n>0) { .... }
can be translated to MIPS using a branch and a label:
.data
n: .word 100
.text
main:
la $t0, n
lw $t1, 0($t0)
li $s0, 0
Loop:
bgt $t1, $zero, EXIT
add $t1, $s0, $t1
addi $t1, $t1, -1
j Loop
EXIT:
Related
I am trying to convert this for loop into MIPS assembly language and i am not sure how exactly to tackle this question. I tried using youtube to understand the concept of it and I am still strugling with it.
here is the C code:
int sum = 0;
for (int i = 1; i <= 10; i ++){
if (i & 1) sum = sum + i * i;
else sum = sum + i;
}
I tried converting but i am just not sure where to start it.
expecting to get MIPS code with explanation in possible so I can learn from it!
I assume that you are doing the bitwise and operator with 1 to indicate if the current 'i' is odd or even. What you can do is the following :
.text
li $t0, 1 #is for counting or i in this case
li $t1, 1 #for if statements condition
li $v0, 0 #output register
forLoop:
bgt $t0, 10, end
andi $t2, $t0, 1 #and opertion
beq $t2, 1, odd #if andi resulted in one do the odd computation if not it is even
add $v0, $v0, $t0
addi $t0, $t0, 1
j forLoop
odd:
mul $t2, $t0, $t0 #t2 is like a temporary register can be overwritten, square of i
add $v0, $v0, $t2
addi $t0, $t0, 1
j forLoop
end:
move $a0, $v0
li $v0, 1
syscall
li $v0, 10
syscall
This implementation is correct however the "right" implementation should use stack pointer and jump and link command for calling the function. This example has the sole purpose for showing the logic behind it.
c code
while (save[i]==k)
i+=1;
put i in $s3, k in $s5 and address of k in $s6
mips code
loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j loop
Exit:
Your save array/pointer must be of some 4-byte type (ints?). Therefore to load save[i] from memory, the index i needs to be translated to a byte offset within the array, and then added to the base address of that array. This is done by multiplying i by four:
sll $t1, $s3, 2
and then adding save:
add $t1, $t1, $s6
This doesn't look like an optimized build though. Usually the compiler can re-write this code to advance a temporary pointer in increments of four directly, thus avoiding two of the instructions in that loop.
I am trying to translate the following C code to mips:
While (i>=j)
{
k = k+ 5
}
The following is the mips code I have so far.
.data
.text
addi $s0,$s0,10
addi $s1,$s1,5
addi $s2,$s2,3
addi $t0,$s0,1 #Adding one to i to have same functionality as a '>='
loop:
slt $t1,$s1,$t0
addi $t2,$zero,1
beq $t1,$t2,op
j exit
op:
addi $s2,$s2,5
j loop
exit:
I'm new at MIPS and have been trying to copy elements from one array to another. I'm unsure about how to go about this. It doesn't really matter what size the array is but lets just say for the sake of doing it that its size 10. I am little weak with MIPS loops and am kind of confused on how to proceed.
add $s0, $zero, $zero
add $t0, $zero, $zero
lui $s0, 0x1001
ori $s0,$s0,0
lui $t0, 0x1001
ori $t0, $t0, 0x0040
There my initialization with $s0 being the address first element in the first array and $t0 being the address of the first element in the 2nd one.
I do not believe the code you have provided is correct, but assuming it is, you would do something like this:
xor $t1, $t1, $t1 ; Zero out $t1
lw $t2, array_length ; Load the length of the array in $t2
loop_start:
lb $t3, $s0 ; Load the next byte from $s0 into $t3
sb $t3, $t0 ; Store the by in $t3 into $t0
addi $s0, $s0, 1 ; Move to the next byte in the source
addi $t0, $t0, 1 ; Move to the next byte in the destination
addi $t1, $t1, 1 ; increment the counter
blt $t1, $t2, loop_start ; Jump to the start of the loop of there are more bytes
Disclaimer: I have not programmed in MIPS since college so this code may not be 100% accurate, but I believe it will give you a place to start.
okay, C++ and java i have no problem learning or what so ever
when it comes to mips it is like hell
okay i wanna learn how to read in the an array and print all the element out
here is a simple array that i wrote
int[] a = new int[20];
for(int i=0; i<a.length; i++){
a[i]=1;
}
for(int j=0; j<a.length; j++){
System.out.Println(a[i])
}
how do you do it in mips
Assuming that you have your array address at register $a1, you can do the following:
li $t0, 1
move $t1, $a1
addi $t2, $a1, 80
loop1:
sw $t0, ($t1)
addi $t1, $t1, 4
bne $t1, $t2, loop1
move $t1, $a1
loop2:
lw $t0, ($t1)
li $v0, 1
move $a0, $t0
syscall
addi $t1, $t1, 4
bne $t1, $t2, loop2
This code should produce the same result as your java code, except that you used println (which will print each element in a new line) and this code will print all the elements of the array in the same line.
I don't know if you have noticed, but your Java code and this code will print all 1s, if you want to print numbers from 1 to 19, you will have to increment $t0, inside loop1