Microprocessor Technique Binary codes Lecture 2 1/47 Binary codes in microprocessor technique Integer arithmetic Fix-point arithmetic Floating-point arithmetic.

Download Report

Transcript Microprocessor Technique Binary codes Lecture 2 1/47 Binary codes in microprocessor technique Integer arithmetic Fix-point arithmetic Floating-point arithmetic.

Microprocessor Technique
Binary codes
Lecture 2
1/47
Binary codes in microprocessor technique
Integer arithmetic
Fix-point arithmetic
Floating-point arithmetic
Binary codes - NB
2/47
NB
B IN
0 00 0
0 00 1
0 01 0
0 01 1
0 10 0
0 10 1
0 11 0
0 11 1
1 00 0
1 00 1
1 01 0
1 01 1
1 10 0
1 10 1
1 11 0
1 11 1
DEC
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HEX
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
7
2
128
6
2
64
bit w eights in byte:
5
4
3
2
2
2
2
2
32
16
8
4
L NB 
1
2
2
0
2
1
i
 bi  2
i  0 .. N  1
E xam ple:
4
3
2
1
30 = 16+ 8+ 4+ 2 = 2 + 2 + 2 + 2  00011110
Binary codes - NB
3/47
value ranges:
binary
1 nibble:
0000..1111
1 by te:
00000000..
..11111111
w ord of 2 by tes:
0000000000000000..
..1111111111111111
decim al
hexadecim al
0..15
0..0F h
0..255
00..0F F h
0..
..65535
0000..
..0F F F F h
A pplications:
possitive integer arithm etic, table/m atrix indexing, event counting
Binary codes - SM
4/47
SM - sign-magnitude
B IN
0 00 0
0 00 1
0 01 0
0 01 1
0 10 0
0 10 1
0 11 0
0 11 1
1 00 0
1 00 1
1 01 0
1 01 1
1 10 0
1 10 1
1 11 0
1 11 1
DEC
0
1
2
3
4
5
6
7
-1
-2
-3
-4
-5
-6
-7
HEX
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
b.z.
0/1
6
2
64
bit w eights in byte:
5
4
3
2
2
2
2
2
32
16
8
4
L SM  (  1 )
b N 1

1
2
2
0
2
1
i
 bi  2
i  0 .. N  2
E xam ple:
4
3
2
1
-30 = -1(16+ 8+ 4+ 2) = -1(2 + 2 + 2 + 2 )  10011110
Binary codes - SM
5/47
value ranges:
binary
1 nibble:
1111..0111
1 by te:
11111111..
..01111111
w ord of 2 by tes:
1111111111111111..
..0111111111111111
decim al
hexadecim al
-7..7
0F h..7
-127..127
0F F h..7F h
-32767..
..32767
0F F F F h..
..7F F F h
A ppliations:
m antisses in floating point notation, som e A /D converters
Binary codes - C2
6/47
C2 - two’s complement
B IN
1 00 0
1 00 1
1 01 0
1 01 1
1 10 0
1 10 1
1 11 0
1 11 1
0 00 0
0 00 1
0 01 0
0 01 1
0 10 0
0 10 1
0 11 0
0 11 1
DEC
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
HEX
8
9
A
B
C
D
E
F
0
1
2
3
4
5
6
7
7
-2
-128
6
2
64
bit w eights in byte:
5
4
3
2
2
2
2
2
32
16
8
4
L C 2   128  b N 1 
1
2
2
0
2
1
i
 bi  2
