# This shows some examples of instructions to test translation of MIPS to Hex (binary)
.text
.globl __start
#
__start:
loop: multu $19, $10
# R-type instruction
# 0x 026A0019
lw $8, example($9)
# pseudo-instruction
# Becomes
# lui $1, 4097 - 0x 3c011001
# addu $1, $1, $9 - 0x 00290821
# lw $8, 1200($1) - 0x 8c2804b0
# 1200 is in bytes and is stored as a signed extended number.
# What if example was more than 2^15 away?
# The pseudo instruction lui $1, 4097, changes to lui $1, 4098
# That lets the offset be in between -2^15 and 2^15
# Explicitly writing lw $8, 65000($1) would result in a different
# lui $1 base address. try it and see.
bne $8, $21,exit
# I-type instruction.
# The 16 bits are used to store an offset from the PC
# equal to the number of instructions from PC + 4 to exit.
# Offset ranges from -2^15 through 2^15 - 1.
# Note we use PC + 4 because the PC is incremented before the branch is executed.
# Note that the number is not in bytes but in instructions.
# 0x 15150002
# What if exit were more than 32k lines away?
# You get a translation error and need to use a jump instruction
addi $29, $28, 4
# I-type instruction.
# The 16 bits holds the sign-extended immediate signed number.
# 0x 239d0004
# try addi $29, $28, 32800. You get an assembler translation error because 32800 > 2^15 -1.
j loop
# J-type instruction
# 26 bits are used to store the actual 32-bit target address of loop.
# Last two bits of 32 bits are assumed to be zero
# because address of instruction is always a multiple of 4.
# And, first 4 bits are assumed to be 0000 (0 in Hex)
# because the .text section start at 0x 00400000
# 0x 08100000
exit:
.data
.space 1200
# try .space 33000
# try .space 65000
example: