48
Microprocessor Technique Binary codes

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

Embed Size (px)

Citation preview

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

Microprocessor Technique

Binary codes

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

Lecture 2 1/47

Binary codes in microprocessor technique

Integer arithmetic

Fix-point arithmetic

Floating-point arithmetic

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

Binary codes - NB 2/47

NB

BIN DEC HEX0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 81001 9 91010 10 A1011 11 B1100 12 C1101 13 D1110 14 E1111 15 F

bit weights in byte:27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

10

2N..i

iiNB bL

Example:30 = 16+8+4+2 = 24+23+22+21 00011110

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

binary decimal hexadecimal1 nibble:

0000..1111 0..15 0..0Fh1 byte:

00000000.. 0..255 00..0FFh..11111111

word of 2 bytes:0000000000000000.. 0.. 0000....1111111111111111 ..65535 ..0FFFFh

Binary codes - NB 3/47

value ranges:

Applications:possitive integer arithmetic, table/matrix indexing, event counting

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

Binary codes - SM 4/47

SM - sign-magnitude

BIN DEC HEX0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 - 81001 -1 91010 -2 A1011 -3 B1100 -4 C1101 -5 D1110 -6 E1111 -7 F

bit weights in byte:b.z. 26 25 24 23 22 21 20

0/1 64 32 16 8 4 2 1

2021 1

N..i

ii

