CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU
C. ErgunCMPE325 CH #3Slide #2 Positive Binary Numbers Computers use binary numbers (base 2) Example (6 bit number) = 0 2 0 = = 22 Each digit is d × Base i where i = 0 at the right and increases going to the left (same process as in base 10) Does not make sense to store binary numbers in ASCII since each character requires 1 byte
C. ErgunCMPE325 CH #3Slide #3 Converting to Binary To convert a number to base 2, continue to divide by 2, keeping the remainder at each step Consider the example 22 going the other direction 22 ÷ 2 = 11 remainder 0LSB 11 ÷ 2 = 5 remainder 1 5 ÷ 2 = 2 remainder 1 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1MSB So the binary number is
C. ErgunCMPE325 CH #3Slide #4 Negative Numbers But, how can negative values be represented? The most obvious answer is to add one additional bit called the sign bit, used to indicate a positive or negative value Where does the sign bit go? Left or right? Arithmetic with signed numbers require extra step Both a positive and negative 0 While possible to overcome, there is a much better way
C. ErgunCMPE325 CH #3Slide #5 The second alternative is to replace all 0’s with 1’s and use a sign bit that is part of the magnitude (ones-complement) Arithmetic still requires extra hardware to do a subtract Both a positive and negative 0 Symmetric positive and negative numbers Again, while additional hardware can solve the problem, a better method does not… Still two zeros 0x = +0 ten 0xFFFFFFFF = -0 ten Arithmetic not too hard Negative Numbers
C. ErgunCMPE325 CH #3Slide #6 Negative Numbers
C. ErgunCMPE325 CH #3Slide #7 Two’s Complement Can represent positive and negative numbers by first bit (MSB) as –2 31 position, then positive 2 n : d 31 x d 30 x d 2 x d 1 x d 0 x 2 0 Example two = 1x x x x2 2 +0x2 1 +0x2 0 = = -2,147,483,648 ten + 2,147,483,644 ten = -4 ten Note! Must specify width to find MSB => 32bits is used in MIPS, so d 31 is MSB
C. ErgunCMPE325 CH #3Slide #8 Two’s Complement Example Consider the same example as before with a 1 in the MSB = -1 2 0 = = -10 Notice that the result was not -22!
C. ErgunCMPE325 CH #3Slide #9 Two’s Complement Shortcut A simpler way is to convert between positive and negative values (goes both ways) Reverse every bit (01 and 10) Add 1 to the resulting number Consider our previous example again = 1 0 ten The original value was -10 and the new value is 10. Explanation: x + x’ ≡ -1 x’ + 1 = -x x=-4 : two x’ : two x’ + 1: two invert: two add 1 : two
C. ErgunCMPE325 CH #3Slide #10 Two’s Complement
C. ErgunCMPE325 CH #3Slide #11 Two’s Complement in 8bits More common: use of 2's complement negatives have one additional number = = 1 … = = = = = -3 All negative numbers = -2 have a '1' in the = -1 highest position
C. ErgunCMPE325 CH #3Slide #12 Two’s Complement in MIPS two = 0 ten two = 1 ten two = 2 ten two = 2,147,483,645 ten two = 2,147,483,646 ten two = 2,147,483,647 ten two = –2,147,483,648 ten two = –2,147,483,647 ten two = –2,147,483,646 ten two =–3 ten two =–2 ten two =–1 ten
C. ErgunCMPE325 CH #3Slide #13 Two’s Complement in MIPS
C. ErgunCMPE325 CH #3Slide #14 Understanding Signed Ops Programmers can explicitly use unsigned data values (such as unsigned int ) Require unsigned operations such as sltu and sltiu Signed instructions help determine whether values are considered to have a sign bit For instance, lbu is for load byte unsigned One byte is copied from memory into a register The high order 24 bits are filled with the value 0 The lb instruction is signed The value is then sign extended, meaning that the sign bit from the first byte is repeated into the high order bits of the word Immediate values remain sign extended
C. ErgunCMPE325 CH #3Slide #15 Sign Extension Sign extended value No sign extension bits bits bits bits
C. ErgunCMPE325 CH #3Slide #16 Number formats Different compare operations required for both number types Signed integer slt Set an less than slti Set on less than immediate Unsigned integer sltu Set an less than sltiu Set on less than immediate
C. ErgunCMPE325 CH #3Slide #17 Number formats
C. ErgunCMPE325 CH #3Slide #18 Hexadecimal Hexadecimal is base 16, so digits are 0-9, A-F Since most values are multiples of 4 bits, hexadecimal is a popular way of representing numbers (commonly written 0xnnnnn) Easy to convert binary to hexadecimal by breaking to blocks of 4 bits (2 4 is 16 values)
C. ErgunCMPE325 CH #3Slide #19 Hexadecimal Table
C. ErgunCMPE325 CH #3Slide #20 Hexadecimal Example Consider the example F 2 6 B
C. ErgunCMPE325 CH #3Slide #21 Octal Octal is base 8 and appears occasionally, though not used as frequently Binary to octal is grouped into 3 bits Example
C. ErgunCMPE325 CH #3Slide #22 Shifting Bits The sll and srl instruction were mentioned before in passing, but its purpose should now make more sense sll $t0, $t1, 2# Shift bits left twice srl $t0, $t1, 2# Shift bits right twice Shifting a value to the left twice is the same as multiplying its value by 2 2 = = 6 ten = 24 ten
C. ErgunCMPE325 CH #3Slide #23 Addition & subtraction
C. ErgunCMPE325 CH #3Slide #24 Addition & subtraction
C. ErgunCMPE325 CH #3Slide #25 Overflow Overflow is when a number gets too large to fit The left most bit is not the same as the infinite number of bits to the left of it Can happen with both positive and negative values Handling overflow is the responsibility of the programmer
C. ErgunCMPE325 CH #3Slide #26 Overflow The difference of two numbers can exceed any representation 2's complement: Numbers change sign and size
C. ErgunCMPE325 CH #3Slide #27 Detecting Overflow Overflow can not occur when adding numbers with different signs or subtracting numbers of the same sign (reverses of each other) Adding numbers of the same sign or subtracting numbers of different signs, however, can cause overflow Addition overflowOverflow conds Sign of operands the same, and Sign of result not the same Subtraction overflow Sign of operands different, and Sign of result different from sign of A A – B = C A Positive and B negative then C should be positive A Negative and B positive then C should be negative
C. ErgunCMPE325 CH #3Slide #28 How to Overcome:
C. ErgunCMPE325 CH #3Slide #29 How to Overcome:
C. ErgunCMPE325 CH #3Slide #30 The sll / srl Instructions When encoded, the sll is an arithmetic R- format instruction that uses the shamt field OP=00 rt rdshamtfunc Bits First Source Register Second Source Register Result Register Shift Amount Function Code
C. ErgunCMPE325 CH #3Slide #31 Shift Operations