MIPS: Memory and Branch

Memory


Organisation

The main memory is a one dimension array with each location of the memory with an address where
k-bit address = 2^k locations

e.g 4 bit  = 16 location
0000 to 1111

Each memory slot (Byte) is 8 bits



Transfer unit

Byte addressable
Word is 2^n bytes 
However, machines can only be one of each addressable and not both

The transfer unit is the size of the transferable item between the processor and the memory. Commonly, the size if the same as the register size, the integer size and instruction size in most architecture

MIPs Memory instruction
It has 32 register which is 32 bit long (4 bytes)
Each word contains 32 bits
Memory address are 32 bits
There are 2^30 words because each word is 4 bytes thus there are 2^32 memory slots (Byte)

Mip uses byte addressable so consecutive words differ by 4.

Load word

Load word from memory

lw $t10 , 4($s0)
4 is a offset,
this is saying that we take the memory stored in $s0 and add 4
let says $s0 contains 4000 memory
then the value at memory 4004 will be loaded in $t10

Store word

sw $t0, 12($s0)

Lets say $t0 contains 0xFFF, when run run this, the value is stored into $s0 + 12 memory
Offset is calculated by index * size of element

The offsets represents the number of bytes offsetted

There is load byte and store byte as well. We will retrieve and load one byte into the memory.

char is use for Byte.

Word Alignment

Word alignment is important because mips likes to read only multi - byte values from memory at an address that's a multiple of the data size
They begin at a byte address thats a multiple of the number of bytes in the word.
E.g 
if a word consists of 4 bytes, it can start from 0 or 4 or 8.
Between the ranges are unaligned bytes

Mips don't allow us to load/store any unaligned using lw/sw
It will throw hardware exception
We can use ulw and usw instead

Register is 32 bits

Byte vs Word

Address of the next word is not the current word + 1
If we have a code:
int temp
temp = v[k]
v[k+1] = temp

How do we get the address at v[k]?
1. Find offset
k*itemsize
sll $2 , $5, 2
where 2 is actually 2^2
and we are multiplying it to k (the index) which is stored in $5
(Remember that sll is like multiplication)
Note: Address movement move in increments of 4 for integer, 8 for double, 1 character

2. Add it to the the other register $4 where $4 point points to the address of the array
add $2, $4 , $2

3. Load the value into temp which is $15
lw $15 , 0($2) //to get index k
lw $16, 4($2) // to get index k+1

We are assuming that the value is an index

We have to do the addition because the syntax dont allow us to load word without a constant

Branch

We use branch to create loop so run a instruction multiple time.
We keep checking the condition and loop to a certain part of the code when its fufil

Conditional Branch

beq $r1,$r2, L1
if $r1 equals to $r2, go to the label l1
if(a==b) goto l1

bne $r1,$r2,l1
If its not equal

Unconditional Branching

j l1
Just jump straight into the label

Technique: Invert the condition for shorter code

If- Else
e.g code
if(i==j){
   f = g+ h
}
else{
   f =  g - h
}

1.  If i! = j, jump to else
2. continue

1. If i==j, continue
2. Jump before reaching else

Inequality

There is no branch greater than or lesser than but we have slt (Set on less than) or slti
slt $t , $s1, $s2 //only compares less than
Slt will compare s1 and s2, 
if s1<s2, t will be 1
else 0

BLT (Branch less than)
We want to check if s1<s2

slt $t0 , $s1 , $s2 
beq $zero, $t0 , false #if its !(s1<s2), jump away

BLE (Branch lesser or Equal)
 We want to check if s1<= s2, we realised that s1<=s2 is contrapositive to s2>s1

slt $t0, $s2, $s1 #Returns 1 if s2>s1
bne $zero, $t0, false #if returns s2>s1, jump away


BGT (Branch Greater than)
We want s1>s2

slt $t0, $s2, $1 #return 1 if s2< s1
beq $zero, $t0, false #if !(s2 < s1), jump away

BGE (Branch Greater or Equal)
We want to check if s1>= s2, we realised that it is a contrapositive to s1<s2

slt $t0, $s1, $s2 #return 1 if s1< s2
bne $zero, $t0, false #if its s1< s2 , jump away




Array and Loop

We want to access array in a loop in a sum
1.


<Prev               Next>