You are on page 1of 38

# EEM336 –

Microprocessors I
Representation of Numbers

1
Contents
• Representation of Numbers
– Unsigned, Sign magnitude, One’s
complement, Two’s complement, Biased
• Real Numbers
– Single Precision, Double Precision
• BCD Numbers
• ASCII Numbers
• Shift and Rotate
2
Data Representation
• Computers operate on binary values (as a
result of being built from transistors)
• There are different binary representations
for integers possible qualifications:
1. Positive numbers only
2. Positive and negative numbers
4. Speed of computer operations

3
Integer Representation
• There are 5 commonly known integer
representations:
1. Unsigned
2. Sign magnitude
3. One’s complement
4. Two’s complement
5. Biased (not commonly known)

4
Integer Representation
(continued)
• All have been used at various times
for various reasons.
• Virtually all modern computers
operate based on two’s complement
representation. Because,
– Hardware is faster
– Hardware is simpler (which makes it
faster)

5
Unsigned Numbers
• The standard binary encoding that
• Only positive integer numbers are
represented
• Range: 0 to 2n - 1 for n bits
• Examples:
– 5 is represented as 0000 0101
– 134 is represented as 1000 0110

6
Sign Magnitude
• Both positive and negative integers
can be represented
• The most significant bit (MSB) is used
to represent the sign
• For positive numbers, MSB is 0 and
for negative numbers, MSB is 1
• Examples:
– 5 is represented in 8-bit as 0000 0101
– -5 is represented in 8-bit as 1000 0101

7
One’s Complement
• Positive integers use the same
representation as unsigned
• Negation is done by taking a bitwise
complement of the positive
representation
• Examples:
– 5 is represented in 8-bit as 0000 0101
– -5 is represented in 8-bit as 1111 1010

8
Two’s Complement
• Positive integers use the same
representation as unsigned
• Negation is done by taking the one’s
• Examples:
– 5 is represented in 8-bit as 0000 0101
– -5 is represented in 8-bit as 1111 1011

9
Biased Representation
• A bias value is chosen as either 2n-1 or 2n-1 -1,
according to the representation algorithm
• The integer number to be represented is added to
the bias
• Unsigned representation of the result is the biased
representation of the number
• Examples:
– In 8-bit, if bias is 127, then 5 is 1000 0100 (132)
– In 8-bit, if bias is 127, then -5 is 0111 1010 (122)

10
Sign Extension in Two’s
Complement
• Assume that you have a signed number in 8 bits and
you want to convert it to 16 bits
• This type of conversion from smaller number of bits to
larger number of bits is called sign extension
• In this case, the original number is copied into LSBs
and the sign bit is copied into the MSBs
• Examples:
5: 0000 0101 in 8 bits  0000 0000 0000 0101 in 16 bits
-5: 1111 1010 in 8 bits  1111 1111 1111 1010 in 16 bits

11
Character
Representation
• ASCII codes are used to represent the characters
• Characters are generally represented in 8-bits
• Examples:
– ‘A’ is represented as 0100 0001 (65 or 41h)
– ‘B’ is represented as 0100 0010 (66 or 42h)
– ‘0’ is represented as 0011 0000 (48 or 30h)
– ‘9’ is represented as 0011 1001 (57 or 39h)
• In Assembly, characters should be written into single-quotes (i.e.
don’t use double-quotes)

12
Converting from String
to Integer
• The ‘0’ character and the number 0 are
different
• If you want to convert the string ‘354’ to
integer, you should follow this algorithm:
– Read ‘3’ and translate it to 3 (just subtract
30h)
– Read ‘5’ and translate it to 5
– integer = 3 * 10 + 5 = 35
– Read ‘4’ and translate it to 4
– integer = 35 * 10 + 4 = 354

13
Representation of Real
Numbers
• Real numbers (or floating-point numbers)
are often encountered in computer programs
• A real number should be converted to binary
system and encoded into bits
• There are several representations but Intel
microprocessors use IEEE 754, version 10.0
standard
• Intel 8086 has no floating point instructions, so
you have to code them by yourself

14
Converting Real Numbers
into Binary
• Separate the number into integer
and fractional parts
• You can easily convert the integer
part into binary
• You can convert the fractional part
by multiplying it by 2 continuously
and taking the integer parts of the
results

15
Example: Convert 64.2 to
Binary
• Separate it into 64 and 0.2
• Binary representation of 64 is 100 0000
• 0.2 can be gotten using the algorithm:
– 0.2 * 2 = 0.4  0 (MSB)
– 0.4 * 2 = 0.8  0
– 0.8 * 2 = 1.6  1
– 0.6 * 2 = 1.2  1
– 0.2 * 2 = 0.4  0 (and it repeats itself)
–  0.2 = 0.0011 0011 0011 …
• Then, 64.2 is 100 0000.0011 0011 0011

16
Scientific Notation
• The real numbers should be
normalized and written in scientific
notation
• In scientific notation, a number has
three components:
– Sign
– Mantissa
– Exponent
• In normalized form, the integer part of
the mantissa should be 1.
17
Example: Scientific
Notation of 64.2
• 64.2 can be written in binary as:
• 1.00 0000 0011 0011 0011 … × 26
• Here, 1.00 0000 0011 0011 0011 …
is called the mantissa
• 6 is the exponent
• Sign is plus (+)

18
IEEE 754 Standard
• According to the IEEE 754 standard,
floating-point numbers can be
represented in either 32 bits or 64
bits
• 32 bit version is called single-
precision and 64 bit version is
called double-precision

19
Single Precision
31 30 23 22 0
S Exponent Mantissa

