jueves, 21 de marzo de 2019

MIPS common used instructions

Load
In this example, I will use the first registers that NO$PSX uses.
General-purpose registers
The registers are like memory to store values, in this case MIPS 32 can store a 32bit value up to 0xFFFFFFFF

LOAD:
Load instructions are used to load (duh) data into registers, it can be from RAM, other register or immediate values.
- Immediate values -

# LUI (load unsigned immediate)
This is a pseudo instruction, will load a value up to 0xFFFF0000
lui at, 0x8008
The register at will end up with the value 0x80080000

# ORI (or immediate)
This performs a logical or in the desired register (Operation form: Z = X + Y)
This instruction is very handy on load values up to 0x0000FFFF and can be used in conjunction with LUI to load offsets like this:
lui at, 0x8008
ori at, at, 0x2030
And it looks like this: at = 0x8008 | 0x2030 at will end with the value 0x80082030

- Useful logical instruccions -
SLTI (Set on lower than immadiate)
It sets a boolean value (1 or 0) when the value in the register is lower than immediate
at = 0x20
slti v0 at, 0x0030
v0 will end up with 0x00000001

XORI (Exclusive OR immendiate)
This is very use for when you want to "add" if the values does not exist or "rest" if the value exist at bit level.
Example "add":
at = 0x20
xori at, at,0x0F
The register at will end with the value 0x2F
at = 0x2F
xori at, at,0x10
The register at will end with the value 0x3F since the value 0x10 does not exist in 0x2F(bit level value)

Example 2 "rest":
at = 0x3F
xori at, at,0x10
The register at will end with the value 0x2F since the value 0x10 does exist in 0x3F(bit level value)
at = 0x2F
xori at, at, 0x2F
The register at will end with the value 0x00 since is the same value as the immediate(bit level value)


ANDI (And immadite)
This is very useful to check if a value exists in a register special for jokers :)
at = 0x2F
andi at, at, 0x20
The register at will end with the value 0x20 since 0x2F contains 0x20(bit level value)
at = 0x20
andi at, at, 0x10
The register at will end with the value 0x00 since 0x20 does not contain 0x10 (bit level value)

Load values from RAM:
Useful when you have the offset and want to retrieve a value [Operation form: Z = Offset(Pointer)]

# LB (load byte, can load unsigned value)
Load a byte from RAM up to 0xFF
lb v0, 0x0000(at)

# LH (load halfword, can load unsigned value)
Load a byte from RAM up to 0xFFFF
lh v0, 0x0000(at)

# LW (load word)
Load a byte from RAM up to 0xFFFFFFFF
lw v0, 0x0000(at)

Note: all load instructions from RAM have a delay, that means you need an extra cycle until the desired value becomes available.

Store
Store values to RAM:
Useful when you need to write something to RAM [Operation form: RAM = Offset (Pointer)]

# SB (store byte)
Store a byte to RAM up to 0xFF
sb v0, 0x0000(at)

# SH (store halfword)
Store a byte to RAM up to 0xFFFF
sh v0, 0x0000(at)

# LW (store word)
Store a byte to RAM up to 0xFFFFFFFF
sw v0, 0x0000(at)

- Branch instructions -
TODO


Best emulator to start learning MIPS

Download:
NO$PSX

How to configure for MIPS:
First screen when you open NO$PSX
Go to options
Go to debug tab
Final screen