Sie sind auf Seite 1von 14

EE 308

Spring 2006

Binary, Hex and Decimal Numbers (4-bit representation)

Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

EE 308

Spring 2006

What does a number represent?

Binary numbers are a code, and represent what the programmer intends for the code. 0x72 Some possible codes: r INC (ASCII) (HC12 instruction)

2.26V (Input from A/D converter) 11410 (Unsigned number) 11410 (Signed number) Set temperature in room to 69 F Set cruise control speed to 120 mph

EE 308

Spring 2006

Binary to Unsigned Decimal:


Convert Binary to Unsigned Decimal 1111011 2 1 x 2
6

+ 1 x 2 +

+ 1 x 2 +

+ 1 x 2 +

+ 0 x 2 +

+ 1 x 2 +

+ 1 x 2 0 + 1 x 1

1 x 64 123 10

1 x 32

1 x 16

1 x 8

0 x 4

1 x 2

Hex to Unsigned Decimal


Convert Hex to Unsigned Decimal 82D6 16 8 x 16
3

+ +

2 x 16 2 x 256

+ +

13 x 16 13 x 16

+ +

6 x 16 6 x 1

8 x 4096 33494 10

EE 308

Spring 2006

Unsigned Decimal to Hex

Convert Unsigned Decimal to Hex R Decimal 1 13 2 Hex 1 D 2

Division 721/16 45/16 2/16

Q 45 2 0

721 10

2D1

16

EE 308

Spring 2006

Unsigned Number Line: Numbers go from 0 to

10 11

12

13

Unsigned Number Line


Unsigned Number Wheel: Numbers go from 0 to 2N 1

4 0

7 000 111 001

110

010

101 100 5 4

011 3

EE 308

Spring 2006

Signed Number Line: Numbers go from to

6 5 4

3 2 1 0

Number Wheel: What to do about 1002

0 0

1 000 111 001

110

010

101 100 3

011 3

EE 308

Spring 2006

Signed Number Line: Numbers go from to

6 5 4

3 2 1 0

Number Wheel: Numbers go from 2(N 1) to 2(N 1) 1

4 3 2 1 0 0 1

1 000 111 001

110

010

101 100 3 4
7

011 3

EE 308

Spring 2006

Number Wheel: Carry and Overow Carry applies to unsigned numbers when adding or subtracting, result is incorrect. Overow applies to signed numbers when adding or subtracting, result is incorrect.
0 0 1 001 1

Carry 7

000 111

2 6

110

010

2 2

101 100 5 3 4 4 Blue: Red: Unsigned Numbers Signed Numbers

011 3 Overflow

EE 308

Spring 2006

Binary, Hex and Decimal (Signed & Unsigned) Numbers (4-bit representatio

Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

Decimal US 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 S 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

EE 308

Spring 2006

Signed Number Representation in 2s Complement Form:


If most significant bit is 0 (most significant hex digit 07), number is positive. Get decimal equivalent by converting number to decimal, and using + sign. Example for 8bit number: 3A 16 > + ( 3 x 16 + ( 3 x 16 + 58
10 1

+ 10 x 16 ) 10 + 10 x 1 )
10

If most significant bit is 1 (most significant hex digit 8F), number is negative. Get decimal equivalent by taking 2s complement of number, converting to decimal, and using sign. Example for 8bit number: A3
16

> ( 5D ) 16 ( 5 x 16 93 10

( 5 x 16 1

+ 13 x 16 0) 10 + 13 x 1 ) 10

10

EE 308

Spring 2006

Ones Complement Table Makes It Simple To Find 2s Complements

Ones Complement Table

0 1 2 3 4 5 6 7

F E D C B A 9 8

To take twos complement, add one to ones complement. Take twos complement of D0C3 :

2F3C + 1 = 2F3D

11

EE 308

Spring 2006

Overow and Carry assume you have a xed word size A carry is generated when you add two unsigned numbers together, and the result is too large to t in the xed word size. A carry is generated when you subtract two unsigned numbers, and the result should be negative. An overow is generated when you add or subtract two signed numbers, and the xed-length answer has the wrong sign. Addition and Subtraction of Binary and Hexadecimal Numbers
ADDITION AND SUBTRACTION OF BINARY AND HEXADECIMAL NUMBERS 1) Limit number of digits to specified word size. 4bit word: 1101 + 1011 1 1000 Keep only 4 bits in answer

2)

Does not matter if numbers are signed or unsigned mechanics the same Do the operation, then determine if carry and/or overflow bits are set. 4bit word: 1101 + 1011 1 1000 Neg Neg Neg

Carry is set, overflow is clear

12

EE 308

Spring 2006

Condition Code Register Gives Information On Result Of Last Operation


S X H I N Z V C

Condition Code Register 8 FFs N Negative : most significant bit of result Z Zero C Carry : 1 > result zero, 0 > result not zero : 1 > last operation generated a carry V Overflow : 1 > last operation generated an overflow

Note: Not all HC12 instructions change CCR bits. A bit in the CCR is the result of the last executed instruction which aects that bit. For example, consider the following instruction sequence: aba staa $0900 ; Add B to A ; Store A in address $0900

The ABA instruction will change the H, N, Z, V and C bits of the CCR. The STAA instruction will change the N and Z bit, and clear the V bit. After the two instructions, the H and C bits will reect the result of the ABA instruction; the N and Z bits will reect the result of the STAA instruction (was the number stored negative or zero), and the V bit will be 0.

13

EE 308

Spring 2006

Overow occurs only under certain addition and subtraction operations.


S X H I N Z V C

Condition Code Register 8 FFs N Negative : most significant bit of result Z Zero C Carry : 1 > result zero, 0 > result not zero : 1 > last operation generated a carry V Overflow : 1 > last operation generated an overflow

If you add a positive and a negative number, on overow never occurs. If you subtract two positive numbers, an overow never occurs. If you subtract two negative numbers, and overow never occurs.

14

Das könnte Ihnen auch gefallen