Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.

Similar presentations


Presentation on theme: "COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1."— Presentation transcript:

1 COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1. Arithmetic Instructions 2. Registers, Memory, and Addressing 3. Load/Save, Logical Operation Instructions 4. Representing MIPS as binary code 5. Instructions for making decisions Patterson: Sections 2.1 – 2.7.

2 2 Levels of Programming 1.Recall that a CPU can only understand binary machine language program 2.Writing binary machine language program is cumbersome 3.An intermediate solution is to write assembly language program that can easily be translated (assembled) to binary language programs 4.In this course we will cover MIPS ISA used by NEC, Nintendo, Silicon Graphics, and Sony 5.MIPS is more primitive than higher level languages with a very restrictive set of instructions

3 3 Fetch and Execute 1.Instructions are stored in the form of bits 2.Programs are stored in memory and are read or written just like data 3.Fetch & Execute Cycle — Instructions are fetched and put into a special register — Bits in the register "control" the subsequent actions — Data if required is fetched from the memory and placed in other registers — Fetch the “next” instruction and continue Processor Memory memory for data, programs, compilers, editors, etc.

4 4 Registers 1.Registers are memory cells 2.In MIPS, data must be in registers before arithmetic operations can be performed 3.Size of each register is 32 bits, referred to as a word (1 word = 4 bytes = 32 bits) 4.MIPS has a total of 32 registers NameRegister numberUsage $zero 0 Constant value of 0 $v0-$v1 2 - 3 Values for results and expression evaluation $a0-$a3 4 - 7 Input arguments to a procedure $t0-$t7 8 - 15 Not preserved across procedures (temp) $s0-$s7 16 - 23 Preserved across procedure calls $t8-$t9 24 - 25 More temporary registers $gp 28 Global pointer $sp 29 Stack pointer, points to last location of stack $fp 30 Frame pointer $ra 31 Return address from a procedure call

5 5 Addition & Subtraction Example: C:f = (g + h) – (i + j); MIPS Code: Step 1: Specify registers containing variables Step 2: Express instruction in MIPS MIPS code: CategoryInstructionExampleMeaningComments Arithmetic add add $s1,$s2,$s3$s1 ← $s2+$s3 overflow detected subtract sub $s1,$s2,$s3$s1 ← $s2-$s3 overflow detected jihg$s0 - $s7 $s6$s7$s5$s4$s3$s2$s1$s0 finali+jg+h $t0 - $t7 $t8$t7$t6$t9$t5$t4$t3$t2$t1$t0 add $t0,$s1,$s2# $t0 ← $s1 + $s2 add $t1,$s3,$s4# $t1 ← $s3 + $s4 sub $t2,$t0,$t1# $t2 ← $t0 - $t1

6 6 Memory Organization 1.Memory can be viewed as a large one dimensional array of cells 2.To access a cell, its address is required (Addresses are indices to the array) 3.In MIPS, each cell is 1 word (4 bytes) long 4.Each word in a memory has an address, which is a multiple of 4 5.Length of an address is 32 bits, hence minimum value of address = 0 maximum value of address = (2 32 – 1) 6.Data is transferred from memory into registers using data transfer instructions CategoryInstructionExampleMeaningComments Data transfer load word lw $s1,100($s2) $s1 ← memory[$s2+100] Memory to Register store word sw $s1,100($s2) memory[$s2+100] ← $s1 Register to memory

7 7 Data Transfer Instructions CategoryInstructionExampleMeaningComments Data transfer load wordlw $s1,100($s2) $s1 ← memory[$s2+100] Memory to Register store wordsw $s1,100($s2) memory[$s2+100] ← $s2 Register to memory 100 10 101 1 Data A[k] … A[1] A[0] Array address address + 4 … Address + 4xk Example: C instruction: g = h + A[k] Register Allocation: $s1 contains computed value of g; $s2 contains value of h $s3 contains base address of array (address of A[0]) $s4 contains value of k; MIPS Code: add $t1,$s4,$s4# $t1 = 2 x k add $t1,$t1,$t1# $t1 = 4 x k add $t1,$t1,$s3# $t1 = address of A[0] + 4 x k lw $t0,0($t1)# $t0 = A[k] Add $s1,$s2,$t0# $s3 = h + A[k]

8 8 So far we have learned … MIPS — loading words but addressing bytes — addition and subtraction operations on registers only InstructionsMeaning add $s1,$s2,$s3# $s1 = $s2 + $s3 (arithmetic) sub $s1,$s2,$s3# $s1 = $s2 – $s3 (arithmetic) lw $s1,100($s2)# $s1 = Memory[$s2+100] (data transfer) sw $s1,100($s2)# Memory[$s2+100] = $s1 (data transfer) Activity 1: Write the MIPS assembly code for the following C assignment instruction A[12] = h + A[8] assuming that the variable h is stored in $s2 and the base address of the array A is in $s3.

