MIPS Assembly Tutorial
MIPS Assembly Tutorial
Types of Instructions
There are 3 main types of assembly instructions
Arithmetic - add, sub, mul, shifts, and, or, etc. Load/store Conditional - branches
Arithmetic Instructions
add a, b, c add a, a, d add a, a, e a = b+c a = d+a = d+b+c a = e+a = e+d+b+c
Solution:
add a, b, c sub d, a, e
Arithmetic Instructions
Example: Translate the following instructions to assembly code. Remember with RISC, only 1 operation per instruction! HINT - you may need temporary variables
f = (g+h) - (i+j)
Solution:
Operands
In assembly code, you cant use variables such as a, b, c, etc In RISC instruction sets, operands must be registers such as r1, r2, r3, etc
r0 is typically reserved to hold the immediate value of 0 There is a limited number of registers
MIPS has 32
Variables are stored in memory and then loaded into registers when needed using data transfer instructions
Load word (lw) and store word (sw)
LW Example
Example: Assume that A is an array of 100 words. Assume the base address is stored in r3, g is in r1 and h is in r2 g = h + A[8]
Solution:
Offset
Base Address
Data in Memory
All variables/data are stored in memory
You will need to do this in your assembler Your ISS will need a data structure to hold main memory
Array is fine
Addressing Data
Architecture usually addresses data in bytes (byte addressable)
32-bit architecture = 4 bytes = 1 word
lw/sw load/store 1 word or 4 bytes
Data in Memory
. . . 12 8 4 0 Address
. . .
100 10 101 1 Data
LW/SW Example
Example: Assume that A is an array of 100 words. Assume the base address is stored in r3 and h is stored in r2. You may directly calculate the offset. Remember, each data piece is 4 bytes when calculating the offset A[12] = h+A[8] Solution: lw r1, 32(r3) add r4, r2, r1 sw r4, 48(r3)
LW/SW Example
Example: Assume that A is an array of 100 words. Assume the base address is stored in r3 and g, h, and i are in r1, r2, and r4 respectively. Calculate the offset using assembly instructions but dont use multiplication yet (mult instruction is different) g = h + A[i] Solution: add r5, r4, r4 add r5, r5, r5 add r5, r5, r3 lw r6, 0(r5) add r1, r6, r2 # Temp reg r5=2*i # Temp reg r5=4*i # t1 = addr of A[i] (4*i+r3) # Temp reg r0=a[i] # g=h+a[i]
Solution:
0 0
decimal
1 2 0 32
binary
000000
6 bits
00000
5 bits
00001
5 bits
00010
5 bits
00000
5 bits
100000
6 bits
MIPS Fields
MIPS fields are giving names to make them easier to discuss
op
6 bits
rs
5 bits
rt
5 bits
rd
5 bits
shamt
5 bits
funct
6 bits
op: Basic operation of the instruction, typically called the opcode rs: The first register source operand rt: The second register source operand rd: The register destination operand, it gets the result of the operation shamt: Shift amount (0 if not shift instruction) funct: Function. This field selects the specific variant of the operation in the op field, and is sometimes called the function code
MIPS Fields
Problem occurs with an instruction needs a longer field than that showed on the previous slide
I.e. LW must specify 2 registers and a constant. Limited to 5-bit constant if use previous format.
MIPS Fields
op
6 bits
rs
5 bits
rt
5 bits
address
16 bits
I-type (I-format)
I=immediate Now LW can specify an address up to 16bits
MIPS Asm -> Machine lw r0, 1200(r1) add r0, r2, r0 Language sw r0, 1200(r1)
Solution:
decimal
op rs rt rd Address /shamt funct
35 0 43
0 0 0
1 2 1 0
1200 0 1200 32
binary
100011 000000 101011 00000 00000 00000 00001 00010 00001 0000 0100 1011 0000 00000 00000 32 0000 0100 1011 0000
Decision Instructions
Branch/jump instructions
Conditional branches
beq register1, register2, Label bne register1, register2, Label
Unconditional branches
j Label
Decision Instructions
Example: Assume f->r0, g->r1, h->r2, i->r3, j->r4 if ( i==j ) goto L1 f = g+h f = f-i
L1:
Solution: beq r3, r4, L1 add r0, r1, r2 sub r0, r0, r3
Labels will need to be translated to instruction address in your assembler
L1:
Decision Instructions
Example: Assume f->r0, g->r1, h->r2, i->r3, j->r4 if ( i==j ) f = g+h else f = g-h
L1: L2:
Decision Instructions
Example: A is 100 elements with the base address in r5. g->r1, h->r2, i->r3, j->r4 Loop: g = g+A[i] i = i+j if ( i!=h ) goto Loop Solution:
Loop: add r6, r3, r3 add r6, r6, r6 add r6, r6, r5 lw r7, 0(r6) add r1, r1, r7 add r3, r3, r4 bne r3, r2, Loop
While Loop
Goto statements are bad, just used them as an example. You will want to use while loops
Or for loops but I am just showing you while loops
While Loop
Example: Base address of save is in r6. i->r3, j->r4, k->r5 while ( save[i] == k ) i = i+j Solution:
Loop: add r1, r3, r4 add r1, r1, r1 add r1, r1, r6 lw r0, 0(r1) bne r0, r5, Exit add r3, r3, r4 j Loop
Exit:
Immediate Operands
Example: What is the machine code for the following? (Remember the I-format instruction) addi r4, r4, 4
Solution:
decimal
op rs rt Immediate
binary
001000 00100 00100 0000 0000 0000 0100