: Notes & Examples :


back...

Load Instructions Notes >

The loading process in not performed immediately.
It takes one more instruction delay to complete the loading from memory
into the general register.

Example:
Consider we want to load a sprite structure pointer from stack and then
use the pointer to set RGB brightness. Let the pointer be on sp+4.

li t6,128             // set the brightness
lw t5, 4(sp)       // get the sprite pointer
nop                    // no operation (wait until spr pointer is really in t5)
sb t6,r(t5)       // set it
sb t6,g(t5)
sb t6,b(t5)

The nop instruction is not a real machine instruction, but the processor
understands the substituted instruction code during assembly 
as do nothing. nop is very helpful if a delay is needed.
Of course we can replace the nop with other instruction to speed up
code.

Example:
Consider we use a sprite as background which is 512 pix wide.
We have to set the brightness in both sprites. Let the pointer of
spr1 be sp+10 and spr2 sp+14.

li t6,128             // set the brightness
lw t5, 10(sp)     // get spr1
lw t7, 14(sp)     // get spr2. pointer is used 3 instructions later.
sb t6,r(t5)        // set spr1
sb t6,g(t5)
sb t6,b(t5)
sb t6,r(t7)        // set spr2
sb t6,g(t7)
sb t6,b(t7)

The example above takes less time to execute as the folowing:

li t6,128             // set the brightness
lw t5, 10(sp)     // get spr1
nop                   // delay.
sb t6,r(t5)        // set spr1
sb t6,g(t5)
sb t6,b(t5)
lw t5, 14(sp)     // get spr2
nop                   // delay.
sb t6,r(t5)        // set spr2
sb t6,g(t5)
sb t6,b(t5)
 

back...

Multiply/Divide Notes

The result of a multiply or divide instruction is not presented
immediately in the HI/LO registers. The results can be accessed
after two more instruction has been passed.

Example:
li t0,10           // t0=10
li t1,100         // t1=100
multu t0,t1    // execute multiply
nop                // delay
nop                // delay
mflo t2           // t2=lo

The same delay as above should be done when using divide
instructions.

back...

Jump/Branch Notes

The branching occurs after executing the next following
intruction after the branch/jump instruction.

Example:
.
.
jal   doit
nop 
addiu t0,t0,1
.
.

When the processor executes the jump/branch instruction,
it takes another one instruction to prepare and complete
the execution of the subroutine.
The nop instruction is executed before the branching is
performed.
This is the reason why, the ra register gets always the
pc+8 address. When a function is left (jr ra) this address is
placed in pc and the program continue.

0(pc) = jal doit
4(pc) = nop
8(pc) = addiu
 

back...

Load/Store Examples >



back...