9 9 Binary Representation 1.A computer can only process bits. 2.Characters, integers, and real numbers must be represented in bits. 3.Different representation are labeled with a subscript. — A decimal number is represented by subscript. — Binary numbers are represented by subscript. — Hexadecimal numbers are represented by subscript. Examples: 987 ten, 1011010 two, and 987 hex Case 1: Decimal to Binary Conversion Example: Convert 445 ten into 32-bit binary Binary Representation: 445 ten = 0000 0000 0000 0000 0000 0001 1011 1101 two Case 2: Binary to Decimal Conversion Example: Convert 0000 0000 0000 0000 0000 0001 1011 1101 two to decimal

10 10 Hexadecimal Representation (1) Case 3: Decimal to Hexadecimal Conversion Example: Convert 445 ten into hexadecimal 445 ten = 000001bd hex in 1 word Case 4: Hexadecimal to Decimal Conversion Example: Convert 000001bd hex to decimal

11 11 Hexadecimal Representation (2) Case 5: Hexadecimal to Binary Conversion Example: Convert 000001bd hex into binary Case 6: Binary to Hexadecimal Conversion Example: Convert 0000 0000 0000 0000 0000 0001 1011 1101 two to hexadecimal Activity 1: Convert 1998 ten into binary using the hexadecimal shortcut.

12 12 2’s Complement (1) 1.MIPS uses 2’s complement to represent signed numbers 2.In 2’s complement, a positive number is represented using a 31-bit binary number — Example: +2 ten is represented as 0000 0000 0000 0000 0000 0000 0000 0010 two or 00000002 hex 3.In 2’s complement, a negative number  X two is represented by taking the complement of its magnitude X two plus 1. — Example:  2 ten Represent the magnitude in binary format 2 ten is represented as 0000 0000 0000 0000 0000 0000 0000 0010 two Take the complement of each digit The results is 1111 1111 1111 1111 1111 1111 1111 1101 two Add 1 to the LSB  2 ten is represented as 1111 1111 1111 1111 1111 1111 1111 1110 two or fffffffe hex

13 13 2’s Complement (2) 4.The MSB (32 nd bit) is in indication of sign. 5.To convert a 32-bit number in 2’s complement to decimal — Example: 0000 0000 0000 0000 0000 0000 0000 0010 two is represented by 2 1111 1111 1111 1111 1111 1111 1111 1110 two is represented by

14 14 Unsigned and Signed Arithmetic MIPS has a separate format for unsigned and signed integers 1.Unsigned integers — are saved as 32-bit words — Example:Smallest unsigned integer is 00000000 hex = 0 ten Largest unsigned integer is ffffffff hex = 4,294,967,295 ten 2.Signed integers — are saved as 32-bit words in 2’s complement with the MSB reserved for sign — If MSB = 1, then the number is negative — If MSB = 0, then the number is positive — Example: Smallest signed integer: 1000 0000 0000 0000 0000 0000 0000 0000 two =  (2 31 ) 10 =  2,147,483,648 10 Largest signed integer:0111 1111 1111 1111 1111 1111 1111 1111 two = (2 31  1) 10 = 2,147,483,647 10

15 15 MIPS to Binary Machine Language (1) Example: add $t0,$s1,$s2 Binary Machine Language Equivalent: 00000010001100100100000000100000 Can we derive the binary machine language code from the MIPS instruction? MIPS field for arithmetic instructions: oprsrtrdshamtfunct 6 bits5 bits 6 bits opcode1 st operand2 nd operanddestinationshiftfunction

16 16 MIPS Fields for Arithmetic Operations For arithmetic operations (R): — Opcode (op) = 0 — Function (funct) = 32 (0x20) for add, 34 (0x22) for sub Example: add $t0,$s1,$s2 (Values of Registers: $t0 = 8, $s1 = 17, $s2 = 18) op = 0 10 = (000000) 2 rs = 17 10 = (10001) 2 rt = 18 10 = (10010) 2 rd = 8 10 = (01000) 2 shamt is not used = (00000) 2 funct = 32 10 = (100000) 2 leads to the binary machine language code: 000000 10001 10010 01000 00000 100000 oprsrtrdshamtfunct 6 bits5 bits 6 bits opcode1 st operand2 nd operanddestinationshiftfunction

17 17 MIPS Fields for Data Transfer Operations For data transfer operations (I): — Opcode (op) = 35 for load (lw) and 43 for save (sw) Example: lw $t0,32($s3) # (Values of Registers: $t0 = 9, $s3 = 19) op = 35 10 = (100011) 2 rs = 19 10 = (10011) 2 rt = 8 10 = (01000) 2 address = 32 10 = (0000 0000 0010 0000) 2 leads to the binary machine language code: 100011 10011 01000 0000000000100000 oprsrtaddress 6 bits5 bits 16 bits opcode1 st operand2 nd operandMemory address (offset)

