I'm new to mips and was trying make equivalent instructions from Java:
if (x == y)
z = 14;
else
w = 23;
I've come up with:
.data
w: .word 23
x: .word 1
y: .word 1
z: .word 14
.text
lw $t1 x
lw $t2 y
lw $t3 z
lw $t4 w
bne $t1, $t2, L1
j L2
L1: jr $t4
L2: jr $t3
but I get an error: invalid program counter value: 0x0000000e
I have no idea what is wrong. Any help in the right direction would be appreciated.
Jr
means jump register and it is intended to be used when you have a function in MIPS
. It's like return in C
and many other languages. In your case you don't have a function so you don't need to return something. Your code should look something like this :
.data
w: .word 23
x: .word 1
y: .word 1
z: .word 14
.text
lw $t1 x
lw $t2 y
lw $t3 z
lw $t4 w
bne $t1, $t2, L1
L1:
#Do something
$v0,10
syscall
In the future you want to use jr
or jal
while most likely using a stack
. Overtime you are calling a different function you want the compiler to have a "please to return", so you are saving the address each time in $ra
(return address). In other words if you just want to use branching without calling a function you don't need to use jr.
I completely forgot to upload what I came up with. The next lesson my teacher covered was jr and jal.
.data
w: .word 23
x: .word 1
y: .word 1
z: .word 14
.text
lw $t1 x
lw $t2 y
bne $t1, $t2, L1
li $t5 14
sw $t5 z
j out
L1: li $t4 23
sw $t4 w
out:
User contributions licensed under CC BY-SA 3.0