Instruction Format
Instruction Format
Instruction Representation
S4 : 2023/2024
Review
• ISA: hardware / software interface
– Design principles, tradeoffs
• MIPS instructions
– Arithmetic: add/sub $t0, $s0, $s1
– Data transfer: lw/sw $t1, 8($s1)
• Operands must be registers
– 32 32-bit registers
– $t0 - $t7 => $8 - $15
– $s0 - $s7 => $16 - $23
• Memory: large, single dimension array of bytes M[232]
– Memory address is an index into that array of bytes
– Aligned words: M[0], M[4], M[8], ….M[4,294,967,292]
– Big/little endian byte order
Machine Language -- MIPS
• All instructions have the same length (32 bits)
• Good design demands good compromises
– Same instruction length or same format
• Three different formats
– R: arithmetic instruction format
– I: transfer, branch, immediate format
– J: jump instruction format
A[300] = h + A[300];
• add $t0, $s1, $s2
lw $t0, 1200($t1)
– 32 bits in machine language add $t0, $s2, $t0
– Fields for: sw $t0, 1200($t1)
– Operation (add) 10101101001010000000010010110000
• Operands ($s1, $s2, $t0) 00000010010010000100000000100000
10001101001010000000010010110000
Instruction Formats
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
R: op rs rt rd shamt funct
I: op rs rt address / immediate
J: op target address
sub $t1, $s1, $s2 (sub $9, $17, $18 # $9 = $17 - $18)
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
0 17 18 9 0 34
Compiler
lw $t0, 1200($t1) # temporary register $t0 gets A[300]
add $t0, $s2, $t0 # temporary register $t0 gets h +A[300]
sw $t0, 1200($t1) # stores h + A[300] back into A[300]
Assembler
35 9 8 1200
0 18 8 8 0 32
43 9 8 1200
Zero Fill
0000 0000 0001 0010 0011 0100 0101 0110
Multiplication and Division
• Use special purpose registers (hi, lo)
– 32-bit value x 32-bit value = 64-bit value
• Mult $s0, $s1 000000 10000 10001 00000 00000 011000
– hi: upper half of product
– lo: lower half of product
• Div $s0, $s1 000000 10000 10001 00000 00000 011010
– hi: remainder ($s0 / $s1)
– lo: quotient ($s0 % $s1)
• Move results into general purpose registers:
– mfhi $s0 000000 00000 00000 10000 00000 010000
– mflo $s1 000000 00000 00000 10001 00000 010010
Assembly vs. Machine Language
• Assembly provides convenient symbolic representation
– Much easier than writing numbers
– Destination operand first
– Pseudo instructions
– Labels to identify and name words that hold instructions/data
• Machine language is the underlying reality
– Destination operand is no longer first
– Efficient format
• Assembly can provide pseudo instructions
– Move $t0, $t1 (add $t0, $t1, $zero)
• When considering performance (IC) you should count real
instructions
Register Conventions
Basic Block
While Loop
while (save[i] = = k)
i = i +j;
# i: $s3; j: $s4; k: $s5; base of save: $s6
Loop: add $t1, $s3, $s3 # $t1 = 2 * i
add $t1, $t1, $t1 # $t1 = 4 * i
add $t1, $t1, $s6 # $t1 = address of save[i]
lw $t0, 0($t1) # $t0 = save[i]
bne $t0, $s5, Exit # go to Exit if save[i] != k
add $s3, $s3, $s4 # i = i +j
j Loop # go to Loop
Exit: