250 likes | 266 Views
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
E N D
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.
Levels of Programming • Recall that a CPU can only understand binary machine language program • Writing binary machine language program is cumbersome • An intermediate solution is to write assembly language program that can easily be translated (assembled) to binary language programs • In this course we will cover MIPS ISA used by NEC, Nintendo, Silicon Graphics, and Sony • MIPS is more primitive than higher level languages with a very restrictive set of instructions
Processor memory for data, programs, compilers, editors, etc. Memory Fetch and Execute • Instructions are stored in the form of bits • Programs are stored in memory and are read or written just like data • 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
Registers Registers are memory cells In MIPS, data must be in registers before arithmetic operations can be performed Size of each register is 32 bits, referred to as a word (1 word = 4 bytes = 32 bits) MIPS has a total of 32 registers Name Register number Usage 0 Constant value of 0 $zero 2 - 3 Values for results and expression evaluation $v0-$v1 4 - 7 Input arguments to a procedure $a0-$a3 8 - 15 Not preserved across procedures (temp) $t0-$t7 16 - 23 Preserved across procedure calls $s0-$s7 24 - 25 More temporary registers $t8-$t9 28 Global pointer $gp 29 Stack pointer, points to last location of stack $sp 30 Frame pointer $fp 31 Return address from a procedure call $ra
$s0 $s1 $s2 $s3 $s4 $s5 $s6 $s7 $s0 - $s7 g h i j $t0 $t1 $t2 $t3 $t4 $t5 $t6 $t7 $t8 $t9 $t0 - $t7 g+h i+j final 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: add $t0,$s1,$s2 # $t0 ← $s1 + $s2 add $t1,$s3,$s4 # $t1 ← $s3 + $s4 sub $t2,$t0,$t1 # $t2 ← $t0 - $t1
Memory Organization Memory can be viewed as a large one dimensional array of cells To access a cell, its address is required(Addresses are indices to the array) In MIPS, each cell is 1 word (4 bytes) long Each word in a memory has an address, which is a multiple of 4 Length of an address is 32 bits, hence minimum value of address = 0maximum value of address = (232 – 1) Data is transferred from memory into registers using data transfer instructions
Address + 4xk A[k] 1 0 0 … … 1 0 address + 4 A[1] 1 0 1 address A[0] 1 Array D a t a Data Transfer Instructions 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]
So far we have learned … MIPS — loading words but addressing bytes — addition and subtraction operations on registers only InstructionsMeaningadd $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.
Binary Representation • A computer can only process bits. • Characters, integers, and real numbers must be represented in bits. • Different representation are labeled with a subscript. • A decimal number is represented by subscript <ten>. • Binary numbers are represented by subscript <two>. • Hexadecimal numbers are represented by subscript <hex>. Examples: 987ten, 1011010two, and 987hex Case 1: Decimal to Binary Conversion Example: Convert 445ten into 32-bit binary Binary Representation: 445ten = 0000 0000 0000 0000 0000 0001 1011 1101two Case 2: Binary to Decimal Conversion Example: Convert 0000 0000 0000 0000 0000 0001 1011 1101two to decimal
Hexadecimal Representation (1) Case 3: Decimal to Hexadecimal Conversion Example: Convert 445ten into hexadecimal 445ten = 000001bdhex in 1 word Case 4: Hexadecimal to Decimal Conversion Example: Convert 000001bdhex to decimal
Hexadecimal Representation (2) Case 5: Hexadecimal to Binary Conversion Example: Convert 000001bdhex into binary Case 6: Binary to Hexadecimal Conversion Example: Convert 0000 0000 0000 0000 0000 0001 1011 1101two to hexadecimal Activity 1: Convert 1998ten into binary using the hexadecimal shortcut.
2’s Complement (1) • MIPS uses 2’s complement to represent signed numbers • In 2’s complement, a positive number is represented using a 31-bit binary number • Example: +2ten is represented as 0000 0000 0000 0000 0000 0000 0000 0010two or 00000002hex • In 2’s complement, a negative number -Xtwo is represented by taking the complement of its magnitude Xtwo plus 1. • Example: -2ten Represent the magnitude in binary format 2ten is represented as 0000 0000 0000 0000 0000 0000 0000 0010two Take the complement of each digit The results is 1111 1111 1111 1111 1111 1111 1111 1101two Add 1 to the LSB -2ten is represented as 1111 1111 1111 1111 1111 1111 1111 1110two or fffffffehex
2’s Complement (2) • The MSB (32nd bit) is in indication of sign. • To convert a 32-bit number in 2’s complement to decimal • Example: 0000 0000 0000 0000 0000 0000 0000 0010two is represented by 2 1111 1111 1111 1111 1111 1111 1111 1110two is represented by
Unsigned and Signed Arithmetic MIPS has a separate format for unsigned and signed integers • Unsigned integers • are saved as 32-bit words • Example: Smallest unsigned integer is 00000000hex = 0ten Largest unsigned integer is ffffffffhex = 4,294,967,295ten • 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 0000two = -(231)10= -2,147,483,64810 Largest signed integer:0111 1111 1111 1111 1111 1111 1111 1111two =(231 - 1)10= 2,147,483,64710
MIPS to Binary Machine Language (1) • Example:add $t0,$s1,$s2 • Binary Machine Language Equivalent: • 000000 10001 10010 01000 00000 100000 • Can we derive the binary machine language code from the MIPS instruction? • MIPS field for arithmetic instructions:
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 = 010 = (000000)2 • rs = 1710 = (10001)2 • rt = 1810 = (10010)2 • rd = 810 = (01000)2 • shamt is not used = (00000)2 • funct = 3210 = (100000)2 • leads to the binary machine language code: 000000 10001 10010 01000 00000 100000
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 = 3510 = (100011)2 • rs = 1910 = (10011)2 • rt = 810 = (01000)2 • address = 3210 = (0000 0000 0010 0000)2 • leads to the binary machine language code: 100011 10011 01000 0000000000100000
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.
MIPS Branch Instructions for if (1) • Branch if equal to: beq $s1,$s2,L1 # if $s1 == $s2, go to L1 • Branch if not equal to: bne $s1,$s2,L2 # if $s1 != $s2, go to L2 • 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
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?
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)
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:
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
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: 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; } f to k stored $s0 to $s5 $t2 = 4 $t4 contains address of an array, jumptable