Chapter Two MIPS Arithmetic Computer Organization
Review Binary Representation Used for all data and instructions Fixed size values: 8, 16, 32, 64 Hexadecimal Sign extension Base and virtual machines. pseudo instructions MIPS Arithmetic 2
Registers Registers special storage locations built into the CPU for faster access. characteristics: 32 user accessible registers. each store 32-bits. each has a symbolic name. some registers have a special purpose. MIPS Arithmetic 3
Registers Some of the more commonly used registers include. Name Usage $zero constant 0. $s0 - $s7 general purpose temporary (saved). $t0 - $t9 general purpose temporary. $a0 - $a3 passing arguments to functions. $v0 - $v1 return values from functions. MIPS Arithmetic 4
Integer Arithmetic All computers must be able to perform arithmetic. In C/C++ + - * / % (/) Integer division if both operands are integers. MIPS Arithmetic 5
MIPS Arithmetic Have three operands. All must be registers. Example: C/C++, Java (assume vars a c are in $s0 - $2) int a, b, c; a = b + c; MIPS add $s0, $s1, $s2 MIPS Arithmetic 6
MIPS Arithmetic Derived from two of the principles: simplicity favors regularity smaller is faster This simplicity comes at a price. a = b + c - d; Requires multiple MIPS instructions. add $t0, $s1, $s2 # t = b + c sub $s0, $t0, $s3 # a = t - d MIPS Arithmetic 7
Arithmetic Example 1 Convert the following C segment to MIPS a = b + c; d = e + f - a; Answer: (a f stored in $s0...$s5) add $s0, $s1, $s2 # a = b + c add $t0, $s4, $s5 # t = e + f sub $s3, $t0, $s0 # d = t - a MIPS Arithmetic 8
Arithmetic Example 2 Convert the following C segment to MIPS x = u + (y z) + (v - w); Answer: (u z stored in $s0...$s5) sub $t0, $s4, $s5 # t0 = y z sub $t1, $s1, $s2 # t1 = v w add $t2, $s0, $t0 # t2 = u + t0 add $s3, $t2, $t1 # x = t2 + t1 MIPS Arithmetic 9
Arithmetic Example 3 Convert the following C segment to MIPS a = b + (d e); b = b - (c + f) a; Answer: MIPS Arithmetic 10
Immediate Operands Constants are very common in programs. a = b - 1; b++; c = x[i+1]; Principle 3: make the common case fast. MIPS provides some immediate instructions. addi $s0, $s1, -1 addi $s1, $s1, 1 MIPS Arithmetic 11
Immediate Operations Machine instructions (non-pseudo) 16-bit constant With sign-extension Consider value -7 16-bits: 1111111111111001 32-bits: 11111111111111111111111111111001 MIPS Arithmetic 12
Arithmetic Integer Operations Instruction Description add rd, rs, rt add: rd rs + rt addi rd, rs, imm add immediate: rd rs + imm sub rd, rs, rt subtract: rd rs + rt mul rd, rs, rt multiply: rd rs * rt div rd, rs, rt divide: rd rs / rt rem rd, rs, rt modulo: rd rs % rt abs rd, rs absolute value: rd rs neg rd, rs negation: rd - rs MIPS Arithmetic 13
neg Implementation How is the neg pseudo instruction implemented? neg $t0, $t1 # $t0 = -$t1 By subtraction sub $t0, $zero, $t1 # $t0 = 0 - $t1 MIPS Arithmetic 14
Load Immediate What about constants in non-immediate instructions. c = b * 5 We can use load immediate (pseudo). li $t2, 5 mul $t0, $t1, $t2 MIPS Arithmetic 15
Load Immediate How can we implement the li instruction? li $t2, 58 Break it into two conditions: up to 15-bits larger than 15-bits For small constants: addi $t2, $zero, 58 MIPS Arithmetic 16
Large Constants What about constants larger than 15-bits? li $t0, 0x4F287 $t0: 0004F287 MIPS Arithmetic 17
Move Operation We also have a move instruction (pseudo). move $t3, $t2 # $t3 $t2 How is it implemented? MIPS Arithmetic 18
Mixed Sizes Consider the following code segment int a, b; short c; char d; a = b + c * d; MIPS Arithmetic 19
Unsigned Values Consider the following code segment int w; unsigned int x; unsigned short y; unsigned char z; w = x + y * z; MIPS Arithmetic 20
Unsigned Arithmetic Operations In MIPS, the only difference between the signed and unsigned operations Whether to signal when an overflow occurs. These do not signal an overflow Instruction addu rd, rs, rt addiu rd, rs, imm subu rd, rs, rt Description add: rd rs + rt add immediate: rd rs + imm subtract: rd rs + rt MIPS Arithmetic 21
Overflow When the proper result of an arithmetic operation can not be represented with the alloted number of bits. What happens? Programming language Operating system Programmer MIPS Arithmetic 22
Overflow Consider the following operation of 4-bits: 0110 + 1101 ------ 10011 Does overflow occur? MIPS Arithmetic 23
Overflow When does overflow occur with addition and subtraction? Operation Operand A Operand B Overflow Occurs for Results A + B >= 0 >= 0 < 0 A+B < 0 < 0 >= 0 A - B >= 0 < 0 < 0 A - B < 0 >= 0 >= 0 MIPS Arithmetic 24