i  0 .. N  2
E xam ple:
4
3
2
1
30 = 16+ 8+ 4+ 2 = 2 + 2 + 2 + 2  00011110
7
6
1
-30 = -128+ 64+ 32+ 2 = -(2 )+ 2 + 2  11000010
Binary codes - C2
7/47
value ranges:
binary
1 nibble:
1000..0111
1 by te:
10000000..
..01111111
w ord of 2 by tes:
1000000000000000..
..0111111111111111
decim al
hexadecim al
-8..7
8..7
-128..127
80h..7F h
-32768..
..32767
8000h..
..7F F F h
A dvantages:
sim ple arithm etic w ith correct sign of sum and difference
A pplications:
integer arithm etic, exponents in floating point notation
Binary codes - C2
8/47
S ign negation in C 2:  L C 2  L C 2  1
E xam ples:
17 00010001
11101110
+1
-17 11101111
-17
17
11101111
00010000
+1
00010001
1
-1
00000001
11111110
+1
11111111
S im ple m ethod:
256
0000000100000000
negation
-256 1111111100000000
256
-256
0000000100000000
1111111011111111
+1
1111111100000000
0 1 0 0h
0 F F 0 0h
com plem ent to 15
com plem ent to 16
Binary codes - C1
9/47
C1 - one’s complement
B IN
1 00 0
1 00 1
1 01 0
1 01 1
1 10 0
1 10 1
1 11 0
1 11 1
0 00 0
0 00 1
0 01 0
0 01 1
0 10 0
0 10 1
0 11 0
0 11 1
DEC
-7
-6
-5
-4
-3
-2
-1
-0
0
1
2
3
4
5
6
7
HEX
8
bit w eights in byte:
7
6
5
4
3
2
1
0
9
1-2
2
2
2
2
2
2
2
A
-127 64
32
16
8
4
2
1
B
C
i
D
L C 1   127  b N 1 
bi  2

