CDA 3101 Spring 2020 Introduction to Computer Organization






![Example A[300] = h + A[300]; /* $t 1 <= base of array A; Example A[300] = h + A[300]; /* $t 1 <= base of array A;](https://slidetodoc.com/presentation_image/f50cf92c8605e3bfd7fcf7545d407ff8/image-7.jpg)











![Loops Loop: g = g + A[i]; i = i + j; if (i Loops Loop: g = g + A[i]; i = i + j; if (i](https://slidetodoc.com/presentation_image/f50cf92c8605e3bfd7fcf7545d407ff8/image-19.jpg)
![While Loop while (save[i] = = k) i = i +j; # i: $s While Loop while (save[i] = = k) i = i +j; # i: $s](https://slidetodoc.com/presentation_image/f50cf92c8605e3bfd7fcf7545d407ff8/image-20.jpg)

![Do-While Loop do { g = g + A[i]; i = i + j; Do-While Loop do { g = g + A[i]; i = i + j;](https://slidetodoc.com/presentation_image/f50cf92c8605e3bfd7fcf7545d407ff8/image-22.jpg)





- Slides: 27
CDA 3101 Spring 2020 Introduction to Computer Organization Instruction Representation Decision Instructions 21, 23 January 2020
Review • ISA: hardware / software interface – Design principles, tradeoffs • MIPS instructions – Arithmetic: add/sub $t 0, $s 1 – Data transfer: lw/sw $t 1, 8($s 1) • Operands must be registers – 32 32 -bit registers – $t 0 - $t 7 => $8 - $15 – $s 0 - $s 7 => $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) • DP 6: Good design demands good compromises – Same instruction length or same format • MIPS Instructions: Three different formats – R: arithmetic instruction format – I: transfer, branch, immediate format – J: jump instruction format • add $t 0, $s 1, $s 2 – 32 bits in machine language – Fields for: – Operation (add) • Operands ($s 1, $s 2, $t 0) A[300] = h + A[300]; lw $t 0, 1200($t 1) add $t 0, $s 2, $t 0 sw $t 0, 1200($t 1) 101011010000000010010110000 101011 000000100100100000000100000 100011010000000010010110000
Instruction Formats 6 bits 5 bits 6 bits R: op rs rt rd shamt funct I: op rs rt J: op address / immediate target address op: basic operation of the instruction (opcode) rs: first source operand register rt: second source operand register rd: destination operand register shamt: shift amount funct: selects the specific variant of the opcode (function code) address: offset for load/store instructions (+/-215) immediate: constants for immediate instructions
R Format add $t 0, $s 1, $s 2 (add $8, $17, $18 # $8 = $17 + $18) 6 bits 5 bits 6 bits 0 17 18 8 0 32 000000 10001 10010 01000 00000 100000 sub $t 1, $s 2 (sub $9, $17, $18 # $9 = $17 - $18) 6 bits 5 bits 6 bits 0 17 18 9 0 34 000000 10001 10010 01001 00000 100010
I Format lw $t 0, 52($s 3) lw $8, 52($19) 6 bits 5 bits 35 19 8 16 bits 52 100011 10011 01000 0000 0011 0100 sw $t 0, 52($s 3) sw $8, 52($19) 6 bits 5 bits 43 19 8 16 bits 52 101011 10011 01000 0000 0011 0100
Example A[300] = h + A[300]; /* $t 1 <= base of array A; $s 2 <= h */ Compiler lw $t 0, 1200($t 1) add $t 0, $s 2, $t 0 sw $t 0, 1200($t 1) # temporary register $t 0 gets A[300] # temporary register $t 0 gets h +A[300] # stores h + A[300] back into A[300] Assembler 35 0 43 9 18 9 8 8 8 100011 000000 101011 010010 01001 01000 8 1200 0 1200 32 0000 0100 1011 0000 01000 00000 100000 0100 1011 0000
Immediates (Numerical Constants) • Small constants are used frequently (50% of operands) – A = A + 5; – C = C – 1; • Solutions – Put typical constants in memory and load them – Create hardwired registers (e. g. $0 or $zero) • DP 4: Make the Common Case Fast • MIPS instructions for constants (I format) – addi $t 0, $s 7, 4 8 001000 23 10111 # $t 0 = $s 7 + 4 8 01000 4 0000 0100
Arithmetic Overflow • Computers have limited arithmetic precision (32 bits) 15 +3 18 1111 0011 10010 • Some languages detect overflow (Ada), some don’t (C) • MIPS provides 2 types of arithmetic instructions: – Add, sub, and addi: cause overflow – Addu, subu, and addiu: (Unsigned) do not cause overflow • MIPS C compilers produce addu, subu, addiu by default
Logical Instructions • Bitwise operations – View contents of registers as 32 bits rather than as a single 32 -bit number • MIPS Instructions – and, or: the 3 operands are registers (R format) – andi, ori: the 3 rd argument is an immediate (I format) • Example: masking (andi $t 0, 0 x. FFF) B I T W I S E 1011 0110 1010 0100 0011 1101 1010 0000 0000 1111 0000 0000 1101 1010 B I T W I S E
Shift Instructions • Move all the bits in a register to the left | right – sll (shift left logical): fills emptied bits with 0 s – srl (shift right logical): fills emptied bits with 0 s – sra (shift right arithmetic): sign-extends emptied bits • Example: srl $t 0, $s 1, 8 (R format) shamt 000000 10001 01000 0001 0010 0011 0100 0101 0110 0111 0100 Zero Fill 0000 0001 0010 0011 0100 0101 0110 000010
Multiplication and Division • Use special purpose registers (hi, lo) – 32 -bit value x 32 -bit value = 64 -bit value • Mult $s 0, $s 1 000000 10001 00000 011000 – hi: upper half of product – lo: lower half of product • Div $s 0, $s 1 000000 10001 00000 011010 – hi: remainder ($s 0 / $s 1) – lo: quotient ($s 0 % $s 1) • Move results into general purpose registers: – mfhi $s 0 – mflo $s 1 000000 10000 0100000 00000 10001 00000 010010
Assembly vs. Machine Language • Assembly provides convenient symbolic representation – – Much easier than writing binary 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 language can provide pseudo instructions – Move $t 0, $t 1 (add $t 0, $t 1, $zero) • When considering performance in terms of instruction count (IC) you should count real (not pseudo) instructions
Register Conventions Name $zero $at $v 0 -$v 1 $a 0 -$a 3 $t 0 -$t 7 $s 0 -$s 7 $t 8 -$t 9 $k 0 -$k 1 $gp $sp $fp $ra Register Number 0 1 2 -3 4 -7 8 -15 16 -23 24 -25 26 -27 28 29 30 31 Usage the constant value 0 reserved for the assembler value for results and expressions arguments (procedures/functions) temporaries saved more temporaries reserved for the operating system global pointer stack pointer frame pointer return address Preserved on call n. a. no yes no n. a. yes yes
New Topic – Decision Instructions • Conditional branches – If-then-else • Loops – While – Do while – For • Inequalities • Switch statement
Conditional Branches • Decision-Making Instructions • Branch if equal – beq register 1, register 2, destination_address • Branch if not equal – bne register 1, register 2, destination_address • Example: beq $s 3, $s 4, 20 6 bits 5 bits 4 19 20 16 bits 5 000100 10011 10100 0000 0101
Labels • No need to calculate addresses for branches f g h i j if (i = = j) go to L 1; f = g + h; L 1: f = f – i; (4000) beq $s 3, $s 4, L 1 => => => $s 0 $s 1 $s 2 $s 3 $s 4 # if i equals j go to L 1 (4004) add $s 0, $s 1, $s 2 # f = g + h L 1: (4008) sub $s 0, $s 3 # f = f - i L 1 corresponds to the address of the subtract instruction
If Statements if (condition) clause 1; else clause 2; if (condition) goto L 1; clause 2; goto L 2; L 1: clause 1; L 2: if (i = = j) f = g + h; else f = g - h; beq $3, $4, True sub $0, $s 1, $s 2 j False True: add $s 0, $s 1, $s 2 False:
Loops Loop: g = g + A[i]; i = i + j; if (i != h) goto Loop; g: $s 1 h: $s 2 i: $s 3 j: $s 4 Base of A: $s 5 Clever method of multiplying by 4 to get byte offset for one word Loop: add $t 1, $s 3 # $t 1 = 2 * i add $t 1, $t 1 # $t 1 = 4 * I add $t 1, $5 # $t 1=address of A[i] lw $t 0, 0($t 1) # $t 0 = A[i] add $s 1, $t 0 # g = g + A[i] add $s 3, $s 4 # i = i + j bne $s 3, $s 2, Loop # go to Loop if i != h Basic Block
While Loop while (save[i] = = k) i = i +j; # i: $s 3; j: $s 4; k: $s 5; base of save: $s 6 Loop: add $t 1, $s 3 add $t 1, $t 1 add $t 1, $s 6 lw $t 0, 0($t 1) bne $t 0, $s 5, Exit add $s 3, $s 4 j Loop Exit: # $t 1 = 2 * i # $t 1 = 4 * i # $t 1 = address of save[i] # $t 0 = save[i] # go to Exit if save[i] != k # i = i +j # go to Loop Number of instructions executed if save[i + m * j] does not equal k for m = 10 and does equal k for 0 £ m £ 9 is 10 ´ 7 + 5 = 75
Optimization Loop Partially Unrolled add $t 1, $s 3 add $t 1, $t 1 # Temp reg $t 1 = 2 * i # Temp reg $t 1 = 4*i add $t 1, $s 6 # $t 1 = address of save[i] lw $t 0, 0($t 1) # Temp reg $t 0 = save[i] bne $t 0, $s 5, Exit # go to Exit if save[i] ¹ k Loop: add $s 3, $s 4 #i=i+j 6 add $t 1, $s 3 # Temp reg $t 1 = 2 * i add $t 1, $t 1 # Temp reg $t 1 = Instr’s 4*i add $t 1, $s 6 # $t 1 = address of save[i] lw $t 0, 0($t 1) # Temp reg $t 0 = save[i] beq $t 0, $s 5, Loop # go to Loop if save[i] = k Exit: The number of instructions executed by this new form of the loop is 5 + 10 ´ 6 = 65 Efficiency = 1. 15 = 75/65. If 4 ´ i is computed before the loop, then further efficiency in the loop body is possible.
Do-While Loop do { g = g + A[i]; i = i + j; } while (i != h); g: $s 1 h: $s 2 i: $s 3 j: $s 4 Base of A: $s 5 Rewrite L 1: g = g + A[i]; i = i + j; if (i != h) goto L 1: sll $t 1, $s 3, 2 add $t 1, $s 5 lw $t 1, 0($t 1) add $s 1, $t 1 add $s 3, $s 4 bne $s 3, $s 2, L 1 # $t 1 = 4*i # $t 1 = addr of A # $t 1 = A[i] # g = g + A[i] # i =i+j # go to L 1 if i != h • The conditional branch is the key to decision making
Inequalities • Programs need to test < and > • Set on less than instruction • slt register 1, register 2, register 3 – register 1 = (register 2 < register 3)? 1 : 0; • Example: if (g < h) goto Less; g: $s 0 h: $s 1 slt $t 0, $s 1 bne $t 0, $0, Less • slti: useful in for loops if (g >= 1) goto Loop slti $t 0, $s 0, 1 beq $t 0, $0, Loop # $t 0 = 1 if g < 1 # goto Loop if g >= 1 • Unsigned versions: sltu and sltiu
Relative Conditions • == != < <= > >= • MIPS does not support directly the last four • Compilers use slt, beq, bne, $zero, and $at • Pseudoinstructions • blt $t 1, $t 2, L # if ($t 1 < $t 2) go to L • ble $t 1, $t 2, L # if ($t 1 <= $t 2) go to L • bgt $t 1, $t 2, L # if ($t 1 > $t 2) go to L • bge $t 1, $t 2, L # slt $at, $t 1, $t 2 bne $at, $zero, L slt $at, $t 2, $t 1 beq $at, $zero, L slt $at, $t 2, $t 1 bne $at, $zero, L slt $at, $t 1, $t 2 if ($t 1 >= $t 2) go to L beq $at, $zero, L
The C Switch Statement switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } if (k==0) f = i + j; else if (k==1) f = g + h; else if (k==2) f = g - h; else if (k==3) f = i - j; # f: $s 0; g: $s 1; h: $s 2; i: $s 3; j: $s 4; k: $s 5 bne $s 5, $0, L 1 add $s 0, $s 3, $s 4 j Exit L 1: addi $t 0, $s 5, -1 bne $t 0, $0, L 2 add $s 0, $s 1, $s 2 j Exit L 2: addi $t 0, $s 5, -2 bne $t 0, $0, L 3 sub $s 0, $s 1, $s 2 j Exit L 3: addi $t 0, $s 5, -3 bne $t 0, $0, Exit sub $s 0, $s 3, $s 4 Exit: # branch k != 0 #f=i+j # end of case # $t 0 = k - 1 # branch k != 1 #f=g+h # end of case # $t 0 = k - 2 # branch k != 2 #f=g-h # end of case # $t 0 = k - 3 # branch k != 3 #f=i-j
Jump Tables • Jump register instruction - jr <register> - unconditional branch to address contained in register Jump Table L 0 L 1 L 2 L 3 # f: $s 0; g: $s 1; h: $s 2; i: $s 3; j: $s 4; k: $s 5 # $t 2 = 4; $t 4 = base address of JT slt $t 3, $s 5, $zero # test k < 0 bne $t 3, $zero, Exit # if so, exit slt $t 3, $s 5, $t 2 # test k < 4 beq $t 3, $zero, Exit # if so, exit add $t 1, $s 5, $5 # $t 1 = 2*k add $t 1, $t 1 # $t 1 = 4*k add $t 1, $t 4 # $t 1 = &JT[k] lw $t 0, 0($t 1) # $t 0 = JT[k] jr $t 0 # jump register L 0: add $s 0, $s 3, $s 4 # k == 0 j Exit # break L 1: add $s 0, $1, $s 2 # k == 1 j Exit # break L 2: sub $s 0, $s 1, $s 2 # k == 2 j Exit # break L 3: sub $s 0, $s 3, $s 4 # k == 3 Exit:
Conclusions • • MIPS instruction format – 32 bits Assembly: Destination = first operand Machine Language: Dest = last operand Three MIPS formats: R (arithmetic) I (immediate) J (jump) • Decision instructions – use jump (goto) • Improve Performance: loop unrolling Anticipate the Weekend