Download presentation
Presentation is loading. Please wait.
1
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2, 3 … 9 Base 2 (binary) - Numbers are represented using 2 numerals: 0, 1 Base 10 - Each position represents a power of 10: 101 = 1*10 2 + 0*10 1 + 1*10 0 = 100 + 1 110 = 1*10 2 + 1*10 1 + 0*10 0 = 100 + 10 Base 2 - Each position represents a power of 2: 101 b = 1*2 2 + 0*2 1 + 1*2 0 = 100 b + 1 b = 4 + 1 110 b = 1*2 2 + 1*2 1 + 0*2 0 = 100 b + 10 b = 4+2 1/15
2
Computer Structure - Computer Arithmetic Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ). d 3 d 2 d 1 d 0 = d 3 *10 3 + d 2 *10 2 + d 1 *10 1 + d 0 *10 0 9786 = 9*1000 + 7*100 + 8*10 + 6*1 = 9*10 3 + 7*10 2 + 8*10 1 + 6*10 0 Numbers in base 2 are called binary numbers, they are composed of the numerals 0 and 1. b 3 b 2 b 1 b 0 = b 3 *2 3 + b 2 *2 2 + b 1 *2 1 + b 0 *2 0 110101 = 1*32 + 1*16 + 0*8 + 1*4 + 0*2 + 1*1 = 1*2 5 + 1*2 4 + 0*2 3 + 1*2 2 + 0*2 1 + 1*2 0 1111 1111 1111 1111 = 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 2 15 + 2 14 + … + 2 1 + 2 0 = 2 n = 2 n+1 - 1 = 2 16 -1
3
Computer Structure - Computer Arithmetic Converting from Binary to Decimal Easy: Multiply each numeral by its exponent. 1001 b = 1*2 3 + 1*2 0 = 1*8 + 1*1 = 9 d 0111 b = 0*2 3 + 1*2 2 + 1*2 1 + 1*2 0 = 100 b + 10 b + 1 b = 4 + 2 + 1 = 7 110110 b = 1*2 5 + 1*2 4 + 0*2 3 + 1*2 2 + 1*2 1 = 100000 b + 10000 b + 100 b + 10 b = 32 + 16 + 4 + 2 = 54 2/15
4
Computer Structure - Computer Arithmetic Given a string that holds a 32 digit binary number called binary[32]; unsigned decimal = 0; unsigned power = ~0; //all bits are now ones for(i=31;i>=0;i--){ decimal += binary[i]*power; power = power >> 1; } The powers of 2: 2 0 = 12 1 = 22 2 = 42 3 = 8 2 4 = 162 5 = 322 6 = 642 7 = 128 2 8 = 2562 9 = 5122 10 = 1024 = 1K 2 11 = 20482 12 = 4096 2 13 = 8192 2 16 = 655362 20 = 1048576 = 1M
5
Computer Structure - Computer Arithmetic Converting From Decimal to Binary In C++ it looks like this: int res; int decimal; int power; for(n=31;n>=0;n--){ power = pow(2,n); res = decimal/power; decimal = decimal - res*power; cout << res; } 3/15
6
Computer Structure - Computer Arithmetic A Base Conversion Example 500/2 9 = 0 ; 500 - 0 = 500 4/2 3 = 0 ; 4 - 0 = 4 500/2 8 = 1 ; 500 - 256 = 244 4/2 2 = 1 ; 4 - 4 = 0 244/2 7 = 1 ; 244 - 128 = 116 0/2 1 = 0 ; 0 - 0 = 0 116/2 6 = 1 ; 116 - 64 = 52 0/2 0 = 0 ; 0 - 0 = 0 52/2 5 = 1 ; 52 - 32 = 20 20/2 4 = 1 ; 20 - 16 = 4 500 d = 111110100 b 4/15
7
Computer Structure - Computer Arithmetic Representation (יצוג) of Numbers int, long, short, char - positive numbers are stored as a binary number where the MSB (Most Significant Bit) is 0 A short is represented by 16 bits (2 bytes) 100d = 2 6 + 2 5 + 2 2 = 0000000001100100 An int is represented by 32 bits (4 bytes) 65545 d = 2 16 + 2 3 + 2 0 = 00000000000000010000000000001001 A char is represented by 8 bits (1 byte) ‘a’ = 48d = 2 5 + 2 4 = 00110000 5/15
8
Computer Structure - Computer Arithmetic Signed and Unsigned Numbers How do we distinguish (להבדיל) between them? Solution: Use 1 bit to represents the sign. The name of this scheme is called: sign and magnitude (גודל). 10001111 b = -15 00001111 b = +15 (8 bit numbers) There are several problems with this scheme: where do we put the sign? an extra step is needed to calculate the sign bit Add the magnitudes. Calculate the sign. there is both a positive and negative zero 10000000 b = -0 00000000 b = +0 6/15
9
Computer Structure - Computer Arithmetic Two's Complement New Solution: Leading 0s means positive and leading 1s means negative. 01111111 b > 0 10000000 b < 0 (8 bit numbers) The largest positive number is: 2,147,483,647 d (2 31 -1) = 01111…11111 b The smallest negative number is: -2,147,483,648 d (-2 31 ) = 10000…00000 b It is followed by -2,147,483,647 d (1000…0001 b ) up to -1 (1111…1111 b ). The sum of a number and its inverse (נגדי) is -1. 7/15
10
Computer Structure - Computer Arithmetic Negative Decimal to Binary Translate the number to binary. Flip the bits (1 -> 0, 0 -> 1) Add 1 -100 d = -01100100 = 10011011 + 1 = 10011100 11111111 b = 00000000 + 1 = -1 d -128 d = -10000000 = 01111111 + 1 = 10000000 11000101 b = 00111010 + 1 = 00111011 = -59 d In a short: -25,000 d = -0110000110101000 = 1001111001010111 + 1 = 10011110011000 b 8/15
11
Computer Structure - Computer Arithmetic Unsigned Integers Are stored as binary numbers. The MSB (sign bit) can be 1 or 0, the number is still positive. Thus in a char: 10011100 b = 2 7 + 2 4 + 2 3 + 2 2 = 156 d 11111111 b = 2 7 + … + 2 0 = 2 8 - 1 = 255 d Problem: How do we compare signed and unsigned numbers? Solution: Add instructions to compare unsigned numbers. sltu $t0,$s0,$s1 sltiu $t0,$s0,100 9/15
12
Computer Structure - Computer Arithmetic Hexadecimal Numbers Numbers in base 16 are called hexadecimal numbers, they are composed of 16 numerals (0-9,a-f). 9786 hex = 9*16 3 + 7*16 2 + 8*16 1 + 6*16 0 = = 9*4096 + 7*256 + 8*16 + 6*1 = 38790 d 0xabcdef = 10*16 5 + 11*16 4 + 12*16 3 + 13*16 2 + 14*16 1 + 15*16 0 = 11259375 d The conversion from binary to hex is very easy, each hex digit is 4 binary digits: 0x0 = 0000 0x1 = 0001 0x2 = 0010 0x3 = 0011 0x4 = 0100 0x5 = 0101 0x6 = 0110 0x7 = 0111 0x8 = 1000 0x9 = 1001 0xa = 1010 0xb = 1011 0xc = 1100 0xd = 1101 0xe = 1110 0xf = 1111 10/15
13
Computer Structure - Computer Arithmetic Binary Hexadecimal Using the previous table it is easy to represent 32 bit binary numbers in a short form: 0100 1010 0010 1101 1000 1100 0111 1001 = 4 a 2 d 8 c 7 9 = 0x4a2d8c79. 0x12390ab4 = 0001 0010 0011 1001 0000 1010 1011 0100 1 2 3 9 0 a b 4 0xffffffff = 1111 1111 1111 1111 1111 1111 1111 1111 Every 2 Hex digits is a byte 11/15
14
Computer Structure - Computer Arithmetic Conversion from decimal to hexadecimal is similar to converting decimal to binary. int res; int decimal; int power; for(n=7;n>=0;n--){ power = pow(16,n); res = decimal/power; decimal = decimal - res*power; if(res>9) cout << (char)((res -10) + 'a'); else cout << res; }
15
Computer Structure - Computer Arithmetic Addition Lets add 6 to 7: 00…00111 = 7 d 00…00110 = 6 d 00…01101 = 13 d Lets add 11 to 5: 00…01011 = 11 d 00…00101 = 5 d 00…10000 = 16 d Instructions that end with an i ( addi, subi, andi ) perform an operation between a register and an immediate: addi $t0,$t1,20 # $t0 =$t1 + 20 12/15
16
Computer Structure - Computer Arithmetic Subtraction Subtraction uses addition, the operand is simply negated before being added: 7 - 6 = 7 + (-6) = 00…00111 = 7 d 11…11010 = -6 d 00…00001 = 1 d The pseudoinstruction neg $t0,$t1 #$t0 = -$t1 is implemented by: sub $t0,$zero,$t1 #$zero is always 0 13/15
17
Computer Structure - Computer Arithmetic Overflow (גלישה) Overflow occurs when the result of a operation can't be represented by the hardware. This can happen when we add two numbers of the same sign or subtract two large numbers of opposing signs. Overflow is detected by the following table: Operation A B Result A+B >=0 >=0 =0 A- B >=0 =0 >=0 How is overflow detected in unsigned numbers? It isn't. The instructions add, addi, sub cause exceptions when overflow occurs. The instructions addu, addiu, subu ignore overflow. Guess what instructions MIPS C++ compilers use?
18
Computer Structure - Computer Arithmetic Logical Operations All processors provide logical instructions that operate on the bits of the operands. and, andi, or, ori, xor and xori perform the AND, OR and XOR (eXlusive OR) operations. If $t0 contains 10 and $t1 contains 9 then: and $t2,$t0,$t1 # $t2=10&9=8 or $t2,$t0,$t1 # $t2=10|9=11 xor $t2,$t0,$t1 # $t2=10&9=3 not $t2,$t0 # $t2=~9=-10 (8 bits) MIPS provides a NOR (Not OR) instruction as well: nor $t2,$t0,$t1 # $t2=~(10|9)=4 14/15
19
Computer Structure - Computer Arithmetic Shift Instructions shift operations move the bits in a word to the left or right, filling the emptied bits with 0s. The instructions are shift left logical ( sll ), and shift right logical ( srl ). These 2 instructions use the shamt field of the R-type instruction. sll $t0,$t1,8 # $t0 = $t1 0000 1101 0000 0000 srl $t2,$t0,3 # $t2 = $t0>>3; 0000 1101 0000 0000 -> 0000 0001 1010 0000 The instructions sllv and slrv contain the shift amount in a register. sllv $t0,$t1,$t2 # $t0=$t1>>$t2 15/15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.