Section 2: Learning Basic MIPS Instructons and how to use them
Basic MIPS Intructions:
Loading Commands:
lui - Load upper immidiate
lw - Load Word
lh - Load half word
lb - Load Byte
Storing Commands:
sw - Store Word
sh - Store Halfword
sb - Store Byte
Branch Commands:
bne - Branch on not equal
beq - Branch on equal
Jump Commands:
jr - Jump Register
j - Jump
jal - Jump and link
Other Commands:
addiu - add immidiate unsigned
nop - No operation
Registers:
$t0-$t9 - Temporary Registers
$ra - Return address
$zero($0) - zero
How to use the MIPS:
Loading Commands:
lui:
Loads 32-Bits of a code and shifts it 16-Bits to the left. Meaning what it does is loads the first half of a code. it looks like this when you use it :
lw:
Load the value in the a register to another register. Meaning it's loading the value of the address/pointer in the register you tell it to.It looks like this when you use it:
lh:
Loads 4 numbers in a register and loads it back to another register. Pretty much just loading the address. It looks like this when you use it:
lb:
Loading a byte into a register and loads it into another register. Looks like this when youuse it:
Storing Commands:
sw:
Stores 32-bits of the value in a register and stores it in another register. Example:
sh:
Stores 16-bit of value in a register into another . Example:
sb:
Stores 8-Bits of code in a register and stores it into another register. Example:
Branch Commands:
*Important Branch Command Rule: Their must be a delay slot at all time after the usage of a branch command. The most common delay slot is the MIPS Command "nop". nop is not the only way you can put the delay slot here is an example of another way.
bne:
Branchs if the value in two certain registers don't equal. Branch lines can be commonly reffered to a check point. Example:
beq:
Branchs if the value in two certain registers equal. Branch lines can be commonly reffered to a check point. Example:
Jump Commands:
*Important jump command rule: Just like the important branch rule at all time their must be a delay slot after the jump command line. For more info reffer to the Branch Command Rule Section.
jr:
The jr command is very simple. It jumps to the address stored in a specific register. Example:
j:
This jumps to a specified address. Yes very simple. Example:
jal:
This calls a subroutine into another subroutine. An example this will involve a function and more advanced mips a vague explination will come with it:
Other Commands:
addiu:
Adds a value to a register and stores it to another register. Here is a example:
nop:
No operation. Nothing empty area its value is 00000000. It is used as delay slots and to take up space. An example of both:
Registers:
$t0-$t9:
Temporary Registers hold values in them. What ever you load and store them in they are holding. You can overwrite them so be careful what you are writing into them. Here are two examples of perfect and overwriting.
Not overwriting:
Overwriting:
$ra:
This stores the address that calls the subroutines it's in. Meaning it's calling a jump command address. Example a common subroutine:
$zero:
Always has the value of zero. If something tries to overwrite this register it is ignored at stays zero. I am not going to bother adding an example for $zero it's really simple.
=========================================
Basic MIPS Intructions:
Loading Commands:
lui - Load upper immidiate
lw - Load Word
lh - Load half word
lb - Load Byte
Storing Commands:
sw - Store Word
sh - Store Halfword
sb - Store Byte
Branch Commands:
bne - Branch on not equal
beq - Branch on equal
Jump Commands:
jr - Jump Register
j - Jump
jal - Jump and link
Other Commands:
addiu - add immidiate unsigned
nop - No operation
Registers:
$t0-$t9 - Temporary Registers
$ra - Return address
$zero($0) - zero
How to use the MIPS:
Loading Commands:
lui:
Loads 32-Bits of a code and shifts it 16-Bits to the left. Meaning what it does is loads the first half of a code. it looks like this when you use it :
- Code:
lui t0, $00000880
lw:
Load the value in the a register to another register. Meaning it's loading the value of the address/pointer in the register you tell it to.It looks like this when you use it:
- Code:
lui t0, $0880
lw t1, $3FFC(t0)
lh:
Loads 4 numbers in a register and loads it back to another register. Pretty much just loading the address. It looks like this when you use it:
- Code:
lui t0, $0880
lh t1, $3FFC(t0)
lb:
Loading a byte into a register and loads it into another register. Looks like this when youuse it:
- Code:
lui t0, $0880
lb t1, $3FFC(t0)
Storing Commands:
sw:
Stores 32-bits of the value in a register and stores it in another register. Example:
- Code:
lui t0, $0880
lui t1, $1337
addiu t1, t1, $1337
sw t1, $3FFC(t0)
sh:
Stores 16-bit of value in a register into another . Example:
- Code:
lui t0, $0880
lui t1, $1337
addiu t1, t1, $1337
sh t1, $3FFC(t0)
sb:
Stores 8-Bits of code in a register and stores it into another register. Example:
- Code:
lui t0, $0880
lui t1, $1337
addiu t1, t1, $1337
sb t1, $3FFC(t0)
Branch Commands:
*Important Branch Command Rule: Their must be a delay slot at all time after the usage of a branch command. The most common delay slot is the MIPS Command "nop". nop is not the only way you can put the delay slot here is an example of another way.
- Code:
lui t0, $1337
lui t1, $0880
lw t2, $3FF0(t1)
bne t2, t0, $To jr ra line
sw t2, $3FF4(t1)//Delay Slot
jr ra
sw t0, $3FF0(t2) //Delay Slot
bne:
Branchs if the value in two certain registers don't equal. Branch lines can be commonly reffered to a check point. Example:
- Code:
lui t0, $0880
lw t0, $3FFC(t0)
addiu t1, zero, $0001
bne t0, t1, $To jr ra line
nop //Delay Slot
jr ra
nop //Delay Slot
beq:
Branchs if the value in two certain registers equal. Branch lines can be commonly reffered to a check point. Example:
- Code:
lui t0, $0880
lw t1, $3FFC(t0)
lw t2, $1880(t0)
beq t1, t2, $To jr ra line
nop //Delay Slot
lw t3, $3FFC(t0)
addiu t4, t3, $0001
sw t4 $3FFC(t0)
jr ra
nop //Delay Slot
Jump Commands:
*Important jump command rule: Just like the important branch rule at all time their must be a delay slot after the jump command line. For more info reffer to the Branch Command Rule Section.
jr:
The jr command is very simple. It jumps to the address stored in a specific register. Example:
- Code:
lui t0, $0880
lw t1, $1880(t0)
lw t2, $1000(t0)
jr t1
nop //Delay Slot
j:
This jumps to a specified address. Yes very simple. Example:
- Code:
j $To Segment 1
lui t0, $0880 //Segment 1
lui t1, $1337
addiu t1, t1, $1337
sw t1 $3FFC(t0)
jr ra // Jumps to return address. The return address here is the j line
jal:
This calls a subroutine into another subroutine. An example this will involve a function and more advanced mips a vague explination will come with it:
- Code:
addiu sp, sp, $FFF0//Subtracts from the stack
sw ra, $0000(sp) //Segment 1
sw s0, $0004(sp) //Segment 2
sw s1, $0008(sp) //Segment 3
lui t0, $0880
lw t0, $3FFC(t0)
jal $08801880 //Random address
sw t0 $3FFC(t0)
lw s1, $0008(sp) //Segment 1.1
lw s0, $0004(sp) //Segment 2.1
lw ra, $0000(sp) //Segment 3.1
jr ra
nop //Delay Slot
Other Commands:
addiu:
Adds a value to a register and stores it to another register. Here is a example:
- Code:
lui t0, $0880
lw t0, $3FF8(t0)
addiu t1, t0, $0004
nop:
No operation. Nothing empty area its value is 00000000. It is used as delay slots and to take up space. An example of both:
- Code:
nop
nop
nop
nop
nop
nop
nop //All the nop above taking up space
lui t0, $0880
lw t1, $3FFC(t0)
lw t2, $3FF8(t0)
bne t1, t2, $To Jr Ra line
nop //Delay Slot
sw t2 $3FF4(t0)
jr ra
nop //Delay Slot
Registers:
$t0-$t9:
Temporary Registers hold values in them. What ever you load and store them in they are holding. You can overwrite them so be careful what you are writing into them. Here are two examples of perfect and overwriting.
Not overwriting:
- Code:
lui t0, $0880
lw t1 $3FFC(t0)
lw t2, $3FF8(t0)
Overwriting:
- Code:
lui t0, $0880
lw t1, $3FFC(t0)
lw t1, $1880(t0)
$ra:
This stores the address that calls the subroutines it's in. Meaning it's calling a jump command address. Example a common subroutine:
- Code:
j $Segment 1 //Now becomes the $ra in the subroutine
lui t0, $0880
lui t1, $1337
addiu t1, t1, $1337
sw t1, $3FFC(t0)
jr $ra //It's jumping to the address in the register i this case it's ra
$zero:
Always has the value of zero. If something tries to overwrite this register it is ignored at stays zero. I am not going to bother adding an example for $zero it's really simple.
=========================================
- Code:
Lesson 2: Learning Basic MIPS Instructons and how to use them
Created by: FoodFx