E
i  0 .. N  2
F
0
1 E xam ple:
5
0
2 33 = 32+ 1 = 2 + 2  00100001
3 -33 = -127+ 64+ 16+ 8+ 4+ 2 = (1-2 7 )+ 2 6 + 2 4 + 2 3 + 2 2 + 2 1  11011110
4
5
6 F aults:
7 „double zero”, m ore com plicated arithm etic
Binary codes - BCD
10/47
BCD - binary coded decimal
B IN
0 00 0
0 00 1
0 01 0
0 01 1
0 10 0
0 10 1
0 11 0
0 11 1
1 00 0
1 00 1
DEC
0
1
2
3
4
5
6
7
8
9
HEX
bit w eights in byte (paccked code):
3
2
1
0
3
2
1
0
0
2
2
2
2
102 102 102 102
1
80
40
20
10
8
4
2
1
2
3
d
i
4
BCD
di
5
d  0 .. D
i  0 .. 3
6
7 E xam ple:
8
1
0
1
0
33 = 10 (2+ 1) + 1 (2+ 1) = 10 (2 + 2 ) + 1 (2 + 2 )  00110011
9
L
  (10   b
2 )
F aults:
not effective use of binary w ord, absence of sign.
A pplications:
B C D arithm etic, sim ple display service (drivers), interm ediate code during
translation betw een binary and text notation of num bers.
Binary codes - Gray code
11/47
Gray code
B IN
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
DEC
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
generation of
Gray code
words:
000
001
011
010
110
111
101
100
A dvantage:
any tw o successive w ord (including the first and the last)
differ only at one position
A pplication:
code disks or bars to control position of m oving elem ents
Binary codes - ASCII
12/47
ASCII
A S C II b a sic (7 b its)
0_
1_
_0
NUL
DLE
_1
SO H
DC1
_2
STX
_3
2_
3_
4_
5_
6_
7_
0
@
P
`
p
!
1
A
Q
a
q
DC2
‘’
2
B
R
b
r
ETX
DC3
#
3
C
S
c
s
_4
EOT
DC4
$
4
D
T
d
t
_5
ENQ
NAK
%
5
E
U
e
u
_6
ACK
SY N
&
6
F
V
f
v
_7
BEL
ETB
‘
7
G
W
g
w
_8
BS
CAN
(
8
H
X
h
x
_9
HT
EM
)
9
I
Y
i
y
_A
LF
SUB
*
:
J
Z
j
z
_B
VT
ESC
+
;
K
[
k
{
_C
FF
FS
,
<
L
\
l
|
_D
CR
GS
-
=
M
]
m
}
_E
SO
RS
.
>
N
^
n
~
_F
SI
US
/
?
O
_
o
DEL
128 codes (0..127) representing:
33 control codes i 95 alphanumeric chars
Applications:
communication (text), service of: mosaic
display, printers, autonomous keyboards, etc.
Extended ASCII - additional 128 codes
(128..255) representing different chars and
semigraphic
Arithmetic - NB
13/47
Addition and subtraction in NB
01011100
+ 10010011
11101111
0
10
01110111
- 00101010
01001101
5Ch
+ 93h
0EFh
77h
- 2Ah
4Dh
92
+ 147
239
119
- 42
77
10001000
+ 10100000
1 00101000
1
0
 10 10
 10 10
 110|1 
 01010000
-  01100100
1 11101100
Carry flag (CY) signals: overflow in addition
borrow in subtraction
88h
136
+ 0A0h + 160
128h
296=256+40
50h
- 64h
0ECh
C2
14h
80
- 100
- 20
Arithmetic - C2
14/47
Addition and subtraction in C2
01011100
+ 00100011
01111111
0
10
01110111
- 00101010
01001101
5Ch
+ 23h
7Fh
77h
- 2Ah
4Dh
92
+ 35
127
119
- 42
77
01011000
+ 10100000
11111000
58h
88
+ 0A0h + (-96)
0F8h
-8
1




-
1
50h
- 84h
0CCh
C2
34h
0
10 10
10 10
1  1
01010000
10000100
11001100
80
-(-124)
204
! overflow !
Let: S1, S2, S - signs of the operands (MSB of two arguments and result)
Overflow occurs: in addition in C2 when S1=S2S
in subtraction in C2 when S1S2 i CY=S
Arithmetic - BCD
15/47
Addition and subtraction in BCD
01010100
+ 00110011
10000111
54h
+ 33h
87h
54
+ 33
87
10001000
+ 01100001
11101001
+ 01100000
1 01001001
88h
+ 61h
0E 9h
+ 60h
149h
01010100
00110011
00100001
54h
- 33h
21h
54
- 33
21
01010010
- 01100100
00011110
- 00000110
0 00011000
52h
34h
1E h
06h
18h
-
-
88
+ 61
149
 correction
(decim al adjust)
52
- 34
18
 correction
Carry flag (CY) after correction signals: overflow in addition BCD of numbers
borrow in subtraction of BCD numbers
Arithmetic - long (multi-byte) numbers
i:=0
CY:=0
R[ i ]:=N1[ i ]+N2[ i ]+CY
i:=i+1
Y
i<m?
N
16/47
N1[0]
...
N1[m-1]
N2[0]
...
N2[m-1]
i:=0
CY:=0
R[ i ]:=N1[ i ]-N2[ i ]-CY
i:=i+1
Y
R[0]
...
R[m-1]
i<m?
N
Arithmetic - long (multi-byte) numbers
17/47
i=X
CY=x
i=0
CY=0
88h + 35h + 0 = 0BDh
i=1
N1:
88
80
02
CY=0
R:
80h + C2h + 0 = 142h
CY=1
i=2
N2:
35
C2
00
02h + 00h + 1 = 03h
CY=0
i=3 (>= m=3)
BD
X
42
X
03
X
Arithmetic - long (multi-byte) numbers
i:=0
CY:=0
R[ i ]:=N1[ i ]+N2[ i ]+CY
BCD correction
i:=i+1
Y
i<m?
N
18/47
N1[0]
...
N1[m-1]
N2[0]
...
N2[m-1]
R[0]
...
R[m-1]
i:=0
CY:=0
R[ i ]:=N1[ i ]-N2[ i ]-CY
BCD correction
i:=i+1
Y
i<m?
N
Arithmetic - multiplication without sign
19/47
ATTENTION: bit-length of product is equal the sum of bit-lengths of factors
1. Multiplication by repeated addition
Let: N1,N2 - 1-byte factor, R - 2-byte product
N1
...
N2
...
RL
RH
R:=0
[2]
computational complexity:
N2=0 & N1=x:
Y
N2 = 0 ?
[1]
N
R:=R+N1
N2:=N2-1
[2]
[1]
CCMIN=2+1=3
N2=255 & N1=x:
CCMAX=2+(1+2+1)*255=1023
so:
CCAVR=513
Arithmetic - multiplication without sign
20/47
2. Multiplication
1215
x 204
4860
2430
247860
N1
...
N2
...
RL
RH
10101
x 110
101010
10101
1111110
Y
(21)
(6)
Let: N1,N2 - 1-byte factor,
R - 2-byte product
(126)
R:=0
i:=0
[2]
[1]
N2i = 0 ?
[1]
computational complexity:
N2=0 & N1=x:
CCMIN=2+1+(1+2+1+1)*8=43
N2=11111111b & L1=x:
N
R:=R+N1
[2]
N1:=SHL(N1)
i:=i+1
[2]
[1]
CCMAX=2+1+(1+2+2+1+1)*8=59
so:
CCAVR=51
i<8?
N
Y [1]
previous method is faster only
in 4% of cases
Arithmetic - multiplication without sign
21/47
3. Examples in Z80 machine language
3.1. Multiplication by repeated addition
source:
; let: E=N1, B=N2,
LD
HL,0
LD
D,H
INC B
JR
kon
rept: ADD HL,DE
fini: DJNZ rept
computational
complexity:
HL=R
;R:=0
;N1h:=0
;addition
;jump if L20
instruction execution time
[CLK cycles]
[10]
[4]
[4]
[12]
[11]
[8/13]
N2=0: CCMIN=10+4+4+12+8=38
N2=255: CCMAX=10+4+4+12+(13+11)*255+8=6158
CC=38+24*N2
CCAVR=3098
Arithmetic - multiplication without sign
22/47
3.2. Multiplication ver.I
source 1:
; let: E=N1, C=N2, HL=R
LD
B,8
;loop counter
LD
HL,0
;R:=0
LD
D,H
;N1h:=0
check: BIT 0,C
;test bit of N2
JR
Z,nosum
ADD HL,DE
nosum: EX
DE,HL
ADD HL,HL
;SHL(N1)
EX
DE,HL
RR
C
;next bit of N2
DJNZ check
computational
complexity:
instruction execution time
[CLK cycles]
[7]
[10]
[4]
[8]
[7/12]
[11]
[4]
[11]
[4]
[8]
[8/13]
N2=0: CCMIN=7+10+4+(8+12+4+11+4+8)*8+13*7+8=496
N2=255: CCMAX=7+10+4+(8+7+11+4+11+4+8)*8+13*7+8=544
CCAVR=520
Arithmetic - multiplication without sign
23/47
3.3. Multiplication ver.II
source 2 optimal:
; let: E=N1, H=N2, HL=R
LD
B,8
;loop counter
LD
D,0
;N1h:=0
LD
L,D
rept: ADD HL,HL
;test bit of N2
JR
NC,nosum
ADD HL,DE
;addition
nosum: DJNZ rept
;next bit
computational
complexity:
instruction execution time
[CLK cycles]
[7]
[7]
[4]
[11]
[7/12]
[11]
[8/13]
N2=0: CCMIN=7+7+4+(11+12)*8+13*7+8=301
N2=255: CCMAX=7+7+4+(11+7+11)*8+13*7+8=349
CCAVR=325
Arithmetic - multiplication without sign
24/47
4. Multiplication of long (multi-byte) numbers
• manipulating of longer bit strings;
• using subroutine to partially multiplication (byte x byte):
x:
x1
x0
y:
y1
y0
4 m u ltip lica tio n s
4 a d d itio n s
x 0 y 0
x 0 y 1
x 1 y 0
x 1 y 1
x y = x 1 y 1 2 5 6 2 5 6 + x 0 y 1 2 5 6 + x 1 y 0 2 5 6 + x 0 y 0
• multiplication of nibbles (only for BCD numbers);
Arithmetic - multiplication without sign
• faster (trick) multiplication:
assume: x = Bx1 + x0
y = By1 + y0
25/47
(B=256)
w = xy = (Bx1 + x0)(By1 + y0) - 4 multiplications & 4 additions
let: u = x1 - x0 i v = y1 - y0 then:
uv = x1y0 + x0y1 - x1y1 - x0y0  x1y0 + x0y1 = uv + x1y1 + x0y0
w = BBx1y1 + B(x1y0 + x0y1) + x0y0 =
= BBx1y1 + Buv + Bx1y1 + Bx0y0 + x0y0 =
= (BB + B)x1y1 + Buv + (B + 1)x0y0
x:
x1
x0
y:
y1
y0
2 su b tractio n s
3 m u ltip licatio n s
4 ad d itio n s
x 0 y 0
x 0 y 0
u v
x 1 y 1
x 1 y 1
Arithmetic - multiplication without sign
5. Multiplication by constant
assume: constant value K is known at coding phase.
ki2i
26/47
n 1
 ki 2
i0
If only few (1..4) ki = 1, then it’s possible to use formula:
L  K = L  2i1 + L  2i2 + L  2i3 + L  2i4
where i1, i2, i3, i4 are exponents of 2 for kiX = 1.
K =Σ
i
Especially if K=2m , then multiplication is equivalent m-times arithmetic
shifting left of number L.
T w o exam ples of arithm etic shift left by 3 bits:
LD
LD
L,number
LD
LD
H,0
SLA
ADD
HL,HL
;HL=number*2
RL
ADD
HL,HL
;HL=number*4
SLA
ADD
HL,HL
;HL=number*8
RL
LD
(product),HL
SLA
RL
LD
L,number
H,0
L
H
;HL=number*2
L
H
;HL=number*4
L
H
;HL=number*8
(product),HL
Arithmetic - multiplication without sign
M ultiplication by constant K  2
27/47
m
E xam ples:
K = 10 = 2 + 8
L K = L 2 + L 8 =
L  2 + (L  2)  4
LD
LD
ADD
LD
LD
ADD
ADD
ADD
LD
L,number
H,0
HL,HL ;HL=number*2
D,H
E,L
HL,HL ;HL=number*4
HL,HL ;HL=number*8
HL,DE ;HL=number*10
(product),HL
K = 15 = 12 + 3 = 3  4 + 3 =
(2 + 1)  4 + (2 + 1)
L  K = (L  2 + L )  4 + (L  2 + L )
LD
LD
LD
LD
ADD
ADD
LD
LD
ADD
ADD
ADD
LD
L,number
H,0
E,L
D,H
;DE=number
HL,HL ;HL=number*2
HL,DE ;HL=number*3
D,H
E,L
HL,HL ;HL=number*6
HL,HL ;HL=number*12
HL,DE ;HL=number*15
(product),HL
Arithmetic - division without sign
28/47
1. Division by repeated subtraction
Let: N1, N2 - 1-byte dividend & divisor, R - 1-byte quotient
Y
division
error
N2 = 0 ?
N
N1
...
N2
...
R
N
computational complexity:
[1] N1<N2:
CCMIN=1+1+1=3
R:=0
[1]
N1  N2 ?
[1]
Y
R:=R+1
N1:=N1-N2
N1=255 i N2=1:
CCMAX=1+1+(1+1+1)*255+1=768
CCAVR:
[1]
[1]
255 255
  ((n div i)  3 + 3)  707172
n0 i1
CCAVR =707172:255:256 = 10,8
R=quotient
N1=remainder
D iv isio n 2 B /1 B :
C C M IN = 5
C C M AX= 5+ 65535*6= 393215
65535 255
C C AVR:

 ((n d iv i)  6 + 5 )  5 , 9 6  1 0
n  0 i 1
10

C C AVR = 3567
Arithmetic - division without sign
29/47
2. Division by power of 2
equivalent to arithmetic shift right
i:=k
i>0?
[1]
N
[1]
Y
N1:=SHRA(N1)
i:=i-1
[1]
[1]
computational complexity:
CC=1+1+(1+1+1)*k=2+3k
depends on:
- bit-length of multiplied number;
- value of power;
- free working registers;
- instruction list.
Example:
consecutive states of N1 during division
N1=31 by 2 3=8
00011111
00001111
00000111
00000011
Arithmetic - division without sign
30/47
3. Division
25
11001
127 : 5
1111111 : 101
10
101
27
101
25
101
2 = reminder
0111
101
10 = reminder
N
zał: N1,N2 - 1-byte dividend & divisor,
NN - 2-byte working variable (register)
R - 1-byte quotient, R=NNL
i:=8
NNH:=0
NNL:=L1
[1]
[1]
[1]
NN:=2NN
[2]
NNH  N2 ?
[1]
Y
NNH:=NNH -N2
NNL0:=1
[1]
[1]
i:=i-1
[1]
i>0?
Y [1]
N
computational complexity:
N1=0 i N2=x:
CCMIN=1+1+1+(2+1+1+1)*8=43
N2=11111111b i N1=x:
CCMAX=1+1+1+(2+1+1+1+1+1)*8=59
CCAVR = 51
Atention:
if N2=0 then R=255
Arithmetic - division without sign
31/47
Example:
consecutive states of NN during division N1=31 by N2=7
NN: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 0 0 00 0 0 1 1 1 1 1 0
0000000001111100
0000000011111000
0 0 0 0 00 00 00 01 1 1 1 1 0 0 0 0
0 0 0 0 0 0 01 01 1 1 1 0 0 0 0 0
0 0 0 0 0 01 01 01 1 1 0 0 0 0 0 0
-N2 0 0 0 0 0 1 1 1
0000000011000001
0 0 0 0 0 0 0 01 1 0 0 0 0 0 1 0
0000001100000100
reminder
R=quotient
N2: 0 0 0 0 0 1 1 1
Arithmetic - division without sign
32/47
4. Division examples in Z80 machine language
4.1. Division by repeated subtraction
source:
; let: A=N1, B=N2, C:=R=N1:N2
LD
C,A
;store N1
LD
A,B
;check N2
AND A
JR
Z,fini ;jump if N2=0
LD
A,C
;restore N1
LD
C,0
;quotient R:=0
rept: SUB B
;N1:=N1-N2
JR
C,fini ;jump if N1<L2
INC C
;quotient+1
JR
rept
fini: ADD B
;A=reminder
;C=quotient
instruction execution time
[CLK cycles]
[4]
[4]
[4]
[7/13]
[4]
[7]
[4]
[7/13]
[4]
[12]
[4]
computational N2=0: CCERR=4+4+4+13+4=29
complexity:
N1<N2: CCMIN=4+4+4+7+4+7+4+13+4=51
N1=255 i N2=1: CCMAX=4+4+4+7+4+7+(4+7+4+12)*255+4+13+4=6936
Arithmetic - division without sign
33/47
4.2. Division
source:
; let: L=N1, E=N2, L:=R=N1:N2
LD
B,8
;loop counter
LD
H,0
;LDH:=0
et1:
ADD HL,HL
;SHL(N1)
LD
A,H
SUB E
;A:=NNh-N2
JR
C,et2
;jump if NNh<L2
LD
H,A
;NNh:=NNh-L2
INC L
;bit 0 in NNl:=1
et2:
DJNZ et1
;H=reminder
;L=quotient
computational
complexity:
instruction execution time
[CLK cycles]
[7]
[7]
[11]
[4]
[4]
[7/12]
[4]
[4]
[8/13]
N1<N2: CCMIN=7+7+(11+4+4+12+13)*7+11+4+4+12+8=361
N1=255 i N2=1:
CCMAX=7+7+(11+4+4+7+4+4+13)*7+11+4+4+7+4+4+8=385
CCAVR=373
Arithmetic - with sign
34/47
Previous algorithms need separate determination of result sign
determining of result sign Z
Z:=0
N
N1 < 0 ?
Y
N1:=-N1
Z:=1
N
N2 < 0 ?
Y
N2:=-N2
Z:=(Z+1)mod 2
setting sign Z to result R
N
Z=1?
Y
R:=-R
Arithmetic - with sign
35/47
determining of result sign - ther way
A:=N1H
A:=A  N2H
Z:=A7
negation of m-byte number
i:=0
CY:=0
N[ i ]:=0-N[ i ]-CY
i:=i+1
Y
i<m?
N
A - accumulator (assume: 1-byte)
N1H,N2H - MS bytes of operands
A7 - accumulator MSB
Arithmetic - fix-point notation
36/47
1. Fix-point notation
bit
w eight
2
m -1

2
m -2
...
2
1
2
m b its of in teger p art
0

2

-1
2
-2
... 2
-(n+ 1)
n b its of fraction al p art
2
-n

value range: 0 .. 2m-2-n
Example: let m=8 i n=4:
10,25  00001010.0100
10,20
 00001010.0011 = 10,1875
number of bits of integer and fractional parts depends on:
 needed precision;
 needed value range.
Positive and negative numbers are represented in C2 code.
Arithmetic - fix-point notation
37/47
Example of fix-point notations:
fo rm at
n eg ativ e v alu es
p o sitiv e v alu es
m .n
m in
m ax
m in
m ax
8 .8 N B
-
-
0
2 -2
2 5 5 ,9 9 6
8 .8 C 2
-(2 )
-1 2 8
-8
0
2 -2
1 2 7 ,9 9 6
1 6 .8 C 2
-(2 )
-3 2 7 6 8
-8
0
2 -2
3 2 7 6 7 ,9 9 6
1 6 .1 6 C 2
-(2 )
-3 2 7 6 8
-16
0
2 -2
3 2 7 6 7 ,9 9 9 9 8
7
-(2 )
-3 ,9 E -3
15
-(2 )
-3 ,9 E -3
15
-(2 )
-1 ,5 2 6 E -5
2. Addition and subtraction
same as on integer numbers
accu racy
8
-8
2
3 ,9 E -3
7
-8
2
3 ,9 E -3
15
15
-8
-8
-8
2
3 ,9 E -3
-8
-16
2
1 ,5 2 6 E -5
-16
Arithmetic - fix-point notation
3. Multiplication of fix-point numbers
E xam p les let m = 4 , n = 2 :
A ) 2 ,5
 2 ,5
1 2 5
5 0
6 ,2 5
B ) 6 ,2 5
 1 ,5
3 1 2 5
6 2 5
9 ,3 7 5
0 0 1 0 .1 0
 0 0 1 0 .1 0
00101 00
001010
0 0 0 0 0 1 1 0 .0 1 0 0

0 1 1 0 .0 1
0 1 1 0 .0 1
 0 0 0 1 .1 0
0110010
011001
0 0 0 0 1 0 0 1 .0 1 1 0

1 0 0 1 .0 1
alb o 1 0 0 1 .1 0
p artial p ro d u cts
su m o f p artial p ro d u cts, m ’= 8 , n ’= 4
n o rm alised p ro d u ct
p artial p ro d u cts
su m o f p artial p ro d u cts, m ’= 8 , n ’= 4
n o rm alised p ro d u ct:
= 9 ,2 5 - erro r at ro u n d in g d o w n
= 9 ,5 0 - erro r at ro u n d in g u p
38/47
Arithmetic - fix-point notation
39/47
4. Division of fix-point numbers
R ealised sam e as in teger d ivisio n .
E xam p le (fo rm at 4 .2 ):
2 ,0 0 : 1 ,2 5 = 1 ,6 0
1 .1 0 0 1  0 0 0 1 .1 0 = 1 ,5 0 - ro u n d in g erro r
001000 : 101
- 101
110
- 101
1000
- 101
11
Conclusions:
1. During multiplication and division rounding and precision errors can occur.
2. Fix-point notation should minimise these errors.
Arithmetic - floating-point notation
1. Floating-point notation - basis
Any real value L can be represented by
L=mpe
where:
m - mantissa from the range < 0 , p0 );
p - counting base (eg. 10);
e - integer exponent;
Usually m  {0}  {< p-1 , p0 )} - so called standardized mantissa.
For binary numbers (base p=2):
m  {0}  {< 1/2 , 1 )}
For example:
0,75 = 2-1 + 2-2
 0.11 = .1100  20 (=0,75  1)
2,75 = 21 + 2-1 + 2-2  10.11  20 = .1011  22 (=0,6875  4)
40/47
Arithmetic - floating-point notation
Mantissa is usually stored in SM code (separate sign bit).
Exponent e is stored in C2 either in “shifted” C2:
exponent e:
127
...
2
1
0
-1
-2
...
-127
-128
eC2
01111111
...
00000010
00000001
00000000
11111111
11111110
...
10000001
10000000
eC2+80h LNB(eC2+80h)
11111111
255
...
...
10000010
130
10000001
129
10000000
128
01111111
127
01111110
126
...
...
00000001
1
00000000
0
Shifted C2 simplifies calculation algorithms,
but needs an attention during multiplication and division.
41/47
Arithmetic - floating-point notation
42/47
2. Formulas for basic calculations
if: L1 = m1pe1, L2 = m2pe2 then:
exponent in C2
exponent in shifted C2 by 
L1  L2 = m1  m2  p e1+e2
L1  L2 = m1  m2  p e1+e2-
L1 : L2 = m1 : m2  p e1-e2
L1 : L2 = m1 : m2  p e1-e2+
 (m1  ( m 2 : p e1  e 2 ))  p e1 if e1 > e2

e1
if e1 = e2
 (m1  m 2 )  p
L1  L2 = 
e 2  e1
e2
((m1
:
p
)

m
2
)

p
if e1 < e2

Arithmetic - floating-point notation
43/47
3. Examples of FP notations
A)
m  {0}  {< 1/2 , 255/256 >} e  < -128 , 127 >
value range:
0 , 2-129 , ... , 255  2119
0 , 1,47  10-39 , ... , 1,69  1038
eC2
m NB
B)
7 6
0
S e C 2 + 40h
m 0 SM
m 1 SM
C)
e C 2 + 80h
m 0 SM
m 1 SM
S m 2 SM
2322
16
m  {0}  {< 1/2 , 1-2-16 >} e  < -64 , 63 >
value range:
-65535  247 ... -(2-65) ,0,2-65
... 65535  247
-9,221018...-2,7110-20,0,2,7110-20...9,221018
m  {0}  {< 1/2 , 1-2-23 >} e  < -128 , 127 >
value range:
-(223-1)2104 ... -2-129 ,0, 2-129
...(223-1) 2104
-1,71038...-1,4710-39 ,0 , 1,4710-39...1,71038
Arithmetic - floating-point notation
D)
m 1 SM
m 0 SM
S e C 2 + 80h
31|30
E)
m 2 SM
23|22
m 1 SM
m 0 SM
m 3 SM
m 2 SM
m 5 SM
m 4 SM
S
e C 2 + 400h
63|62
F)
16
m 6 SM
68|67
m 1 SM
m 0 SM
m 3 SM
m 2 SM
m 5 SM
m 4 SM
m 7 SM
m 6 SM
S
79|78
64
I8087 real number short
|| - hidden, default 7 bit of m2SM allways =1
value range:
-(224-1)2103 ... -2-129 ,0, 2-129
...(224-1) 2103
-1,71038...-1,4710-39 ,0 , 1,4710-39...1,71038
wartość 0  m=0 i e=0
I8087 real number long
|| - hidden, default 4 bit of m6SM allways =1
value range:
-(229-1)2995 ... -2-1025 ,0, 2-1025
...(229-1)2995
-1,810308...-2,7810-309 ,0 ,2,7810-309...1,810308
for value 0  m=0 i e=0
I8087 real number extended
value range:
-(264-1)216319 ... -2-16385,0,2-16385 ...(264-1) 216319
-5,95104931...-4,210-4933 , 0 ,
4,210-4933 ...5,95104931
for value 0  m=0 i e=0
e C 2 + 4000h
64
44/47
Arithmetic - floating-point notation
45/47
4. Examples of algorithms
4.1. Addition and subtraction
R = mr  p er = N1  N2 =
 (m1  ( m 2 : p e1  e 2 ))  p e1 if e1 > e2

e1
if e1 = e2
 (m1  m 2 )  p
=
 ((m1 : p e 2  e1 )  m 2 )  p e 2 if e1 < e2

e:=e1-e2
<0
m1:=m1:p|e|
ew:=e2
>0
e ?
m2:=m2:pe
=0
er:=e1
mr:=m1m2
Y
mr > p0 ?
mr:=mr : p
er:=er+1
N
Y
mr = 0 ?
N
N
mr < p-1 ?
Y
mr:=mrp
er:=er-1
Arithmetic - floating-point notation
46/47
4.2. Multiplication
R = mr  p er = N1  N2 = m1  m2  p e1+e2
due to mantissas
er:=e1+e2
mr:=m1  m2
Y
mr = 0 ?
N
N
mr < p-1 ?
Y
er:=er-1
mr:=mr  p
m1 & m2
standardized
m1 else m2
non-standardized
Arithmetic - floating-point notation
47/47
4.3. Division
R = mr  p er = N1 : N2 = m1 : m2  p e1-e2
due to mantissas:
N
er:=e1-e2
mr:=m1 : m2
Y
mr = 0 ?
st
m2 = 0 ?
Y
st - m1 & m2 standardized
nst - m1 else m2 non-standardized
N
N
mr > p0 ?
nst
N
error alert
Y
mr < p-1 ?
Y
er:=er-1
mr:=mr  p
er:=er+1
mr:=mr : p
st
nst