CSE 341 Computer Organization Lecture 4 ISA Assembly
CSE 341 Computer Organization Lecture 4 ISA : Assembly Language 3 Prof. Lu Su Computer Science & Engineering Slides adapted from Raheel Ahmad, Luis Ceze , Sangyeun Cho, Howard Huang, Bruce Kim, Josep Torrellas, Bo Yuan, and Craig Zilles
Review the assembly convention �$t 0 is a register; �x($t 0) is a memory value; �A[] is stored in the memory; �A, b, var, result are saved in the register; �Instruction Format ◦ Destination variable first, then source variable ◦ Except sw �sw $t 1, 8($t 0) 2
Review of Control Flow Instructions l MIPS provides three control-flow instructions -- j: unconditional jumps -- bne and beq: conditional branches -- slt and slti // set if less l How to translate the high-level control operation to MIPS ASM? -- if-else-then -- for, while 3
Translate If-else v 0 = a 0; move $v 0 $a 0 if (v 0<0) bge $v 0, $0 Label v 0= sub $v 0, $v 0 v 0; Label: add $v 1, $v 0, v 1 = v 0 + $v 0 l Note here we invert the original v 0; condition 4
Can we use another method? (1) v 0 = a 0; if (v 0<0) v 0= v 0; v 1 = v 0 + v 0; move $v 0 $a 0 blt $v 0, $0 Label_1 j Label_2 Label_1: sub $v 0, $v 0 Label_2: add $v 1, $v 0 5
Can we use another method? (2) v 0 = a 0; move $v 0 $a 0 if (v 0<0) blt $v 0, $0 Label v 0= add $v 1, $v 0 v 0; Label: sub $v 0, 0, v 1 = v 0 + $v 0 v 0; Incorrect Solution! 6
Question What do the following instructions do? $s 1 #$s 1=1 7
Translate if-then-else if (v 0 < 0) v 0 --; else v 0++; v 1 = v 0; bge $v 0, $0, Label 1 sub $v 0, $s 1 #$s 1=1 j Label 2 Label 1: add $v 0, $s 1 Label 2: move $v 1, $v 0 8
Can we use another method? if (v 0 < 0) v 0 --; else v 0++; v 1 = v 0; blt $v 0, $0, Label 1 add $v 0, $s 1 j Label 2 Label 1: sub $v 0, $s 1 Label 2: move $v 1, $v 0 9
Case/Switch l Case-switch statement has multi-way branches switch (A) { case 0: break; case 1: count--; break; case 2: count++; break; default: count=0; } 10
Translate Case/Switch (1) �Step 1: convert case/switch to if-thenelse form If (A==0) break; switch (A) { else if (A==1) case 0: break; count--; case 1: count--; break; else if (A==2) case 2: count++; break; count++; default: count=0; else count=0; } 11
Translate Case/Switch (2) �Step 2: Translate if-else-then to ASM code If (A==0) break; else if (A==1) count--; else if (A==2) count++; else count=0; beq $v 0, $0, Label 1 beq $v 0, $s 1, Label 2 # $s 1=1 beq $v 0, $s 2, Label 3 # $s 2=2 move $s 0, $0 j Label_after_break Label 1: j Label_after_break Label 2: sub $s 0, $s 1 j Label_after_break Label 3: add $s 0, $s 1 j Label_after_break 12 Label_after_break: ….
Can we use another method? �Step 2: Translate if-else-then to ASM code If (A==0) break; else if (A==1) count--; else if (A==2) count++; else count=0; bne $v 0, $0, Label 1 j Label_after_break Label 1: bne $v 0, $s 1, Label 2 sub $s 0, $s 1 j Label_after_break Label 2: bne $s 0, $s 2, Label 3 add $s 0, $s 1 j Label_after_break Label 3: move $s 0, $0 j Label_after_break 13 Label_after_break: ….
Translate loop For (i=0; i<4; i++) { // stuff } move $t 0, 0 # i=0 Loop: // stuff addi $t 0, 1 # i++ blti $t 0, 4, Loop #if t 0<4, #jump to loop 14
Question (while loop) i=0; while (i<4) { // stuff i=i+1; } 15
Question (while loop) i=0; while (i<4) { // stuff i=i+1; } move $t 0, 0 # i=0 Loop: // stuff addi $t 0, 1 # i++ blti $t 0, 4, Loop #if t 0<4, #jump to loop 16
Shift Operation l. Shifts move all the bits in a word left or right sll $t 2, $s 0, 8 #$t 2 = $s 0 << 8 bits srl $t 2, $s 0, 8 #$t 2 = $s 0 >> 8 bits l“ 0” will be filled after shifting -- Shift can be used as multiply or divide 17
Shift Operation sll $t 2, $s 0, 2 #Multiply by 4 …. 0010 … 00100 #Shift left once (* by 2) … 001000 #Shift left twice(* by 4) srl $t 2, $s 0, 2 #Divide by 4 001… 0001… #Shift right once (div by 2) 00001… #Shift right twice(div by 4) 18
AND Operation
OR Operation
Logical Operations
- Slides: 21