Branching in mips

1

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.

if-statement
assembly
branch
mips
asked on Stack Overflow Oct 13, 2015 by cry2u6 • edited Oct 13, 2015 by Konrad Lindenbach

2 Answers

0

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.

answered on Stack Overflow Oct 13, 2015 by Korpel • edited Oct 15, 2015 by Korpel
0

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: 
answered on Stack Overflow Oct 21, 2015 by cry2u6

User contributions licensed under CC BY-SA 3.0