Integer Representation for People Computer Organization and Assembly Language: Module 3
Motivation u The representation of numbers is made difficult because of the discrete nature of computer representation u Not all numbers can be represented in a computer u The chosen method of representation affects the ease by which arithmetic operations are performed
Weighted Positional Notation u Use the position of the symbol to indicate the value u By assigning each position the appropriate power of the base, we can get a unique representation of numbers in that base Decimal System (Base 10) Given a sequence of n digits d 0,d 1,…,d n-1 d n-1.10 n-1 + … + d d
Weighted Positional Notation u In general, given a sequence of n digits s 0,s 1,…,s n-1 and a base b, s n-1.b n-1 + … + s 1.b 1 + s 0.b 0 yields a unique integer N u s 0 is the least significant digit u s n-1 is the most significant digit u The most significant symbol can not be zero
Weighted Positional Notation u Any positive integer value can be used as the base or radix for a weighted positional notation u For bases less than or equal to 10, a subset of the ten decimal digits can be used binary (base 2) octal (base 8) u For bases more than 10, new symbols will have to be introduced hexadecimal (base16) a=10, b=11, c=12, d=13, e=14, f=15
Weighted Positional Notation u A subscript is used to indicate the base of the number in typeset A number without a subscript is assumed to be base 10 Not an option while programming in MAL u In MIPS assembly language, hexadecimal representation begins with 0x 0x10 (= decimal 16) u So the following instructions are identical li $v0, 0x12 li $v0, 18
Representations in Different Radices binary hexdecimalbinaryhexdecimal a b c d e f15
Binary and Hexadecimal u Discriminating between two symbols is currently cheaper in digital circuitry than three or more, thus binary is the representation for computers u Since bits can be grouped into three to obtain octal value and into four to obtain hexadecimal value, it is much easier for humans to work with octal or hexadecimal representations. Octal is no longer very common; we will focus on hexadecimal u Binary to hexadecimal conversion is more direct than binary to decimal conversion. Reason: 16 is a power of 2
Binary to Hexadecimal u To convert from binary to hexadecimal: Starting with the least significant bit, partition the bits of the binary representation into groups of 4 The most significant group may have fewer than 4 members Add zeroes to the left of the most significant group until it contains 4 bits Convert each group of four bits into the hexadecimal symbol corresponding to that bit pattern
Binary and Hexadecimal Example: Convert to hexadecimal. binary hexadecimal 1 b hence: = 1b 16
Hexadecimal to Binary u To convert from binary to hexadecimal: Convert each hexadecimal symbol to a group of 4 bits corresponding to that value Eliminate leading zeroes Example: Convert 2bc1 16 to binary. hexadecimal 2 b c 1 binary hence: 2bc1 16 =
Conversion to Decimal Given a sequence of base-r digits s n-1... s 1 s 0 convert the sequence into a decimal number N using: N = s n-1 *b n-1 + … + s 1 *b 1 + s 0 *b 0
Conversion to Decimal Convert to decimal. N = 1* * * * * *2 0 = 38 Convert 3b2 16 to decimal. N = 3* * *16 0 = 946 Convert to decimal. N = 3* * *8 0 = = 248
Decimal Binary or Hexadecimal Repeated division by 2 or 16 Example: Convert 292 to hexadecimal. 292/16 = 18 R 4 18/16 = 1 R 2 1/16 = 0 R = Conversion from Decimal When the quotient is less than 16, the process ends
Conversion from Decimal u Convert 42 to hexadecimal 42/16 = 2 R 10 (but 10 = a 16 ) 2/16 = 0 R 242 = 2a 16 u Convert 109 to binary 109/2 = 54 R 1 54/2 = 27 R 0 27/2 = 13 R 1 13/2 = 6 R 1 6/2 = 3 R 0 3/2 = 1 R 1 1/2 = 0 R 1109 =
Shortcut: convert to hexadecimal u It is sometimes easier to convert from binary to hexadecimal, then to decimal, instead of converting directly to decimal = = 3 a b 6 = 3*16^3 + 10*16^2 + 11* = 3* * = = = 14940
Shortcut: convert to hexadecimal u The converse is also true: converting from decimal to hexadecimal, then to binary generally requires far fewer steps than converting directly to binary 278/16 = 17 R 6 17/16 = 1 R 1 1/16 = 0 R = = =
Representing Numbers u Choosing an appropriate representation is a critical decision a computer designer has to make u The chosen representation must allow for efficient execution of primitive operations u For general-purpose computers, the representation must allow efficient algorithms for addition of two integers determination of additive inverse
Representing Numbers u With a sequence of N bits, there are 2 N unique representations u Each memory cell can hold N bits u The size of the memory cell determines the number of unique values that can be represented u The cost of performing operations also increases as the size of the memory cell increases u It is reasonable to select a memory cell size such that numbers that are frequently used are represented
Binary Representation u The binary, weighted positional notation is the natural way to represent non-negative numbers. u MAL numbers the bits from right to left, beginning with 0 as the least significant digit
Modulo Arithmetic u Consider the set of number {0, …,7} u Suppose all arithmetic operations were finished by taking the result modulo 8 u = 9, 9 mod 8 = 1 = 1 u 3*5 = 15, 15 mod 8 = 7 3 * 5 =
Modulo Arithmetic: Additive Inverse u What is the additive inverse of 7? u 7 + x = 0 u = 0 u 0 and 4 are their own additive inverses u Does each number also have a multiplicative inverse?