---Constants--- For small constants (values between -32768 and 32767), we can use I-Type instructions as the value will fit in the immediate field. Remember that this field is signed (2's complement). The format for I-Type instructions is [op-6bits][rs-5bits][rt-5bits][imm-16bits] Examples: C int x = 20; MIPS addi $s0, $zero, 20 Machine Code 001000 00000 10000 0000 0000 0001 0100 C int y = x | 301; MIPS andi $s1, $s0, 301 Machine Code 001100 10000 10001 0000 0001 0010 1101 For large constants (values greater than 32767 or smaller than -32678), we need to first load the value into a register before we can operate on it. This is a two step process that uses instructions lui (load upper immediate) and ori (bitwise OR immediate). Examples: C (One of the following, they all do the same thing) int y = 4000000; //base 10 int y = 0b00000000001111010000100100000000; //base 2, accepted by most compilers int y = 0x003d0900; //base 16 MIPS (Both of the following required to recreate the C step) lui $s1, 61 #upper half of the base 2 number, converted to decimal ori $s1, $s1, 2304 #lower half of the base 2 number, converted to decimal Machine Code 001111 00000 10001 0000000000111101 001101 10001 10001 0000100100000000 Step-by-Step: lui step: s1 = 61 << 16 s1 = 0000 0000 0011 1101 0000 0000 0000 0000 #logical shift places 0s in lower half ori step: s1 = s1 | 2304 #bitwise or 0000 0000 0011 1101 0000 0000 0000 0000 | 0000 0000 0000 0000 0000 1001 0000 0000 = 0000 0000 0011 1101 0000 1001 0000 0000 What about math? C int z = x + 4000000; MIPS lui $at, 61 ori $at, $at, 2304 add $s2, $s0, $at ---Addressing Modes--- MEMORY IS BYTE ADDRESSED AND WORD ALIGNED (this is why you see x4 or <<2 on address calculations) From slide 105 bne $t0, $s5, 2 #branch to 2 instructions further than the next instr PC = PC + 4 always add 4 to PC IF !t0 == s5 also add "branch target address" PC = PC + 2 << 2 PC = 80012 + 4 + 2 << 2 PC = 80012 + 4 + 8 PC = 80012 + 12 PC = 80024