18 18 Example Activity 2: Consider the C instruction A[300] = h + A[300] A. Write the equivalent MIPS code for the above C instruction assuming $t1 contains the base address of array A (i.e., address of A[0]) and $s2 contains the value of h B. Write the binary machine language code for the result in part A.

19 19 MIPS Branch Instructions for if (1) 1.Branch if equal to: beq $s1,$s2,L1# if $s1 == $s2, go to L1 2.Branch if not equal to: bne $s1,$s2,L2# if $s1 != $s2, go to L2 3.Unconditional jump: j L3# go to L3 Example: if (i == j) go to L1; f = g + h; L1: f = f – i Assume that the five variables f, g, h, i, and j are stored in the registers: $s0 to $s4 MIPS Code: beq $s3,$s4,L1# go to L1 if i == j add $s0,$s1,$s2# f = g + h L1: sub $s0,$s0,$s3# f = f - i

20 20 MIPS Branch Instructions for if (2) Example: C instructions if (i == j) f = g + h; else f = g - h; Assume that the five variables f, g, h, i, and j are stored in the registers: $s0 to $s4 MIPS Code: bne $s3,$s4,L1# go to L1 if i == j add $s0,$s1,$s2# f = g + h j L2# L1: sub $s0,$s1,$s2# f = g - h L2: Activity 3: Write the above code using “branch if equal to” statement?

21 21 Loops (for) Example: C instructions Loop: g = g + A[i] i = i + j; if (i != h) goto Loop; Assume A is an array of 100 elements and that the compiler associates the variables g, h, i, and j are stored in the registers: $s1 to $s4. The base address of the array is contained in $s5. MIPS Code: Loop: add $t1,$s3,$s3#$t1 = 2 × i add $t1,$t1,$t1#$t1 = 4 × i add $t1,$t1,$s5#$t1 = address of A[i] lw $t0,0($t1)#$t0 = A[i] add $s3,$s3,$s4#$s3 = i + j bne $s3,$s2,Loop#go to Loop if (i!=h)

22 22 Loops (while) Example: C instructions while (save[i] == k) i = i + j; Assume that the variables i, j, and k are stored in the registers: $s3, $s4, and $s5. The base address of the array (save) is contained in $s6. What is the MIPS code? MIPS Code: Loop: add $t1,$s3,$s3#$t1 = 2 x i add $t1,$t1,$t1#$t1 = 4 x 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#$s3 = i + j j Loop#go to Loop Exit:

23 23 Loops (case/switch) Example: C instructions 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; } Assume that the variables f through k are stored in the registers: $s0 to $s5. The register $t2 contains 4. What is the MIPS code? To write the MIPS Code for the above code, 2 additional MIPS instructions are introduced 1. Set it less than: slt $t0,$s3,$s4# if ($s3<$s4) $t0=1; else $t0=0; 2. Jump register jr $s3# jump to address contained in $s3

24 24 Loops (case/switch) 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; } 1.f to k stored $s0 to $s5 2.$t2 = 4 3.$t4 contains address of an array, jumptable jumbtable[3]Address of L3 jumptable[2]Address of L2 jumptable[1]Address of L1 jumptable[0]Address of L0 slt $t3,$s5,$zero#test if k<0 bne $t3,$zero,Exit#if k<0, go to Exit slt $t3,$s5,$t2#test if k<4 beq $t3,$zero,Exit#if k >=4, go to Exit add $t1,$s5,$s5#$t1 = 2 * k add $t1,$t1,$t1#$t1 = 4 * k add $t1,$t1,$t4#$t1 = &jumptable[k] lw $t0,0($t1)#$t0 = jumbtable[k] jr $t0 L0: add $s0,$s3,$s4#$s0 = (i + j) j Exit L1: add $s0,$s1,$s2#$s0 = (g + h) j Exit L2: sub $s0,$s1,$s2#$s0 = (g - h) j Exit L3: add $s0,$s3,$s4#$s0 = (i - j) j Exit Exit:

25 25 Summary CategoryInstructionExampleMeaningComments Arithmetic add add $s1,$s2,$s3$s1 ← $s2+$s3 subtract sub $s1,$s2,$s3$s1 ← $s2-$s3 Data Transfer load word lw $s1,100($s2)$s1 ← Mem[$s2+100] store word lw $s1,100($s2)Mem[$s2+100] ← $s1 Conditional branch branch on equal beq $s1,$s2,Lif($s1==$s2) go to L branch not equal bne $s1,$s2,Lif($s1!=$s2) go to L set on less than slt $s1,$s2,$s3 if($s2<$s3) $s1 = 1 else $s1 = 0 Unconditional jump jump j 2500go to (4 x 2500) Jump register jr $rago to $ra NameExampleComments 32 Registers $s0,$s1,…$s7, $zero $t0,$t1,…$t9 $s0-$s7 are preserved across procedures, $t0-$t9 are not preserved Memory w/ 2 30 words Memory[0], Memory[4], … Memory[4294967292] Memory is accessed one word at a time


Download ppt "COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1."

Similar presentations


Ads by Google