Download presentation
Presentation is loading. Please wait.
Published byMaurice Golden Modified over 9 years ago
1
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU
2
C. ErgunCMPE325 CH #3Slide #2 Positive Binary Numbers Computers use binary numbers (base 2) Example (6 bit number) 0 1 0 1 1 0 = 0 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 1 2 1 + 0 2 0 = 0 + 16 + 0 + 4 + 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
3
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 10110.
4
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
5
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… 00000 0000101111... 111111111010000... Still two zeros 0x00000000 = +0 ten 0xFFFFFFFF = -0 ten Arithmetic not too hard Negative Numbers
6
C. ErgunCMPE325 CH #3Slide #6 Negative Numbers
7
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 -2 31 + d 30 x 2 30 +... + d 2 x 2 2 + d 1 x 2 1 + d 0 x 2 0 Example 1111 1111 1111 1111 1111 1111 1111 1100 two = 1x-2 31 +1x2 30 +1x2 29 +... +1x2 2 +0x2 1 +0x2 0 = -2 31 + 2 30 + 2 29 +... + 2 2 + 0 + 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
8
C. ErgunCMPE325 CH #3Slide #8 Two’s Complement Example Consider the same example as before with a 1 in the MSB 1 1 0 = -1 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 1 2 1 + 0 2 0 = -32 + 16 + 0 + 4 + 2 + 0 = -10 Notice that the result was not -22!
9
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 1 0 1 1 0 0 0 1 0 0 1 + 1 ------------- 0 0 1 0 1 0 = 1 0 ten The original value was -10 and the new value is 10. Explanation: x + x’ ≡ -1 x’ + 1 = -x x=-4 : 1111 1111 1111 1111 1111 1111 1111 1100two x’ : 0000 0000 0000 0000 0000 0000 0000 0011two x’ + 1: 0000 0000 0000 0000 0000 0000 0000 0100two invert: 1111 1111 1111 1111 1111 1111 1111 1011two add 1 : 1111 1111 1111 1111 1111 1111 1111 1100two
10
C. ErgunCMPE325 CH #3Slide #10 Two’s Complement 0000 0001 0010 1111 1110 1000 01111001 0 1 2 -2 -7 -8 7 0011 0100 0101 0110 1010 1011 1101 3 4 5 6 -3 -4 -5 -6 0-8-6-4-22468 1100
11
C. ErgunCMPE325 CH #3Slide #11 Two’s Complement in 8bits More common: use of 2's complement negatives have one additional number 0000 0000 = 0 0000 0001 = 1 … 0111 1110 = 126 0111 1111 = 127 1000 0000 = -128 1000 0001 = -127... 1111 1101 = -3 All negative numbers 1111 1110 = -2 have a '1' in the 1111 1111 = -1 highest position
12
C. ErgunCMPE325 CH #3Slide #12 Two’s Complement in MIPS 0000... 0000 0000 0000 0000 two = 0 ten 0000... 0000 0000 0000 0001 two = 1 ten 0000... 0000 0000 0000 0010 two = 2 ten... 0111... 1111 1111 1111 1101 two = 2,147,483,645 ten 0111... 1111 1111 1111 1110 two = 2,147,483,646 ten 0111... 1111 1111 1111 1111 two = 2,147,483,647 ten 1000... 0000 0000 0000 0000 two = –2,147,483,648 ten 1000... 0000 0000 0000 0001 two = –2,147,483,647 ten 1000... 0000 0000 0000 0010 two = –2,147,483,646 ten... 1111... 1111 1111 1111 1101 two =–3 ten 1111... 1111 1111 1111 1110 two =–2 ten 1111... 1111 1111 1111 1111 two =–1 ten
13
C. ErgunCMPE325 CH #3Slide #13 Two’s Complement in MIPS
14
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
15
C. ErgunCMPE325 CH #3Slide #15 Sign Extension Sign extended value No sign extension 101010101 8 bits 001010100 8 bits 10101010 8 bits 0010101000000000 8 bits 00000000
16
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
17
C. ErgunCMPE325 CH #3Slide #17 Number formats
18
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)
19
C. ErgunCMPE325 CH #3Slide #19 Hexadecimal Table
20
C. ErgunCMPE325 CH #3Slide #20 Hexadecimal Example Consider the example 1111 0010 0110 1011 F 2 6 B
21
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 111 100 100 110 7 4 4 6
22
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 = 4 0 0 0 1 1 0 = 6 ten 0 1 1 0 0 0 = 24 ten
23
C. ErgunCMPE325 CH #3Slide #23 Addition & subtraction
24
C. ErgunCMPE325 CH #3Slide #24 Addition & subtraction
25
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
26
C. ErgunCMPE325 CH #3Slide #26 Overflow The difference of two numbers can exceed any representation 2's complement: Numbers change sign and size
27
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
28
C. ErgunCMPE325 CH #3Slide #28 How to Overcome:
29
C. ErgunCMPE325 CH #3Slide #29 How to Overcome:
30
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 Bits655556 First Source Register Second Source Register Result Register Shift Amount Function Code
31
C. ErgunCMPE325 CH #3Slide #31 Shift Operations
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.