Wednesday, March 18, 2020

MIPS jalr instruction demo

This is a sample MIPS assembler demo code to test the jalr instruction. Both the full jalr instruction syntax and the implied jalr syntax are demonstrated. The code is fully commented.

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.

# ----- Demonstration of the MIPS jalr instruction ----- #
.text

main:

# ...................................................... #
#   jalr $d, $s: rd <- return address, PC <- rs          #

    la $v0, sub1
    jalr $t0, $v0   # jump to address pointed to by $v0
                    # $t0 <- return address
                     
# ...................................................... #
# jalr $s: rd = $ra (implied)<- return address, PC <- rs #

    la $v1, sub2
    jalr $v1        # jump to address pointed to by $v1
                    # $ra(implied) <- return address

# ...................................................... #

    li $v0, 10
    syscall         # exit application

# ...................................................... #

sub1:
   sll $0, $0, 0    # Do nothing!
   jr $t0           # return to address pointed to by $t0

# ...................................................... #

sub2:
    sll $0, $0, 0   # Do nothing!
    jr $ra          # return to address pointed to by $ra

# ------------------------------------------------------ #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment