Professional Documents
Culture Documents
Lecture Note 03
Lecture Note 03
• Number conversions
• Signed numbers
1
Number Systems
2
Number Systems
• When working with assembler, we need to be
able to interpret data storage directly; i.e., we
need to be able to translate from binary to
something we can understand.
• Unfortunately, humans don’t work well with
binary numbers.
• Converting those binary numbers to decimal
numbers won’t work well, either.
3
Number Systems
• So, we often ask the computer to display
computer storage as hexadecimal (base 16)
numbers instead, because
– the conversion from binary to hex is relatively simple,
and
– hex numbers are easier to use than binary numbers --
contrast
0001 1111 0100 0011 0010 0111 1010 1001
with 1F4327A9
4
5
The Decimal (Base 10) System
• How do we know the value of a decimal
number?
13210
(2 x 100) = (2 x 1 ) = 2
(3 x 101) = (3 x 10) = 30
(1 x 102) = (1 x 100) = 100
__________
132
6
Binary (Base 2)
to Decimal (Base 10)
• Binary allowed digits = 0, 1. Example 1:
101102
(0 x 20) = (0 x 1) = 0
(1 x 21) = (1 x 2) = 2
(1 x 22) = (1 x 4) = 4
(0 x 23) = (0 x 8) = 0
(1 x 24)
_____________
= (1 x 16) = 16
22
7
Binary (Base 2)
to Decimal (Base 10)
• Binary allowed digits = 0, 1. Example 2:
10010112
( 1 x 20) =
(1 x 1) = 1
( 1 x 21) = (1 x 2) = 2
( 0 x 22) = (0 x 4) = 0
( 1 x 23) = (1 x 8) = 8
( 0 x 24) = (0 x 16) = 0
( 0 x 25) = (0 x 32) = 0
( 1 x 26) = (1 x 64) = 64
_____________
75
8
Hexadecimal (Base 16)
to Decimal (Base 10)
• Hex allowed digits = 0 - 9, A - F. Example 1:
1 D 2 16
( 2 x 160) = ( 2 x 1) = 2
( 13 x 161) = ( 13 x 16) = 208
( 1 x 162) = ( 1 x 256) = 256
_________
466
2 4 1 3 E16
( 14 x 160) =
( 14 x 1) = 14
( 3 x 161) = ( 3 x 16) = 48
( 1 x 162) = ( 1 x 256) = 256
( 4 x 163) = ( 4 x 4096) = 16,384
( 2 x 164) = ( 2 x 65,536) = 131,072
____________________
147,774
11
Decimal to Binary
• Algorithm: divide by two, continue to save the
remainders as the answer. Example 1: 10910
109 / 2 = 54 + r1
54 / 2 = 27 + r0
27 / 2 = 13 + r1
13 / 2 = 6 + r1
6/2 = 3 + r0
3/2 = 1 + r1
1/2 = 0 + r1
1 1 0 1 1 0 12
12
Decimal to Binary
• Example 2: 8510
85 / 2 = 42 + r1
42 / 2 = 21 + r0
21 / 2 = 10 + r1
10 / 2 = 5 + r0
5/2 = 2 + r1
2/2 = 1 + r0
1/2 = 0 + r1
1 0 1 0 1 0 12
13
Decimal to Hexadecimal
• Algorithm: divide by 16, continue to save the remainders
as the answer. Example: 495610
4956 / 16 = 309 + r12
309 / 16 = 19 + r5
19 / 16 = 1 + r3
1 / 16 = 0 + r1
1 3 5 C16
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
14
Binary to Hex
15
Binary to Hex
11010112
1.Separate into groups of four, 1. 110 10112
starting on the right.
2.Add zeros on left to make a 2. 0110 1011
group of four digits.
3.Convert each group of four 3. 6 B
to a single hex digit.
4.Merge the numbers into the 4. 6B16
final number.
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 16
Hex to Binary
17
Hex to Binary
6B16
1.Separate the hex digits. 1. 6 B
19
Conversion Summary
• Binary to hexadecimal:
– Separate into groups of four binary digits, starting on right.
– Fill in zeros on left.
– Convert each group of four into a single hex digit.
• Hex to binary:
– Convert each hex digit into four binary digits.
– Drop leading zeros and squash together.
20
Adding and Subtracting in
Binary
• So, in binary,
• 0+0=0 1 + 1 = 0 + carry
1.
• 1+0=1 1 + 1 + 1 = 1 + carry 1
21
Binary Addition Examples
1 1 1 1 0 0 0 carries 1111111111
1011101 1010101111
+111010 +1111111111
10010111 11010101110
22
Binary Subtraction Examples
02 0 2 borrows 02
1011101 1010101111
-111010 -1000011111
100011 0010010000
23
Signed Numbers in Binary
• So far we have discussed storing only positive
numbers in binary/hex.
• What about negative numbers? With only
“off” and “on”, can’t store actual plus or
minus signs in the number.
• Need to be able to
– identify negative numbers.
– perform arithmetic on them.
24
Signed Numbers in Binary
• Leftmost bit is the sign bit: 0 if positive, 1 if
negative. But, not that simple…
• Bad example using only 4 bits, with leftmost bit as
sign bit:
1 0001
+-5 +1101
-4 not equal 1110 = -6
• Oops.
25
Signed Numbers in Binary
26
Sign bit;
Signed Numbers in Binary not used for
determining the
number itself.
• Step 2: Find the two’s complement.
– Add 1 to the one’s complement.
– So, 510 01012 1010 + 1 1011
27
Signed Numbers in Binary
• Try our example, but store the negative number as
a two’s complement.
1 0001
+-5 +1011 (two’s complement)
-4 1100
equal (negative sign bit)
0011+ 1 (two’s comp.)
0100
(absolute value)
(-) 4
28
Signed Numbers in Binary
29
Shortcut to
Two’s Complement in Hex
30
Shortcut to
Two’s Complement in Hex
1. So 123C EDC3
2. Add 1 to the result: 0 F
EDC4 1 E
2 D
Check it in binary: 3 C
1 2 3 C
0001 0010 0011 1100 4 B
1110 1101 1100 0011 5 A
1110 1101 1100 0100
E D C 4 6 9
7 8
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
31