• The first bit (bit 31) represents the sign (0 for
positive and 1 for negative numbers)
• Next 8 bits (23-30) represent the biased exponent
(bias = 127)
• Next 23 bits (0-22) represent the 24 bits of the
mantissa (24 bits in 23 bits!!! Is it correct?)
– Yes! The first bit of the mantissa, which comes from the
integer part, is not needed to be coded

20
Double Precision
63 62 52 51 0
S Exponent Mantissa

• The first bit (bit 63) represents the sign (0 for
positive and 1 for negative numbers)
• Next 11 bits (52-62) represent the biased exponent
(bias = 1023)
• Next 52 bits (0-51) represent the 53 bits of the
mantissa (53 bits in 52 bits!!! Is it correct?)
– Yes! The first bit of the mantissa, which comes from the
integer part, is not needed to be coded

21
64.2 in Single Precision
• Since 64.2 is positive, sign bit should be 0
• Exponent is 6; add 127 and obtain biased value (6
+ 127 = 133 = 1000 0101)
• Mantissa is 1.00 0000 0011 0011 0011 …
• Ignore 1 in integer part and take the 23 bits from
the fractional part: 000 0000 0110 0110 0110
0110
• Combine them: 0100 0010 1000 0000 0110 0110
0110 0110 (or 42806666h)

22
64.2 in Double Precision
• Since 64.2 is positive, sign bit should be 0
• Exponent is 6; add 1023 and obtain biased value (6 +
1023 = 1029 = 100 0000 0101)
• Mantissa is 1.00 0000 0011 0011 0011 …
• Ignore 1 in integer part and take the 52 bits from the
fractional part: 0000 0000 1100 1100 1100 1100 1100
1100 1100 1100 1100 1100 1100
• Combine them: 0100 0000 0101 0000 0000 1100 1100
1100 1100 1100 1100 1100 1100 1100 1100 1100 (or
40500CCCCCCCCCCCh)

23
Exceptions
• Zero is stored as all zeros.
• The number infinity is stored as all
ones in the exponent and all zeros in
the mantissa. The sign bit indicates
either a positive or a negative infinity.
+∞ = 0 1111 1111 000 0000 0000
0000 0000 0000
-∞ = 1 1111 1111 000 0000 0000
0000 0000 0000
24
Exercises
• Calculate the single and double precision
representations of 12.3 (ans: 4144 CCCDh and
4028 9999 9999 999Ah)
• Calculate the single and double precision
representations of -23.4 (ans: C1BB 3333h
and C037 6666 6666 6666h)
• What does the single precision floating point
number 3F80 0000h represent?

25
BCD (Binary Coded
Decimal) Data
• In some cases, you might consider
representing each digits of an integer
number in 4 bits
• For example, 1234h might represent
the decimal value 1234
• These type of numbers are called
BCD numbers
• There are some special instructions
to be used with BCD numbers
26
DAA
• Corrects the result of addition of two
packed BCD values on AL register
• Example:
MOV AL, 15 ; AL = 0Fh
DAA ; AL = 15h

27
DAA Example
• Sum two 4-digit BCD numbers stored in
BX and DX and store the result in CX as
a BCD.
MOV DX, 1234h ; Load 1234 BCD
MOV BX, 3099h ; Load 3099 BCD
MOV AL, BL ; Sum low bytes
ADD AL, DL ; AL = 34h + 99h = CDh
DAA ; CF = 1, AL = 33h (133)
MOV CL, AL ; Store low byte of result
MOV AL, BH ; AL = 30h (sum high bytes)
ADC AL, DH ; AL = 43h
DAA ; CF = 0, AL = 43h
MOV CH, AL ; Store high byte of result

28
DAS
Subtraction
• Corrects the result of subtraction of
two packed BCD values on AL
register
• Similar to DAA but DAS follows a
subtraction

29
ASCII Coded Numbers
• You may use ASCII codes of numbers
to represent them
• For example, you can represent 19 as
3139h since ASCII codes of ‘1’ and ‘9’
are 31h (=49) and 39h (=57)
respectively
• There are some instructions to be used
on ASCII coded numbers: AAA, AAD,
AAM, and AAS.
30
AAA
• Corrects result in AH and AL after
addition of two ASCII coded numbers
• Works on AL and changes AL and AH
• 3030h should be added to AX after AAA
• Example:
MOV AX, '01' ; AX = 3031h (ASCII code for 1)
ADD AL, '9' ; AL = 0Ah
AAA ; AH = 01h, AL = 00h
ADD AX, 3030h ; AX = 3130 (ASCII code for 10)

31
Other ASCII Code
Operations
Multiplication
• AAS: ASCII Adjust after Subtraction

32
Shift and Rotate
• Shift and rotate instructions manipulate
binary numbers at the binary bit level
• Common applications in low-level
software used to control I/O devices
• Shift/Rotate count may be an immediate
value or CL
• Examples:
SHL AX, 1
SHR BX, CL

33
Shift
• Position or move numbers to the left or
right within a register or memory
location.
– also perform simple arithmetic as
multiplication by powers of 2+n (left shift) and
division by powers of 2-n (right shift).
• The microprocessor’s instruction set
contains four different shift instructions:
– two are logical; two are arithmetic shifts

34
The Shift Instructions
• logical shifts move
0 in the rightmost
bit for a logical left
shift;
• 0 to the leftmost bit
position for a logical
right shift
• arithmetic right
shift copies the
sign-bit through the
number
• logical right shift
copies a 0 through
the number.

35
Logical vs. Arithmetic?
• Logical shifts multiply or divide
unsigned data; arithmetic shifts
multiply or divide signed data.
– a shift left always multiplies by 2 for
each bit position shifted
– a shift right always divides by 2 for each
position
– shifting a two places, multiplies or
divides by 4

36
Rotate

37
References
• http://pages.cs.wisc.edu/~smoler/x86tex
• emu8086 Emulator’s Documentation
• Textbook

38