bSM b)(L N

Example:-30 = -1(16+8+4+2) = -1(24+23+22+21) 10011110

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

Binary codes - SM 5/47

binary decimal hexadecimal1 nibble:

1111..0111 -7..7 0Fh..71 byte:

11111111.. -127..127 0FFh..7Fh..01111111

word of 2 bytes:1111111111111111.. -32767.. 0FFFFh....0111111111111111 ..32767 ..7FFFh

value ranges:

Appliations:mantisses in floating point notation, some A/D converters

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

Binary codes - C2 6/47

C2 - two’s complement

BIN DEC HEX1000 -8 81001 -7 91010 -6 A1011 -5 B1100 -4 C1101 -3 D1110 -2 E1111 -1 F0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 7

bit weights in byte:-27 26 25 24 23 22 21 20

-128 64 32 16 8 4 2 1

20

12 2128N..i

iiNC bbL

Example:30 = 16+8+4+2 = 24+23+22+21 00011110-30 = -128+64+32+2 = -(27)+26+21 11000010

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

Binary codes - C2 7/47

binary decimal hexadecimal1 nibble:

1000..0111 -8..7 8..71 byte:

10000000.. -128..127 80h..7Fh..01111111

word of 2 bytes:1000000000000000.. -32768.. 8000h....0111111111111111 ..32767 ..7FFFh

value ranges:

Advantages:simple arithmetic with correct sign of sum and differenceApplications:integer arithmetic, exponents in floating point notation

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

Sign negation in C2: 1LL 2C2C

Examples:17 00010001 -17 11101111 1 00000001 256 0000000100000000

11101110 00010000 11111110 1111111011111111+1 +1 +1 +1

-17 11101111 17 00010001 -1 11111111 -256 1111111100000000

Simple method:256 0000000100000000 0 1 0 0h

negation-256 1111111100000000 0 F F 0 0h

complement to 15 complement to 16

Binary codes - C2 8/47

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

Binary codes - C1 9/47

C1 - one’s complement

BIN DEC HEX1000 -7 81001 -6 91010 -5 A1011 -4 B1100 -3 C1101 -2 D1110 -1 E1111 -0 F0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 7

bit weights in byte:1-27 26 25 24 23 22 21 20

-127 64 32 16 8 4 2 1

20

11 2127N..i

iiNC bbL

Example:33 = 32+1 = 25+20 00100001-33 = -127+64+16+8+4+2 = (1-27)+26+24+23+22+21 11011110

Faults:„double zero”, more complicated arithmetic

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

Binary codes - BCD 10/47

BCD - binary coded decimal

BIN DEC HEX0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 81001 9 9

bit weights in byte (paccked code):1023 1022 1021 1020 23 22 21 20

80 40 20 10 8 4 2 1

Example:33 = 10(2+1) + 1(2+1) = 10(21 + 20) + 1(21 + 20) 00110011

3..0i

i

D..0d

dBCD )2b10(L di

Faults:not effective use of binary word, absence of sign.

Applications:BCD arithmetic, simple display service (drivers), intermediate code duringtranslation between binary and text notation of numbers.

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

Binary codes - Gray code 11/47

Gray code

BIN DEC0000 00001 10011 30010 20110 60111 70101 50100 41100 121101 131111 151110 141010 101011 111001 91000 8

0 0 00 0 10 1 10 1 01 1 01 1 11 0 11 0 0

generation of Gray code words:

Advantage:any two successive word (including the first and the last)differ only at one positionApplication:code disks or bars to control position of moving elements

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

Binary codes - ASCII 12/47

ASCII128 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

ASCII basic (7 bits)

0_ 1_ 2_ 3_ 4_ 5_ 6_ 7_

_0 NUL DLE 0 @ P ` p

_1 SOH DC1 ! 1 A Q a q

_2 STX DC2 ‘’ 2 B R b r

_3 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 SYN & 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

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

Arithmetic - NB 13/47

Addition and subtraction in NB

01011100 + 10010011 11101111

5Ch + 93h 0EFh

92 + 147 239

10001000 + 10100000 1 00101000

88h + 0A0h 128h

136 + 160 296=256+40

0 10 01110111 - 00101010 01001101

77h - 2Ah 4Dh

119 - 42 77

1 0 10 10 10 10 1 10|1 01010000 - 01100100 1 11101100

50h - 64h 0ECh C2 14h

80 - 100 - 20

Carry flag (CY) signals: overflow in addition borrow in subtraction

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

Arithmetic - C2 14/47

Addition and subtraction in C2

Let: S1, S2, S - signs of the operands (MSB of two arguments and result)Overflow occurs: in addition in C2 when S1=S2S in subtraction in C2 when S1S2 i CY=S

01011100 + 00100011 01111111

5Ch + 23h 7Fh

92 + 35 127

01011000 + 10100000 11111000

58h + 0A0h 0F8h

88 + (-96) -8

0 10 01110111 - 00101010 01001101

77h - 2Ah 4Dh

119 - 42 77

1 0 10 10 10 10 1 1 01010000 - 10000100 1 11001100

50h - 84h 0CCh C2 34h

80 -(-124) 204 ! overflow !

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

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 0E9h + 60h 149h

88+ 61 149 correction(decimal adjust)

01010100- 00110011 00100001

54h - 33h 21h

54 - 33 21

01010010- 01100100 00011110- 00000110 0 00011000

52h - 34h 1Eh - 06h 18h

52- 34 18 correction

Carry flag (CY) after correction signals: overflow in addition BCD of numbers borrow in subtraction of BCD numbers

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

Arithmetic - long (multi-byte) numbers 16/47

N1[0]

... N1[m-1]

i:=0 CY:=0

R[ i ]:=N1[ i ]+N2[ i ]+CY i:=i+1

i < m ? Y

N

N2[0] ...

N2[m-1]

R[0] ...

R[m-1]

i:=0 CY:=0

R[ i ]:=N1[ i ]-N2[ i ]-CY i:=i+1

i < m ? Y

N

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

Arithmetic - long (multi-byte) numbers 17/47

R: XXX

N1:8002

N2:

88

C200

35

i=X CY=x

i=0 CY=0

88h + 35h + 0 = 0BDh

BDCY=0i=1

80h + C2h + 0 = 142h

CY=1

42

i=202h + 00h + 1 = 03h

03

CY=0i=3 (>= m=3)

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

Arithmetic - long (multi-byte) numbers 18/47

N1[0]

... N1[m-1]

i:=0 CY:=0

R[ i ]:=N1[ i ]+N2[ i ]+CY BCD correction

i:=i+1

i < m ? Y

N

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

i < m ? Y

N

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

Arithmetic - multiplication without sign 19/47

ATTENTION: bit-length of product is equal the sum of bit-lengths of factors

N1 ... N2 ... RL

RH

R:=0

R:=R+N1 N2:=N2-1

N2 = 0 ? Y

N

[2] [1] [2] [1]

1. Multiplication by repeated addition

Let: N1,N2 - 1-byte factor, R - 2-byte product

computational complexity:

N2=0 & N1=x:

CCMIN=2+1=3

N2=255 & N1=x:

CCMAX=2+(1+2+1)*255=1023

so:

CCAVR=513

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

Arithmetic - multiplication without sign 20/47

2. Multiplication

1215 10101 (21) x 204 x 110 (6) 4860 101010 2430 10101 247860 1111110 (126)

N1 ... N2 ... RL

RH

R:=0 i:=0

R:=R+N1

N2i = 0 ? Y

N

[2] [1] [1]

[2] [2] [1]

[1]

N1:=SHL(N1) i:=i+1

i < 8 ? Y

N

Let: N1,N2 - 1-byte factor, R - 2-byte product

computational complexity:

N2=0 & N1=x:

CCMIN=2+1+(1+2+1+1)*8=43

N2=11111111b & L1=x:

CCMAX=2+1+(1+2+2+1+1)*8=59

so:

CCAVR=51

previous method is faster only in 4% of cases

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

Arithmetic - multiplication without sign 21/47

3. Examples in Z80 machine language

3.1. Multiplication by repeated addition

source: instruction execution time [CLK cycles]

; let: E=N1, B=N2, HL=R LD HL,0 ;R:=0 LD D,H ;N1h:=0 INC B JR kon rept: ADD HL,DE ;addition fini: DJNZ rept ;jump if L20

[10] [4] [4] [12] [11] [8/13]

computational complexity:

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

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

Arithmetic - multiplication without sign 22/47

3.2. Multiplication ver.I

source 1: instruction execution time [CLK cycles]

; 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

[7] [10] [4] [8] [7/12] [11] [4] [11] [4] [8] [8/13]

computational complexity:

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

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

Arithmetic - multiplication without sign 23/47

3.3. Multiplication ver.II

source 2 optimal: instruction execution time [CLK cycles]

; 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

[7] [7] [4] [11] [7/12] [11] [8/13]

computational complexity:

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

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

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):

4 multiplications 4 additions

xy = x1y1

256256 + x0y1

256 + x1y0

256 + x0y0

x1 x0

y1 y0

x:

y:

x0y0

x0y1

x1y0

x1y1

• multiplication of nibbles (only for BCD numbers);

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

Arithmetic - multiplication without sign 25/47

• faster (trick) multiplication:

assume: x = Bx1 + x0 y = By1 + y0 (B=256)

w = xy = (Bx1 + x0)(By1 + y0) - 4 multiplications & 4 additions

let: u = x1 - x0 i v = y1 - y0 then:

uv = x1y0 + x0y1 - x1y1 - x0y0 x1y0 + x0y1 = uv + x1y1 + x0y0

w = BBx1y1 + B(x1y0 + x0y1) + x0y0 =

= BBx1y1 + Buv + Bx1y1 + Bx0y0 + x0y0 =

= (BB + B)x1y1 + Buv + (B + 1)x0y0

2 subtractions 3 multiplications 4 additions

x1 x0

y1 y0

x:

y:

x0y0

x0y0

uvx1

y1

x1y1

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

5. Multiplication by constant assume: constant value K is known at coding phase.

K =Σ ki2i

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.

Especially if K=2m , then multiplication is equivalent m-times arithmetic shifting left of number L.

Arithmetic - multiplication without sign 26/47

1n

0i

ii 2k

Two examples of arithmetic shift left by 3 bits:

LD L,numberLD H,0ADD HL,HL ;HL=number*2ADD HL,HL ;HL=number*4ADD HL,HL ;HL=number*8LD (product),HL

LD L,number LD H,0 SLA L RL H ;HL=number*2 SLA L RL H ;HL=number*4 SLA L RL H ;HL=number*8 LD (product),HL

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

Arithmetic - multiplication without sign 27/47

Multiplication by constant K2m

Examples:

K = 10 = 2 + 8L K = L 2 + L 8 = L 2 + (L 2) 4

K = 15 = 12 + 3 = 3 4 + 3 = (2 + 1) 4 + (2 + 1)L K = (L 2 + L) 4 + (L 2 + L)

LD L,number LD H,0 ADD HL,HL ;HL=number*2 LD D,H LD E,L ADD HL,HL ;HL=number*4 ADD HL,HL ;HL=number*8 ADD HL,DE ;HL=number*10 LD (product),HL

LD L,number LD H,0 LD E,L LD D,H ;DE=number ADD HL,HL ;HL=number*2 ADD HL,DE ;HL=number*3 LD D,H LD E,L ADD HL,HL ;HL=number*6 ADD HL,HL ;HL=number*12 ADD HL,DE ;HL=number*15 LD (product),HL

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

Arithmetic - division without sign 28/47

1. Division by repeated subtractionLet: N1, N2 - 1-byte dividend & divisor, R - 1-byte quotient

computational complexity: N1<N2: CCMIN=1+1+1=3 N1=255 i N2=1: CCMAX=1+1+(1+1+1)*255+1=768 CCAVR:

((n div i) 3 + 3)255

in 10

255707172

CCAVR =707172:255:256 = 10,8

N1 ... N2 ... R

R:=0

R:=R+1 N1:=N1-N2

N1 N2 ? N

Y

[1]

[1]

[1]

[1] [1]

N2 = 0 ? Y

N

division error

R=quotient N1=remainder

Division 2B/1B:CCMIN=5 CCMAX=5+65535*6=393215

CCAVR: ((n div i) 6 + 5)255

in 10

65535105 96 10, CCAVR = 3567

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

Arithmetic - division without sign 29/47

2. Division by power of 2

equivalent to arithmetic shift right

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

[1]

[1]

[1] [1]

N1:=SHRA(N1) i:=i-1

i > 0 ?

Y

N

i:=k

0 0 0 1 1 1 1 1

0 0 0 0 1 1 1 1

0 0 0 0 0 1 1 1

0 0 0 0 0 0 1 1

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

Arithmetic - division without sign 30/47

3. Division

25 127 : 5 10 27 25 2 = reminder

11001 1111111 : 101 101 101 101 0111 101 10 = reminder

zał: N1,N2 - 1-byte dividend & divisor, NN - 2-byte working variable (register) R - 1-byte quotient, R=NNL

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

NN:=2NN

i:=i-1

NNH N2 ? N

Y

[1] [1] [1] [2]

[1]

[1] [1]

[1] [1]

NNH:=NNH -N2 NNL0:=1

i > 0 ? Y

N

i:=8 NNH:=0

NNL:=L1

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

Arithmetic - division without sign 31/47

Example:

consecutive states of NN during division N1=31 by N2=7

0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1NN:

0 0 0 0 0 0 0 00 0 0 1 1 1 1 1 0

0 0 0 0 0 0 0 00 0 0 1 1 1 1 1 0 0

0 0 0 0 0 0 0 00 0 0 1 1 1 1 1 0 0 0

0 0 0 0 0 0 0 00 0 0 1 1 1 1 1 0 0 0 0

0 0 0 0 0 0 0 00 0 0 1 1 1 1 1 0 0 0 0 0

0 0 0 0 0 0 0 00 0 0 1 1 1 1 1 0 0 0 0 0 0

0 0 0 0 0 1 1 1N2:

0 0 0 0 0 1 1 1-N2 0 0 0 0 0 0 0 00 0 0 0 0 0 1 1 0 0 0 0 0 1

0 0 0 0 0 0 0 00 0 0 0 0 0 1 1 0 0 0 0 0 1 0

0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 reminder R=quotient

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

Arithmetic - division without sign 32/47

4. Division examples in Z80 machine language

4.1. Division by repeated subtraction

source: instruction execution time [CLK cycles]

; 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

[4] [4] [4] [7/13] [4] [7] [4] [7/13] [4] [12] [4]

computational complexity:

N2=0: CCERR=4+4+4+13+4=29 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

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

Arithmetic - division without sign 33/47

4.2. Division

source: instruction execution time [CLK cycles]

; 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

[7] [7] [11] [4] [4] [7/12] [4] [4] [8/13]

computational complexity:

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

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

Arithmetic - with sign 34/47

Previous algorithms need separate determination of result sign

determining of result sign Z

setting sign Z to result R

N1:=-N1 Z:=1

N1 < 0 ?

Y

Z:=0

N2:=-N2 Z:=(Z+1)mod 2

N2 < 0 ?

Y

N

N

R:=-R

Z = 1 ?

Y

N

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

Arithmetic - with sign 35/47

determining of result sign - ther way

A - accumulator (assume: 1-byte) N1H,N2H - MS bytes of operands A7 - accumulator MSB

negation of m-byte number

A:=N1H A:=A N2H

Z:=A7

i:=0 CY:=0

N[ i ]:=0-N[ i ]-CY i:=i+1

i < m ? Y

N

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

Arithmetic - fix-point notation 36/47

1. Fix-point notation

bitweight 2m-1 2m-2 . . . 21 20 2-1 2-2 . . . 2-(n+1) 2-n

m bits of integer part n bits of fractional part

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.

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

Arithmetic - fix-point notation 37/47

Example of fix-point notations:

format negative values positive values accuracym.n min max min max

8.8 NB - - 0 28-2-8

255,9962-8

3,9E-38.8 C2 -(27)

-128-(2-8)

-3,9E-30 27-2-8

127,9962-8

3,9E-316.8 C2 -(215)

-32768-(2-8)

-3,9E-30 215-2-8

32767,9962-8

3,9E-316.16 C2 -(215)

-32768-(2-16)

-1,526E-50 215-2-16

32767,999982-16

1,526E-5

2. Addition and subtraction

same as on integer numbers

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

Arithmetic - fix-point notation 38/47

3. Multiplication of fix-point numbers

Examples let m=4, n=2:

A) 2,5 0010.10 2,5 0010.10 1 2 5 00101 00 partial products 5 0 001010 6,2 5 00000110.0100 sum of partial products, m’=8, n’=4 0110.01 normalised product

B) 6,2 5 0110.01 1,5 0001.10 3 1 2 5 0110010 partial products 6 2 5 011001 9,3 7 5 00001001.0110 sum of partial products, m’=8, n’=4 normalised product: 1001.01 = 9,25 - error at rounding down albo 1001.10 = 9,50 - error at rounding up

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

Arithmetic - fix-point notation 39/47

4. Division of fix-point numbers

Realised same as integer division.Example (format 4.2):2,00 : 1,25 = 1,60 1.1001 0001.10 = 1,50 - rounding error 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.

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

Arithmetic - floating-point notation 40/47

1. Floating-point notation - basis

Any real value L can be represented by L = m p e

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)

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

Arithmetic - floating-point notation 41/47

Mantissa is usually stored in SM code (separate sign bit).Exponent e is stored in C2 either in “shifted” C2:

exponent e: eC2 eC2+80h LNB(eC2+80h) 127 01111111 11111111 255 ... ... ... ... 2 00000010 10000010 130 1 00000001 10000001 129 0 00000000 10000000 128 -1 11111111 01111111 127 -2 11111110 01111110 126 ... ... ... ...

-127 10000001 00000001 1 -128 10000000 00000000 0

Shifted C2 simplifies calculation algorithms, but needs an attention during multiplication and division.

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

Arithmetic - floating-point notation 42/47

2. Formulas for basic calculations

if: L1 = m1pe1, L2 = m2pe2 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+

L1 L2 =

e2 < e1 if )2):((m1

e2 = e1 if )2(m1

e2 > e1 if )):2((m1

212

1

121

eee

e

eee

pmp

pm

ppm

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

Arithmetic - floating-point notation 43/47

3. Examples of FP notations

A)

mNB

eC2

m {0} {< 1/2 , 255/256 >} e < -128 , 127 >

value range: 0 , 2-129 , ... , 255 2119 0 , 1,47 10-39 , ... , 1,69 1038

B)

m0SM

S eC2+40h

m1SM

7 6 0

m {0} {< 1/2 , 1-2-16 >} e < -64 , 63 > value range: -65535 247 ... -(2-65) ,0,2-65 ... 65535 247 -9,221018...-2,7110-20,0,2,7110-20...9,221018

C)

S m2SM

m0SM

eC2+80h

m1SM

2322 16

m {0} {< 1/2 , 1-2-23 >} e < -128 , 127 > value range: -(223-1)2104 ... -2-129 ,0, 2-129 ...(223-1) 2104 -1,71038...-1,4710-39 ,0 , 1,4710-39...1,71038

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

Arithmetic - floating-point notation 44/47

D) m0SM

S eC2+80h

m1SM

31|30 23|22 16

m2SM

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,71038...-1,4710-39 ,0 , 1,4710-39...1,71038 wartość 0 m=0 i e=0

E) m0SM

S eC2+400h

m1SM

63|62 68|67 64

m6SM

m4SM m5SM

m2SM m3SM

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,810308...-2,7810-309 ,0 ,2,7810-309...1,810308 for value 0 m=0 i e=0

F) m0SM

S eC2+4000h

m1SM

79|78 64

m6SM

m4SM m5SM

m2SM m3SM

m7SM

I8087 real number extended value range: -(264-1)216319 ... -2-16385,0,2-16385 ...(264-1) 216319 -5,95104931...-4,210-4933 , 0 , 4,210-4933 ...5,95104931 for value 0 m=0 i e=0

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

Arithmetic - floating-point notation 45/47

4. Examples of algorithms

4.1. Addition and subtraction

R = mr p er = N1 N2 =

=

e2 < e1 if )2):((m1

e2 = e1 if )2(m1

e2 > e1 if )):2((m1

212

1

121

eee

e

eee

pmp

pm

ppm

<0 >0

=0

e:=e1-e2

e ?

er:=e1

m2:=m2:pe m1:=m1:p|e|

ew:=e2

mr:=m1m2

mr > p0 ? N Y

mr = 0 ?

N

Y

mr < p-1 ? N

Y

mr:=mrp er:=er-1

mr:=mr : p er:=er+1

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

Arithmetic - floating-point notation 46/47

4.2. Multiplication

R = mr p er = N1 N2 = m1 m2 p e1+e2

due to mantissas

m1 & m2 standardized

er:=e1+e2 mr:=m1 m2

mr = 0 ? Y N

mr < p-1 ?

er:=er-1 mr:=mr p

Y

N

m1 else m2 non-standardized

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

Arithmetic - floating-point notation 47/47

4.3. Division

R = mr p er = N1 : N2 = m1 : m2 p e1-e2

due to mantissas:

st

st

er:=e1-e2 mr:=m1 : m2

mr = 0 ? Y N

er:=er-1 mr:=mr p

mr < p-1 ?

Y

N

nst

m2 = 0 ? Y N error alert

mr > p0 ?

Y

N

er:=er+1 mr:=mr : p

nst

st - m1 & m2 standardized nst - m1 else m2 non-standardized