Search code examples
assemblymipsmips32

MIPS32 assembly beq instruction explanation


Can someone explain why this following assembly line makes sense ?

beq $0, $0, 1

Note that $0 refers to a register that always have the value of 0. So we are saying if $0 = $0 then go to PC + 4 + 1 else go to next instruction.

My confusion comes from the immediate field of beq instruction which is 1. Does this mean we are going to address PC + 5 ??? Doesn't MIPS requires alignment when accessing memory and all memory locations have to be divisible by 4 ?

Note that the book says that this instruction just skips the next instruction.


Solution

  • The semantic of beq $t, $s, offset is

    if ($t == $s) 
        PC = PC + 4 + 4 * offset;
    else 
        PC = PC + 4
    

    Simply put the PC is always advanced by 4 by the time the instruction is executed and the immediate is assumed missing the lower two bits since they are always zero and can be reintroduced with a shift (offset * 4 = offset << 2).


    beq $0, $0, 1 just skip the next instruction as it tautologically set PC = PC + 8.