Search code examples
assemblymips

check if a register value is even/odd in MIPS


I tried to do the following:

                        # with $s6 holding i+j
andi $s7, $s6, 0x1      # (i + j) & 1 (to check if it's even)
if: bneq $s7, $zero, else

however it generates an error... am I doing something wrong?


Solution

  • Perhaps your assembler doesn't like 0x1 integers?

    andi $s7, $s6, 1
    bnez $s7, odd         # branch if not-equal to Zero.
    

    Or if your assembler doesn't like ABI names for registers, use register numbers?

    andi $23, $22, 1      # $22=$s6  and   $23=$s7
    

    If you use MARS or SPIM Simulator, or clang or other normal MIPS assemblers,
    andi $s7, $s6, 0x1 assembles just fine.


    Note andi doesn't add anything, so the i+j comment doesn't match

    andi Rdest, Rsrc1, Imm
    Put the logical AND of the integers from register Rsrc1 And Imm into register Rdest.