Download as pdf or txt
Download as pdf or txt
You are on page 1of 77

Workbook

Assembly Language Workbook


Co p yrig h t 2 0 0 0 - 2 0 0 2 Kip R. Irvin e . All rig h t s re s e rve d . No p a rt o f t h is we b
s it e m a y b e re p ro d u ce d , in a n y fo rm o r b y a n y m e a n s , wit h o u t p e rm is s io n in
writ in g fro m t h e a u t h o r ( kip @n u vis io n m ia m i. co m ) .

Us e t h e W o rk b o o k N o w

We lco m e t o t h e As s e m b ly La n g u a g e Wo rkb o o k, writ t e n b y Kip R. Irvin e t o s e rve a s a s u p p le m e n t t o


As s e m b ly La n g u a g e fo r I n t e l- Ba s e d Co m p u t e rs ( Pre n t ice - Ha ll) . By co m b in in g m y b o o k wit h t h e
wo rkb o o k e xe rcis e s , yo u s h o u ld h a ve a n e ve n g re a t e r ch a n ce o f s u cce s s in yo u r As s e m b ly La n g u a g e
co u rs e . Of co u rs e , t h e re is s t ill n o s u b s t it u t e fo r h a vin g a kn o wle d g e a b le , h e lp fu l in s t ru ct o r wh e n yo u a re
le a rn in g a p ro g ra m m in g la n g u a g e .

Ma n y o f t h e le s s o n s in vo lve b a s ic s kills t h a t s h o u ld b e p ra ct ice d a s s o o n a s yo u b e g in le a rn in g a s s e m b ly


la n g u a g e . Th e le s s o n s a re p la ce d in a m o re - o r- le s s lo g ica l o rd e r fro m e a s y t o d ifficu lt . Fo r e xa m p le , yo u
s h o u ld s t a rt wit h t h e fo llo win g t o p ics :

● Bin a ry a n d He xa d e cim a l Nu m b e rs
● S ig n e d In t e g e rs
● Re g is t e r Na m e s a n d Us a g e
● Us in g De b u g t o Writ e Pro g ra m s
● Us in g t h e Lin k Lib ra ry

Ma n y o f t h e t o p ics b e g in wit h a t u t o ria l a n d a re fo llo we d b y a s e t o f re la t e d e xe rcis e s . Ea ch e xe rcis e p a g e


is a cco m p a n ie d b y a co rre s p o n d in g p a g e wit h a ll o f t h e a n s we rs . Of co u rs e , yo u s h o u ld t ry t o d o t h e
e xe rcis e s firs t , wit h o u t lo o kin g a t t h e a n s we rs !

A n u m b e r o f t u t o ria ls we re a d d e d t o t h e wo rkb o o k fo r t o p ics n o t co ve re d in t h e b o o k. I fo u n d , b y


co rre s p o n d in g wit h o t h e r co lle g e p ro fe s s o rs , t h a t t h e y we re p ro vid in g t h e ir o wn h a n d o u t s fo r s e le ct e d
t o p ics . He re is a p a rt ia l lis t o f t u t o ria ls fo r t h e s e n e w t o p ics :

● Flo a t in g - Po in t Bin a ry
● Th e Pre cis io n Pro b le m
● Erro r- Co rre ct in g Co d e s
● Ca ch e Me m o ry
● Pip e lin in g
● S u p e rs ca la r Arch it e ct u re
● Bra n ch Pre d ict io n
● Us in g Pro g ra m m e rs Wo rkb e n ch

No d o u b t , a d d it io n a l t o p ics will a p p e a r t h ro u g h o u t t h e ye a r, s u ch a s 3 2 - b it fla t m o d e l p ro g ra m m in g . Th is


is a wo rkb o o k in p ro g re s s .

If yo u t h in k yo u 've fo u n d a m is t a ke , ve rify it wit h yo u r in s t ru ct o r a n d if it n e e d s co rre ct in g , p le a s e le t m e


kn o w rig h t a wa y. Yo u r in fo rm a t io n co u ld h e lp t h o u s a n d s o f o t h e r p e o p le .

Us e t h e W o rk b o o k N o w

http://www.nuvisionmiami.com/books/asm/workbook/index.htm [1/15/2003 4:43:42 PM]


Workbook Topics

Workbook Topics
Co p yrig h t 2 0 0 0 - 2 0 0 2 Kip R. Irvin e . All rig h t s re s e rve d . No p a rt o f t h is we b
s it e m a y b e re p ro d u ce d , in a n y fo rm o r b y a n y m e a n s , wit h o u t p e rm is s io n
in writ in g fro m t h e a u t h o r ( kip @n u vis io n m ia m i. co m ) .

1. Bin a ry a n d He xa d e cim a l In t e g e rs
2. S ig n e d In t e g e rs ( t u t o ria l)
3. S ig n e d In t e g e rs
4. Flo a t in g - Po in t Bin a ry ( t u t o ria l)
5. Flo a t in g - Po in t Bin a ry
6. Th e Pre cis io n Pro b le m ( t u t o ria l)
7. Re g is t e r a n d Im m e d ia t e Op e ra n d s
8. Ad d it io n a n d S u b t ra ct io n In s t ru ct io n s
9. Dire ct Me m o ry Op e ra n d s
10. In d ire ct a n d In d e xe d Op e ra n d s
11. Ma p p in g Va ria b le s t o Me m o ry
12. MS - DOS Fu n ct io n Ca lls , Pa rt 1
13. MS - DOS Fu n ct io n Ca lls , Pa rt 2
14. Erro r- Co rre ct in g Co d e s
15. Bo o le a n a n d Co m p a ris o n In s t ru ct io n s
16. De co d in g a 1 2 - b it FAT ( t u t o ria l)

1.

http://www.nuvisionmiami.com/books/asm/workbook/workbook.htm [1/15/2003 4:43:44 PM]


Binary and Hexadecimal Integers

Binary and Hexadecimal


Integers
Click h e re t o vie w t h e a n s we rs .

1 . Writ e e a ch o f t h e fo llo win g d e cim a l n u m b e rs in b in a ry:

a. 2 g. 15

b. 7 h. 16

c. 5 i. 20

d. 8 j. 27

e. 9 k. 32

f. 12 l. 64

2 . Writ e e a ch o f t h e fo llo win g b in a ry n u m b e rs in d e cim a l:

a.00000101 g.00110000

b.00001111 h.00100111

c.00010000 i.01000000

d.00010110 j.01100011

e.00001011 k.10100000

f.00011100 l.10101010

http://www.nuvisionmiami.com/books/asm/workbook/binhex.htm (1 of 3) [1/15/2003 4:43:45 PM]


Binary and Hexadecimal Integers

3 . Writ e e a ch o f t h e fo llo win g b in a ry n u m b e rs in h e xa d e cim a l:

a.00000101 g.00110000

b.00001111 h.00100111

c.00010000 i.01001000

d.00010110 j.01100011

e.00001011 k.10100000

f.00011100 l.10101011

4 . Writ e e a ch o f t h e fo llo win g h e xa d e cim a l n u m b e rs in b in a ry:

a. 0005hg. 0030h

b. 000Fhh. 0027h

c. 0010hi. 0048h

d. 0016hj. 0063h

e. 000Bhk. A064h

f. 001Chl. ABDEh

5 . Writ e e a ch o f t h e fo llo win g h e xa d e cim a l n u m b e rs in d e cim a l:

a. 00D5hg. 0B30h

b. 002Fhh. 06DFh

c. 0110hi. 1AB6h

http://www.nuvisionmiami.com/books/asm/workbook/binhex.htm (2 of 3) [1/15/2003 4:43:45 PM]


Binary and Hexadecimal Integers

d. 0216hj. 0A63h

e. 004Bhk. 02A0h

f. 041Chl. 1FABh

http://www.nuvisionmiami.com/books/asm/workbook/binhex.htm (3 of 3) [1/15/2003 4:43:45 PM]


Answers: Binary and Hexadecimal Numbers

An s w e rs : Bin a ry a n d He x a d e c im a l N u m b e rs

1 . Writ e e a ch o f t h e fo llo win g d e cim a l n u m b e rs in b in a ry.

Hin t : To co n ve rt a b in a ry n u m b e r t o it s d e cim a l e q u iva le n t , e va lu a t e e a ch d ig it p o s it io n a s a


p o we r o f 2 . Th e d e cim a l va lu e o f 2 0 is 1 , 2 1 is 2 , 2 2 is 4 , a n d s o o n . Fo r e xa m p le , t h e b in a ry
n u m b e r 1 1 1 1 is e q u a l t o 1 5 d e cim a l.

a. 2 = 00000010 g. 15 = 00001111

b. 7 = 00000111 h. 16 = 00010000

c. 5 = 00000101 i. 20 = 00010100

d. 8 = 00001000 j. 27 = 00011011

e. 9 = 00001001 k. 32 = 00100000

f. 12 = 00001100 l. 64 = 01000000

2 . Writ e e a ch o f t h e fo llo win g b in a ry n u m b e rs in d e cim a l:

Hin t : To ca lcu la t e t h e d e cim a l va lu e o f a b in a ry n u m b e r, a d d t h e va lu e o f e a ch b it p o s it io n


co n t a in in g a 1 t o t h e n u m b e r’s t o t a l va lu e . Fo r e xa m p le , t h e b in a ry n u m b e r 0 0 0 0 1 0 0 1 m a y
b e in t e rp re t e d in d e cim a l a s ( 1 * 2 3 ) + ( 1 * 2 0 ) .

a.00000101 = 5 g.00110000 = 48

b.00001111 = 15h.00100111 = 39

c.00010000 = 16i.01000000 = 64

d.00010110 = 22j.01100011 = 99

e.00001011 = 11k.10100000 = 160

f.00011100 = 28l.10101010 = 170

http://www.nuvisionmiami.com/books/asm/workbook/binhex_ans.htm (1 of 3) [1/15/2003 4:43:46 PM]


Answers: Binary and Hexadecimal Numbers

3 . Writ e e a ch o f t h e fo llo win g b in a ry n u m b e rs in h e xa d e cim a l:

Hin t : To ca lcu la t e t h e h e xa d e cim a l va lu e o f a b in a ry n u m b e r, t ra n s la t e e a ch g ro u p o f fo u r b it s t o


it s e q u iva le n t h e xa d e cim a l d ig it . Fo r e xa m p le , 1 1 0 0 = C, a n d 1 0 1 1 = B.

a.00000101 = 05hg.00110000 = 30h

b.00001111 = 0Fhh.00100111 = 27h

c.00010000 = 10hi.01001000 = 48h

d.00010110 = 16hj.01100011 = 63h

e.00001011 = 0Bhk.10100000 = A0h

f.00011100 = 1Chl.10101011 = ABh

4 . Writ e e a ch o f t h e fo llo win g h e xa d e cim a l n u m b e rs in b in a ry:

Hin t : To ca lcu la t e t h e b in a ry va lu e o f a h e xa d e cim a l n u m b e r, t ra n s la t e e a ch h e xa d e cim a l d ig it


in t o it s co rre s p o n d in g fo u r- b it b in a ry p a t t e rn . ( Yo u ca n a ls o t ra n s la t e t h e d ig it t o d e cim a l, a n d
t h e n co n ve rt it t o it s e q u iva le n t b in a ry b it p a t t e rn . ) Fo r e xa m p le , h e x C= 1 1 0 0 , a n d h e x B =
1011.

a.0005h = 00000101g.0030h = 00110000

b.000Fh = 00001111h.0027h = 00100111

c.0010h = 00010000i.0048h = 01001000

d.0016h = 00010110j.0063h = 01100011

e.000Bh = 00001011k.A064h = 10100000 01100100

f.001Ch = 00011100l.ABDEh = 10101011 11011110

5 . Writ e e a ch o f t h e fo llo win g h e xa d e cim a l n u m b e rs in d e cim a l:

http://www.nuvisionmiami.com/books/asm/workbook/binhex_ans.htm (2 of 3) [1/15/2003 4:43:46 PM]


Answers: Binary and Hexadecimal Numbers

Hin t : To ca lcu la t e t h e d e cim a l va lu e o f a h e xa d e cim a l n u m b e r, m u lt ip ly e a ch h e xa d e cim a l d ig it


b y it s co rre s p o n d in g p o we r o f 1 6 . Th e s u m o f t h e s e p ro d u ct s is t h e d e cim a l va lu e o f t h e n u m b e r.
Fo r e xa m p le , h e xa d e cim a l 1 2 A = ( 1 * 2 5 6 ) + ( 2 * 1 6 ) + ( 1 0 * 1 ) = 2 9 8 . Hin t : 1 6 0 = 1 , 1 6 1 = 1 6 , 1 6 2 =
2 5 6 , a n d 1 6 3 = 4 0 9 6 . Als o , yo u ca n u s e t h e fo llo win g He xa d e cim a l d ig it t a b le a s a n a id :

Ext e n d e d He xa d e cim a l Dig it s

A = 10 B = 11
C = 12 D = 13

E = 14 F = 15

An s we rs :

a.00D5h = 213 g.0B30h = 2864

b.002Fh = 47 h.06DFh = 1759

c.0110h = 272 i.1AB6h = 6838

d.0216h = 534 j.0A63h = 2659

e.004Bh = 75 k.02A0h = 672

f.041Ch = 1052l.1FABh = 8107

http://www.nuvisionmiami.com/books/asm/workbook/binhex_ans.htm (3 of 3) [1/15/2003 4:43:46 PM]


Signed Binary Representation

Tutorial: Signed Integers


In m a t h e m a t ics , t h e a d d it iv e in v e rs e o f a n u m b e r n is t h e va lu e , wh e n a d d e d t o n , p ro d u ce s ze ro . He re a re a fe w e xa m p le s , e xp re s s e d in
d e cim a l:

6 + –6 = 0

0 + 0 = 0

–1 + 1 = 0

Pro g ra m s o ft e n in clu d e b o t h s u b t ra ct io n a n d a d d it io n o p e ra t io n s , b u t in t e rn a lly, t h e CPU re a lly o n ly p e rfo rm s a d d it io n . To g e t a ro u n d t h is


re s t rict io n , t h e co m p u t e r u s e s t h e a d d it ive in ve rs e . Wh e n s u b t ra ct in g A – B, t h e CPU in s t e a d p e rfo rm s A + ( – B) . Fo r e xa m p le , t o s im u la t e t h e
s u b t ra ct io n o f 4 fro m 6 , t h e CPU a d d s – 4 t o 6 :

6 + –4 = 2

Bin a ry Tw o ’s Co m p le m e n t
Wh e n wo rkin g wit h b in a ry n u m b e rs , we u s e t h e t e rm t w o ’s co m p le m e n t t o re fe r t o a n u m b e r’s a d d it ive in ve rs e . Th e t wo ’s co m p le m e n t o f a
n u m b e r n is fo rm e d b y re ve rs in g n ’s b it s a n d a d d in g 1 . He re , fo r e xa m p le , n e q u a ls t h e 4 - b it n u m b e r 0 0 0 1 :

N: 0001

Re ve rs e N: 1110

Ad d 1 : 1111

Th e t wo ’s co m p le m e n t o f n , wh e n a d d e d t o n , p ro d u ce s ze ro :

0001 + 1111 = 0000

It d o e s n ’t m a t t e r h o w m a n y b it s a re u s e d b y n . Th e t wo ’s co m p le m e n t is fo rm e d u s in g t h e s a m e m e t h o d :

N= 1 00000001

Re ve rs e N: 11111110

Ad d 1 : 11111111

N= 1 0000000000000001

Re ve rs e N: 1111111111111110

Ad d 1 : 1111111111111111

He re a re s o m e e xa m p le s o f 8 - b it t wo ’s co m p le m e n t s :

n ( d e cim a l) n ( b in a ry) NEG( n ) ( d e cim a l)

http://www.nuvisionmiami.com/books/asm/workbook/signedbin_tut.htm (1 of 2) [1/15/2003 4:43:47 PM]


Signed Binary Representation

+2 00000010 11111110 –2

+ 16 00010000 11110000 –16

+ 127 01111111 10000001 –127

http://www.nuvisionmiami.com/books/asm/workbook/signedbin_tut.htm (2 of 2) [1/15/2003 4:43:47 PM]


Signed Integers

Signed Integers
Click h e re t o vie w a t u t o ria l t h a t h e lp s t o cla rify t h e re p re s e n t a t io n o f s ig n e d in t e g e rs u s in g t wo 's co m p le m e n t n o t a t io n . Click h e re t o vie w t h e
a n s we rs .

1 . Writ e e a ch o f t h e fo llo win g s ig n e d d e cim a l in t e g e rs in 8 - b it b in a ry n o t a t io n :

If a n y n u m b e r ca n n o t b e re p re s e n t e d a s a s ig n e d 8 - b it b in a ry n u m b e r, in d ica t e t h is in yo u r
a n s we r.

a. -2 e. +15

b. -7 f. -1

c. -128 g. -56

d. -16 h. +127

2 . Writ e e a ch o f t h e fo llo win g 8 - b it s ig n e d b in a ry in t e g e rs in d e cim a l:

a.11111111 g.00001111

b.11110000 h.10101111

c.10000000 i.11111100

d.10000001 j.01010101

3 . Wh ich o f t h e fo llo win g in t e g e rs a re va lid 1 6 - b it s ig n e d d e cim a l in t e g e rs ?

( in d ica t e V= va lid , I= in va lid )

a.+32469 d. +32785

b.+32767 e. -32785

http://www.nuvisionmiami.com/books/asm/workbook/signed.htm (1 of 2) [1/15/2003 4:43:47 PM]


Signed Integers

c.-32768 f. +65535

4 . In d ica t e t h e s ig n o f e a ch o f t h e fo llo win g 1 6 - b it h e xa d e cim a l in t e g e rs :

( in d ica t e P= p o s it ive , N= n e g a t ive )

a. 7FB9h c. 0D000h

b. 8123h d. 649Fh

5 . Writ e e a ch o f t h e fo llo win g s ig n e d d e cim a l in t e g e rs a s a 1 6 - b it h e xa d e cim a l va lu e :

a. -42 e. -32768

b. -127 f. -1

c. -4096 g. -8193

d. -16 h. -256

http://www.nuvisionmiami.com/books/asm/workbook/signed.htm (2 of 2) [1/15/2003 4:43:47 PM]


Answers: Signed Integers

An s w e rs : S ig n e d I n t e g e rs
1 . Writ e e a ch o f t h e fo llo win g s ig n e d d e cim a l in t e g e rs in 8 - b it b in a ry n o t a t io n :

Hin t : Re m o ve t h e s ig n , cre a t e t h e b in a ry re p re s e n t a t io n o f t h e n u m b e r, a n d t h e n co n ve rt it t o it s
t wo 's co m p le m e n t .

a.-2 = 11111110 e.+15 = 00001111

b.-7 = 11111001 f.-1 = 11111111

c.-128 = 10000000g.-56 = 11001000

d.-16 = 11110000 h.+127 = 01111111

2 . Writ e e a ch o f t h e fo llo win g 8 - b it s ig n e d b in a ry in t e g e rs in d e cim a l:

Hin t : If t h e h ig h e s t b it is s e t , co n ve rt t h e n u m b e r t o it s t wo 's co m p le m e n t , cre a t e t h e d e cim a l


re p re s e n t a t io n o f t h e n u m b e r, a n d t h e n p re p e n d a n e g a t ive s ig n t o t h e a n s we r.

a.11111111 = -1 g.00001111 = +15

b.11110000 = -16 h.10101111 = -81

c.10000000 = -128i.11111100 = -4

d.10000001 = -127j.01010101 = +85

3 . Wh ich o f t h e fo llo win g in t e g e rs a re va lid 1 6 - b it s ig n e d d e cim a l in t e g e rs ?

a.+32469 = Vd.+32785 = I

b.+32767 = Ve.-32785 = I

c.-32768 = Vf.+65535 = I

http://www.nuvisionmiami.com/books/asm/workbook/signed_ans.htm (1 of 2) [1/15/2003 4:43:48 PM]


Answers: Signed Integers

4 . In d ica t e t h e s ig n o f e a ch o f t h e fo llo win g 1 6 - b it h e xa d e cim a l in t e g e rs :

a.7FB9h = Pc.0D000h = N

b.8123h = Nd.649Fh = P

5 . Writ e e a ch o f t h e fo llo win g s ig n e d d e cim a l in t e g e rs a s a 1 6 - b it h e xa d e cim a l va lu e :

a.-42 = FFD6h e.-32768 = 8000h

b.-127 = FF81h f.-1 = FFFFh

c.-4096 = F000hg.-8193 = DFFFh

d.-16 = FFF0h h.-256 = FF00h

http://www.nuvisionmiami.com/books/asm/workbook/signed_ans.htm (2 of 2) [1/15/2003 4:43:48 PM]


Tutorial: Floating-Point Binary

Tutorial: Floating-Point Binary


Th e t wo m o s t co m m o n flo a t in g - p o in t b in a ry s t o ra g e fo rm a t s u s e d b y In t e l p ro ce s s o rs we re cre a t e d fo r In t e l a n d la t e r s t a n d a rd ize d b y t h e IEEE o rg a n iza t io n :

IEEE S h o rt Re a l: 3 2 b it s 1 b it fo r t h e s ig n , 8 b it s fo r t h e e xp o n e n t , a n d 2 3 b it s fo r t h e m a n t is s a . Als o ca lle d s in g le p re cis io n .

IEEE Lo n g Re a l: 6 4 b it s 1 b it fo r t h e s ig n , 1 1 b it s fo r t h e e xp o n e n t , a n d 5 2 b it s fo r t h e m a n t is s a . Als o ca lle d d o u b le p re cis io n .

Bo t h fo rm a t s u s e e s s e n t ia lly t h e s a m e m e t h o d fo r s t o rin g flo a t in g - p o in t b in a ry n u m b e rs , s o we will u s e t h e S h o rt Re a l a s a n


e xa m p le in t h is t u t o ria l. Th e b it s in a n IEEE S h o rt Re a l a re a rra n g e d a s fo llo ws , wit h t h e m o s t s ig n ifica n t b it ( MS B) o n t h e le ft :

Fig .
1

Th e S ig n
Th e s ig n o f a b in a ry flo a t in g - p o in t n u m b e r is re p re s e n t e d b y a s in g le b it . A 1 b it in d ica t e s a n e g a t ive n u m b e r, a n d a 0 b it in d ica t e s a
p o s it ive n u m b e r.

Th e Ma n t is s a
It is u s e fu l t o co n s id e r t h e wa y d e cim a l flo a t in g - p o in t n u m b e rs re p re s e n t t h e ir m a n t is s a . Us in g - 3 . 1 5 4 x 1 0 5 a s a n e xa m p le , t h e
s ig n is n e g a t ive , t h e m a n t is s a is 3 . 1 5 4 , a n d t h e e x p o n e n t is 5 . Th e fra ct io n a l p o rt io n o f t h e m a n t is s a is t h e s u m o f e a ch d ig it
m u lt ip lie d b y a p o we r o f 1 0 :

.154 = 1/ 10 + 5/ 100 + 4/ 1000

A b in a ry flo a t in g - p o in t n u m b e r is s im ila r. Fo r e xa m p le , in t h e n u m b e r + 1 1 . 1 0 1 1 x 2 3 , t h e s ig n is p o s it ive , t h e m a n t is s a is 1 1 . 1 0 1 1 ,


a n d t h e e xp o n e n t is 3 . Th e fra ct io n a l p o rt io n o f t h e m a n t is s a is t h e s u m o f s u cce s s ive p o we rs o f 2 . In o u r e xa m p le , it is e xp re s s e d
as:

.1011 = 1/ 2 + 0/ 4 + 1/ 8 + 1/ 16

Or, yo u ca n ca lcu la t e t h is va lu e a s 1 0 1 1 d ivid e d b y 2 4 . In d e cim a l t e rm s , t h is is e le ve n d ivid e d b y s ixt e e n , o r 0 . 6 8 7 5 . Co m b in e d


wit h t h e le ft - h a n d s id e o f 1 1 . 1 0 1 1 , t h e d e cim a l va lu e o f t h e n u m b e r is 3 . 6 8 7 5 . He re a re a d d it io n a l e xa m p le s :

Bin a ry Flo a t in g - P o in t Ba s e 1 0 Fra c t io n Ba s e 1 0 D e c im a l

11.11 3 3/4 3.75

0.00000000000000000000001 1/8388608 0.00000011920928955078125

Th e la s t e n t ry in t h is t a b le s h o ws t h e s m a lle s t fra ct io n t h a t ca n b e s t o re d in a 2 3 - b it m a n t is s a . Th e fo llo win g t a b le s h o ws a fe w


s im p le e xa m p le s o f b in a ry flo a t in g - p o in t n u m b e rs a lo n g s id e t h e ir e q u iva le n t d e cim a l fra ct io n s a n d d e cim a l va lu e s :

Bin a ry D e c im a l Fra c t io n D e c im a l Va lu e

http://www.nuvisionmiami.com/books/asm/workbook/floating_tut.htm (1 of 4) [1/15/2003 4:43:49 PM]


Tutorial: Floating-Point Binary

.1 1/2 .5

.01 1/4 .25


.001 1/8 .125

.0001 1/16 .0625


.00001 1/32 .03125

Th e Ex p o n e n t
IEEE S h o rt Re a l e xp o n e n t s a re s t o re d a s 8 - b it u n s ig n e d in t e g e rs wit h a b ia s o f 1 2 7 . Le t 's u s e t h e n u m b e r 1 . 1 0 1 x 2 5 a s a n e xa m p le .
Th e e xp o n e n t ( 5 ) is a d d e d t o 1 2 7 a n d t h e s u m ( 1 3 2 ) is b in a ry 1 0 1 0 0 0 1 0 . He re a re s o m e e xa m p le s o f e xp o n e n t s , firs t s h o wn in
d e cim a l, t h e n a d ju s t e d , a n d fin a lly in u n s ig n e d b in a ry:

Ad ju s t e d
Ex p o n e n t ( E) Bin a ry
(E + 127)

+5 132 10000100

0 127 01111111

-10 117 01110101

+ 128 255 11111111

-127 0 00000000
-1 126 01111110

Th e b in a ry e xp o n e n t is u n s ig n e d , a n d t h e re fo re ca n n o t b e n e g a t ive . Th e la rg e s t p o s s ib le e xp o n e n t is 1 2 8 - - wh e n a d d e d t o 1 2 7 , it
p ro d u ce s 2 5 5 , t h e la rg e s t u n s ig n e d va lu e re p re s e n t e d b y 8 b it s . Th e a p p ro xim a t e ra n g e is fro m 1 . 0 x 2 - 1 2 7 t o 1 . 0 x 2 + 1 2 8 .

N o rm a liz in g t h e Ma n t is s a
Be fo re a flo a t in g - p o in t b in a ry n u m b e r ca n b e s t o re d co rre ct ly, it s m a n t is s a m u s t b e n o rm a lize d . Th e p ro ce s s is b a s ica lly t h e s a m e
a s wh e n n o rm a lizin g a flo a t in g - p o in t d e cim a l n u m b e r. Fo r e xa m p le , d e cim a l 1 2 3 4 . 5 6 7 is n o rm a lize d a s 1 . 2 3 4 5 6 7 x 1 0 3 b y m o vin g
t h e d e cim a l p o in t s o t h a t o n ly o n e d ig it a p p e a rs b e fo re t h e d e cim a l. Th e e xp o n e n t e xp re s s e s t h e n u m b e r o f p o s it io n s t h e d e cim a l
p o in t wa s m o ve d le ft ( p o s it ive e xp o n e n t ) o r m o ve d rig h t ( n e g a t ive e xp o n e n t ) .

S im ila rly, t h e flo a t in g - p o in t b in a ry va lu e 1 1 0 1 . 1 0 1 is n o rm a lize d a s 1 . 1 0 1 1 0 1 x 2 3 b y m o vin g t h e d e cim a l p o in t 3 p o s it io n s t o t h e


le ft , a n d m u lt ip lyin g b y 2 3 . He re a re s o m e e xa m p le s o f n o rm a liza t io n s :

Bin a ry Va lu e N o rm a liz e d As Ex p o n e n t

1101.101 1.101101 3

.00101 1.01 -3

1.0001 1.0001 0

10000011.0 1.0000011 7

Yo u m a y h a ve n o t ice d t h a t in a n o rm a lize d m a n t is s a , t h e d ig it 1 a lwa ys a p p e a rs t o t h e le ft o f t h e d e cim a l p o in t . In fa ct , t h e le a d in g


1 is o m it t e d fro m t h e m a n t is s a in t h e IEEE s t o ra g e fo rm a t b e ca u s e it is re d u n d a n t .

http://www.nuvisionmiami.com/books/asm/workbook/floating_tut.htm (2 of 4) [1/15/2003 4:43:49 PM]


Tutorial: Floating-Point Binary

Cre a t in g t h e I EEE Bit Re p re s e n t a t io n


We ca n n o w co m b in e t h e s ig n , e xp o n e n t , a n d n o rm a lize d m a n t is s a in t o t h e b in a ry IEEE s h o rt re a l re p re s e n t a t io n . Us in g Fig u re 1 a s
a re fe re n ce , t h e va lu e 1 . 1 0 1 x 2 0 is s t o re d a s s ig n = 0 ( p o s it ive ) , m a n t is s a = 1 0 1 , a n d e xp o n e n t = 0 1 1 1 1 1 1 1 ( t h e e xp o n e n t va lu e
is a d d e d t o 1 2 7 ) . Th e "1 " t o t h e le ft o f t h e d e cim a l p o in t is d ro p p e d fro m t h e m a n t is s a . He re a re m o re e xa m p le s :

Bin a ry Va lu e Bia s e d Ex p o n e n t S ig n , Ex p o n e n t , Ma n t is s a

-1.11 127 1 01111111 11000000000000000000000

+ 1101.101 130 0 10000010 10110100000000000000000

-.00101 124 1 01111100 01000000000000000000000

+ 100111.0 132 0 10000100 00111000000000000000000

+ .0000001101011 120 0 01111000 10101100000000000000000

Co n v e rt in g D e c im a l Fra c t io n s t o Bin a ry Re a ls
If a d e cim a l fra ct io n ca n b e e a s ily re p re s e n t e d a s a s u m o f fra ct io n s in t h e fo rm ( 1 / 2 + 1 / 4 + 1 / 8 + . . . ) , it is fa irly e a s y t o d is co ve r
t h e co rre s p o n d in g b in a ry re a l. He re a re a fe w s im p le e xa m p le s

D e c im a l Fra c t io n Fa c t o re d As . . . Bin a ry Re a l

1/2 1/2 .1

1/4 1/4 .01


3/4 1/2 + 1/4 .11

1/8 1/8 .001


7/8 1/2 + 1/4 + 1/8 .111

3/8 1/4 + 1/8 .011


1/16 1/16 .0001

3/16 1/8 + 1/16 .0011

5/16 1/4 + 1/16 .0101

Of co u rs e , t h e re a l wo rld is n e ve r s o s im p le . A fra ct io n s u ch a s 1 / 5 ( 0 . 2 ) m u s t b e re p re s e n t e d b y a s u m o f fra ct io n s wh o s e


d e n o m in a t o rs a re p o we rs o f 2 . He re is t h e o u t p u t fro m a p ro g ra m t h a t s u b t ra ct s e a ch s u cce s ive fra ct io n fro m 0 . 2 a n d s h o ws e a ch
re m a in d e r. In fa ct , a n e xa ct va lu e is n o t fo u n d a ft e r cre a t in g t h e 2 3 m a n t is s a b it s . Th e re s u lt , h o we ve r, is a ccu ra t e t o 7 d ig it s . Th e
b la n k lin e s a re fo r fra ct io n s t h a t we re t o o la rg e t o b e s u b t ra ct e d fro m t h e re m a in in g va lu e o f t h e n u m b e r. Bit 1 , fo r e xa m p le , wa s
e q u a l t o . 5 ( 1 / 2 ) , wh ich co u ld n o t b e s u b t ra ct e d fro m 0 . 2 .

starting: 0.200000000000

1
2
3 subtracting 0.125000000000
remainder = 0.075000000000
4 subtracting 0.062500000000
remainder = 0.012500000000
5
6
7 subtracting 0.007812500000

http://www.nuvisionmiami.com/books/asm/workbook/floating_tut.htm (3 of 4) [1/15/2003 4:43:49 PM]


Tutorial: Floating-Point Binary

remainder = 0.004687500000
8 subtracting 0.003906250000
remainder = 0.000781250000
9
10
11 subtracting 0.000488281250
remainder = 0.000292968750
12 subtracting 0.000244140625
remainder = 0.000048828125
13
14
15 subtracting 0.000030517578
remainder = 0.000018310547

16 subtracting 0.000015258789
remainder = 0.000003051758
17
18
19 subtracting 0.000001907349
remainder = 0.000001144409
20 subtracting 0.000000953674
remainder = 0.000000190735
21
22
23 subtracting 0.000000119209
remainder = 0.000000071526

Mantissa: .00110011001100110011001

http://www.nuvisionmiami.com/books/asm/workbook/floating_tut.htm (4 of 4) [1/15/2003 4:43:49 PM]


Floating-Point Binary Representation

Floating-Point Binary Representation


Up d a t e d 9 / 3 0 / 2 0 0 2

Click h e re t o vie w t h e a n s we rs

1 . Fo r e a ch o f t h e fo llo win g b in a ry flo a t in g - p o in t n u m b e rs , s u p p ly t h e e q u iva le n t va lu e a s a b a s e 1 0 fra ct io n , a n d


t h e n a s a b a s e 1 0 d e cim a l. Th e firs t p ro b le m h a s b e e n d o n e fo r yo u :

Bin a ry Flo a t in g - P o in t Ba s e 1 0 Fra c t io n Ba s e 1 0 D e c im a l

1 . 1 0 1 ( s a m p le ) 1 5/8 1.625

11.11

1.1

101.001

1101.0101

1110.00111

10000.101011

111.0000011

11.000101

2 . Fo r e a ch o f t h e fo llo win g e xp o n e n t va lu e s , s h o wn h e re in d e cim a l, s u p p ly t h e a ct u a l b in a ry b it s t h a t wo u ld b e


u s e d fo r a n 8 - b it e xp o n e n t in t h e IEEE S h o rt Re a l fo rm a t . Th e firs t a n s we r h a s b e e n s u p p lie d fo r yo u :

Ex p o n e n t ( E) Bin a ry Re p re s e n t a t io n

2 ( s a m p le ) 10000001

-10

128

-1

3 . Fo r e a ch o f t h e fo llo win g flo a t in g - p o in t b in a ry n u m b e rs , s u p p ly t h e n o rm a lize d va lu e a n d t h e re s u lt in g e xp o n e n t .


Th e firs t a n s we r h a s b e e n s u p p lie d fo r yo u :

Bin a ry Va lu e N o rm a liz e d As Ex p o n e n t

1 0 0 0 0 . 1 1 ( s a m p le ) 1.000011 4

1101.101

http://www.nuvisionmiami.com/books/asm/workbook/floating.htm (1 of 2) [1/15/2003 4:43:50 PM]


Floating-Point Binary Representation

.00101

1.0001

10000011.0

.0000011001

4 . Fo r e a ch o f t h e fo llo win g flo a t in g - p o in t b in a ry e xa m p le s , s u p p ly t h e co m p le t e b in a ry re p re s e n t a t io n o f t h e n u m b e r


in IEEE S h o rt Re a l fo rm a t . Th e firs t a n s we r h a s b e e n s u p p lie d fo r yo u :

Bin a ry Va lu e S ig n , Ex p o n e n t , Ma n t is s a

- 1 . 1 1 ( s a m p le ) 1 01111111 11000000000000000000000

+ 1101.101

-.00101

+ 100111.0

+ .0000001101011

http://www.nuvisionmiami.com/books/asm/workbook/floating.htm (2 of 2) [1/15/2003 4:43:50 PM]


Answers: Floating-Point Binary

An s w e rs : Flo a t in g - P o in t Bin a ry
Up d a t e d 9 / 3 0 / 2 0 0 2

Th e re is n o s e ct io n o f t h e b o o k co ve rin g t h is t o p ic, s o click h e re t o vie w a t u t o ria l.

1. Fo r e a ch o f t h e fo llo win g b in a ry flo a t in g - p o in t n u m b e rs , s u p p ly t h e e q u iva le n t va lu e a s a b a s e 1 0 fra ct io n , a n d t h e n a s a b a s e 1 0 d e cim a l. Th e


firs t p ro b le m h a s b e e n d o n e fo r yo u :

Bin a ry Flo a t in g - P o in t Ba s e 1 0 Fra c t io n Ba s e 1 0 D e c im a l

1.101 1 5/8 1.625

11.11 3 3/4 3.75

1.1 1 1/2 1.5

101.001 5 1/8 5.125

1101.0101 13 5/16 13.3125

1110.00111 14 7/32 14.21875

10000.101011 16 43/64 16.671875

111.0000011 7 3/128 7.0234375

11.000101 3 5/64 3.078125

2 . Fo r e a ch o f t h e fo llo win g e xp o n e n t va lu e s , s h o wn h e re in d e cim a l, s u p p ly t h e a ct u a l b in a ry b it s t h a t wo u ld b e u s e d fo r a n 8 - b it e xp o n e n t in t h e


IEEE S h o rt Re a l fo rm a t . Th e firs t a n s we r h a s b e e n s u p p lie d fo r yo u :

Ex p o n e n t ( E) Bin a ry Re p re s e n t a t io n

2 10000001

5 10000100

0 01111111

-10 01110101

128 11111111

-1 01111110

3 . Fo r e a ch o f t h e fo llo win g flo a t in g - p o in t b in a ry n u m b e rs , s u p p ly t h e n o rm a lize d va lu e a n d t h e re s u lt in g e xp o n e n t . Th e firs t a n s we r h a s b e e n


s u p p lie d fo r yo u :

Bin a ry Va lu e N o rm a liz e d As Ex p o n e n t

10000.11 1.000011 4

1101.101 1.101101 3

.00101 1.01 -3

http://www.nuvisionmiami.com/books/asm/workbook/floating_ans.htm (1 of 2) [1/15/2003 4:43:51 PM]


Answers: Floating-Point Binary

1.0001 1.0001 0

10000011.0 1.0000011 7

.0000011001 1.1001 -6

4 . Fo r e a ch o f t h e fo llo win g flo a t in g - p o in t b in a ry e xa m p le s , s u p p ly t h e co m p le t e b in a ry re p re s e n t a t io n o f t h e n u m b e r in IEEE S h o rt Re a l fo rm a t .


Th e firs t a n s we r h a s b e e n s u p p lie d fo r yo u :

Bin a ry Va lu e S ig n , Ex p o n e n t , Ma n t is s a

-1.11 1 01111111 11000000000000000000000

+ 1101.101 0 10000010 10110100000000000000000

-.00101 1 01111100 01000000000000000000000

+ 100111.0 0 10000100 00111000000000000000000

+ .0000001101011 0 01111000 10101100000000000000000

http://www.nuvisionmiami.com/books/asm/workbook/floating_ans.htm (2 of 2) [1/15/2003 4:43:51 PM]


Tutorial: The Precision Problem

Tutorial: The Precision


Problem
Yo u n e e d t o re a d t h e p re vio u s t o p ic, Flo a t in g - Po in t Bin a ry Nu m b e rs b e fo re re a d in g t h is t o p ic.

Th e a ccu ra cy o f a d e cim a l fra ct io n is lim it e d b y h o w clo s e ly it ca n b e re p re s e n t e d b y a s u m o f fra ct io n s b a s e d o n t h e


p o we rs o f t wo . Fo r e xa m p le , t h e d e cim a l va lu e o f 1 / 1 2 8 is . 0 0 7 8 1 2 5 . In t h e 2 3 - b it m a n t is s a o f a n IEEE S h o rt Re a l
s t o ra g e fo rm a t , t h e n e xt la rg e s t n u m b e r t h a t ca n b e s t o re d is t h e s u m o f 1 / 1 2 8 a n d 1 / 2 2 3 :

1/128 0.00781250000000000000000

1/ 223 p lu s : 0 . 0 0 0 0 0 0 1 1 9 2 0 9 2 8 9 5 5 0 7 8 1 2 5

1/ 128 + 1/ 223 e q u a ls : 0 . 0 0 7 8 1 2 6 1 9 2 0 9 2 8 9 5 5 0 7 8 1 2 5

Wh a t if yo u wa n t t o s t o re a n u m b e r s u ch a s 0 . 0 0 7 8 1 2 5 1 ? In fa ct , it is im p o s s ib le t o d o s o wit h a 2 3 b it m a n t is s a .
Th e re is n o a va ila b le fra ct io n t h a n ca n b e a d d e d t o 1 / 1 2 8 t h a t will g ive u s t h is n u m b e r. Th e d iffe re n ce , . 0 0 0 0 0 0 0 1 ,
is s m a lle r t h a n 1 / 2 2 3 ( a p p ro xim a t e ly 0 . 0 0 0 0 0 0 1 1 9 2 0 9 2 8 9 5 5 0 8 ) .

A m o re a ccu ra t e b in a ry re p re s e n t a t io n o f 0 . 0 0 7 8 1 2 5 1 , u s in g 5 2 b it s , wo u ld b e :
.0000001000000000000000000010101011110011000111011100.

Th e re is cle a rly n o wa y t h a t we ca n a ccu ra t e ly re p re s e n t t h is n u m b e r wit h o n ly 2 3 b it s .

Wh e n t h e S h o rt Re a l d a t a t yp e is im p le m e n t e d b y h ig h - le ve l la n g u a g e co m p ile rs , t h e g u a ra n t e e d ra n g e o f p re cis io n
is 7 d ig it s . Th is is b e ca u s e n o n u m b e r m o re p re cis e t h a t ra n g e ca n b e t ru s t e d .

Un le s s yo u 're a n e n g in e e r, yo u p ro b a b ly wo n 't lo s e m u ch s le e p o ve r p ro b le m s wit h b in a ry p re cis io n . Bu t b e ca re fu l:


s e rio u s e rro rs o ccu r in a n y p ro g ra m t h a t d o e s a s u cce s s ive s e rie s o f flo a t in g - p o in t ca lcu la t io n s in wh ich e a ch n e w
ca lcu la t io n is b a s e d o n t h e re s u lt s o f a p re vio u s ca lcu la t io n . Th e s m a ll e rro rs in e a ch ca lcu la t io n a re co m p o u n d e d t o
t h e p o in t wh e re t h e p ro g ra m 's o u t p u t b e a rs n o re la t io n t o re a lit y.

http://www.nuvisionmiami.com/books/asm/workbook/precision.htm [1/15/2003 4:43:52 PM]


Register and Immediate Operands

Register and Immediate


Operands
Th is t o p ic co ve rs t h e MOV in s t ru ct io n , a p p lie d t o re g is t e r a n d im m e d ia t e o p e ra n d s . Click h e re t o vie w t h e a n s we rs .

1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,bx g.mov al,dh

b.mov dx,bl h.mov ax,dh

c.mov ecx,edxi.mov ip,ax

d.mov si,di j.mov si,cl

e.mov ds,ax k.mov edx,ax

f.mov ds,es l.mov ax,es

2 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,16 g.mov 123,dh

b.mov dx,7F65h h.mov ss,ds

c.mov ecx,6F23458hi.mov 0FABh,ax

d.mov si,-1 j.mov si,cl

http://www.nuvisionmiami.com/books/asm/workbook/regimmed.htm (1 of 2) [1/15/2003 4:43:53 PM]


Register and Immediate Operands

e.mov ds,1000h k.mov edx,esi

f.mov al,100h l.mov edx,-2

http://www.nuvisionmiami.com/books/asm/workbook/regimmed.htm (2 of 2) [1/15/2003 4:43:53 PM]


Register and Immediate Operands

An s w e rs : Re g is t e r a n d I m m e d ia t e
Op e ra n d s
1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,bx V g. mov al,dh V

b.mov dx,bl I h. mov ax,dh I

c.mov ecx,edxV i. mov ip,ax I

d.mov si,di V j. mov si,cl I

e.mov ds,ax V k. mov edx,axI

f.mov ds,es I l. mov ax,es V

2 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,16 V g.mov 123,dh I

b.mov dx,7F65h V h.mov ss,ds I

c.mov ecx,6F23458hV i.mov 0FABh,ax I

d.mov si,-1 V j.mov si,cl I

e.mov ds,1000h I k.mov edx,esi V

f.mov al,100h I l.mov edx,-2 V

http://www.nuvisionmiami.com/books/asm/workbook/regimmed_ans.htm [1/15/2003 4:43:54 PM]


Addition and Subtraction Instructions

Addition and Subtraction


Instructions
Th is t o p ic co ve rs t h e ADD, S UB, INC, a n d DEC in s t ru ct io n s , a p p lie d t o re g is t e r a n d im m e d ia t e o p e ra n d s . Click h e re t o vie w t h e a n s we rs .

1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g in s t ru ct io n s is va lid .

( n o t a t e : V = va lid , I = in va lid ) As s u m e t h a t a ll o p e ra t io n s a re u n s ig n e d .

a.add ax,bx

b.add dx,bl

c.add ecx,dx

d.sub si,di

e.add bx,90000

f.sub ds,1

g. dec ip

h. dec edx

i. add edx,1000h

j. sub ah,126h

k. sub al,256

l. inc ax,1

2 . Wh a t will b e t h e va lu e o f t h e Ca rry fla g a ft e r e a ch o f t h e fo llo win g in s t ru ct io n s e q u e n ce s h a s e xe cu t e d ?

http://www.nuvisionmiami.com/books/asm/workbook/addsub.htm (1 of 5) [1/15/2003 4:43:55 PM]


Addition and Subtraction Instructions

( n o t a t e : CY = ca rry, NC = n o ca rry)

a. mov ax,0FFFFh
add ax,1

b. mov bh,2
sub bh,2

c. mov dx,0
dec dx

d. mov al,0DFh
add al,32h

e. mov si,0B9F6h
sub si,9874h

f. mov cx,695Fh
sub cx,A218h

3 . Wh a t will b e t h e va lu e o f t h e Ze ro fla g a ft e r e a ch o f t h e fo llo win g in s t ru ct io n s e q u e n ce s h a s e xe cu t e d ?

( n o t a t e : ZR = ze ro , NZ = n o t ze ro )

a. mov ax,0FFFFh
add ax,1

b. mov bh,2
sub bh,2

c. mov dx,0
dec dx

d. mov al,0DFh
add al,32h

e. mov si,0B9F6h
sub si,9874h

http://www.nuvisionmiami.com/books/asm/workbook/addsub.htm (2 of 5) [1/15/2003 4:43:55 PM]


Addition and Subtraction Instructions

f. mov cx,695Fh
add cx,96A1h

4 . Wh a t will b e t h e va lu e o f t h e S ig n fla g a ft e r e a ch o f t h e fo llo win g in s t ru ct io n s e q u e n ce s h a s e xe cu t e d ?

( n o t a t e : PL = p o s it ive , NG = n e g a t ive )

a. mov ax,0FFFFh
sub ax,1

b. mov bh,2
sub bh,3

c. mov dx,0
dec dx

d. mov ax,7FFEh
add ax,22h

e. mov si,0B9F6h
sub si,9874h

f. mov cx,8000h
add cx,A69Fh

5 . Wh a t will b e t h e va lu e s o f t h e Ca rry, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : CY/ NC, PL/ NG, ZR/ NZ)

mov ax,620h
sub ah,0F6h

6 . Wh a t will b e t h e va lu e s o f t h e Ca rry, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : CY/ NC, PL/ NG, ZR/ NZ)

http://www.nuvisionmiami.com/books/asm/workbook/addsub.htm (3 of 5) [1/15/2003 4:43:55 PM]


Addition and Subtraction Instructions

mov ax,720h
sub ax,0E6h

7 . Wh a t will b e t h e va lu e s o f t h e Ca rry, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : CY/ NC, PL/ NG, ZR/ NZ)

mov ax,0B6D4h
add al,0B3h

8 . Wh a t will b e t h e va lu e s o f t h e Ove rflo w, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : OV/ NV, PL/ NG, ZR/ NZ)

mov bl,-127
dec bl

9 . Wh a t will b e t h e va lu e s o f t h e Ca rry, Ove rflo w, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve


e xe cu t e d ?

( n o t a t e : CY/ NC, OV/ NV, PL/ NG, ZR/ NZ)

mov cx,-4097
add cx,1001h

1 0 . Wh a t will b e t h e va lu e s o f t h e Ca rry, Ove rflo w, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve


e xe cu t e d ?

( n o t a t e : CY/ NC, OV/ NV, PL/ NG, ZR/ NZ)

http://www.nuvisionmiami.com/books/asm/workbook/addsub.htm (4 of 5) [1/15/2003 4:43:55 PM]


Addition and Subtraction Instructions

mov ah,-56
add ah,-60

http://www.nuvisionmiami.com/books/asm/workbook/addsub.htm (5 of 5) [1/15/2003 4:43:55 PM]


Answers: Addition and Subtraction Instructions

An s w e rs :Ad d it io n a n d S u b t ra c t io n
I n s t ru c t io n s
1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g in s t ru ct io n s is va lid .

a.add ax,bx V

b.add dx,bl Ioperand size mismatch

c.add ecx,dx I

d.sub si,di V

e.add bx,90000 Isource too large

f.sub ds,1 Icannot use segment reg

g. dec ip Icannot modify IP

h. dec edx V

i. add edx,1000hV

j. sub ah,126h Isource too large

k. sub al,256 Isource too large

l. inc ax,1 Iextraneous operand

2 . Wh a t will b e t h e va lu e o f t h e Ca rry fla g a ft e r e a ch o f t h e fo llo win g in s t ru ct io n s e q u e n ce s h a s e xe cu t e d ?

( n o t a t e : CY = ca rry, NC = n o ca rry)

a.mov ax,0FFFFh
CY
add ax,1

http://www.nuvisionmiami.com/books/asm/workbook/addsub_ans.htm (1 of 4) [1/15/2003 4:43:56 PM]


Answers: Addition and Subtraction Instructions

b.mov bh,2
NC
sub bh,2

c.mov dx,0
?? (Carry not affected by INC and DEC)
dec dx

d.mov al,0DFh
CY
add al,32h

e.mov si,0B9F6h
NC
sub si,9874h

f.mov cx,695Fh
CY
sub cx,A218h

3 . Wh a t will b e t h e va lu e o f t h e Ze ro fla g a ft e r e a ch o f t h e fo llo win g in s t ru ct io n s e q u e n ce s h a s e xe cu t e d ?

( n o t a t e : ZR = ze ro , NZ = n o t ze ro )

a. mov ax,0FFFFh
ZR
add ax,1

b. mov bh,2
ZR
sub bh,2

c. mov dx,0
NZ
dec dx

d. mov al,0DFh
NZ
add al,32h

e. mov si,0B9F6h
NZ
sub si,9874h

f. mov cx,695Fh
ZR
add cx,96A1h

http://www.nuvisionmiami.com/books/asm/workbook/addsub_ans.htm (2 of 4) [1/15/2003 4:43:56 PM]


Answers: Addition and Subtraction Instructions

4 . Wh a t will b e t h e va lu e o f t h e S ig n fla g a ft e r e a ch o f t h e fo llo win g in s t ru ct io n s e q u e n ce s h a s e xe cu t e d ?

( n o t a t e : PL = p o s it ive , NG = n e g a t ive )

a. mov ax,0FFFFh
PL
sub ax,1

b. mov bh,2
NG
sub bh,3

c. mov dx,0
NG
dec dx

d. mov ax,7FFEh
NG
add ax,22h

e. mov si,0B9F6h
PL
sub si,9874h

f. mov cx,8000h
PL
add cx,A69Fh

5 . Wh a t will b e t h e va lu e s o f t h e Ca rry, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : CY/ NC, PL/ NG, ZR/ NZ)

mov ax,620h
sub ah,0F6h CY,PL,NZ

6 . Wh a t will b e t h e va lu e s o f t h e Ca rry, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : CY/ NC, PL/ NG, ZR/ NZ)

mov ax,720h
sub ax,0E6h NC,PL,NZ

http://www.nuvisionmiami.com/books/asm/workbook/addsub_ans.htm (3 of 4) [1/15/2003 4:43:56 PM]


Answers: Addition and Subtraction Instructions

7 . Wh a t will b e t h e va lu e s o f t h e Ca rry, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : CY/ NC, PL/ NG, ZR/ NZ)

mov ax,0B6D4h
add al,0B3h CY,NG,NZ

8 . Wh a t will b e t h e va lu e s o f t h e Ove rflo w, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve e xe cu t e d ?

( n o t a t e : OV/ NV, PL/ NG, ZR/ NZ)

mov bl,-127
dec bl NV,NG,NZ

9 . Wh a t will b e t h e va lu e s o f t h e Ca rry, Ove rflo w, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve


e xe cu t e d ?

( n o t a t e : CY/ NC, OV/ NV, PL/ NG, ZR/ NZ)

mov cx,-4097
add cx,1001h CY,NV,PL,ZR

1 0 . Wh a t will b e t h e va lu e s o f t h e Ca rry, Ove rflo w, S ig n , a n d Ze ro fla g s a ft e r t h e fo llo win g in s t ru ct io n s h a ve


e xe cu t e d ?

( n o t a t e : CY/ NC, OV/ NV, PL/ NG, ZR/ NZ)

mov ah,-56
add ah,-60 CY,NV,NG,NZ

http://www.nuvisionmiami.com/books/asm/workbook/addsub_ans.htm (4 of 4) [1/15/2003 4:43:56 PM]


Direct Memory Operands

Direct Memory Operands


Up d a t e d 9 / 3 0 / 2 0 0 2

Th is t o p ic co ve rs t h e MOV in s t ru ct io n , a p p lie d t o d ire ct m e m o ry o p e ra n d s a n d o p e ra n d s wit h d is p la ce m e n t s . Click h e re t o vie w t h e a n s we rs .

Us e t h e fo llo win g d a t a d e cla ra t io n s fo r Qu e s t io n s 1 - 4 . As s u m e t h a t t h e o ffs e t o f b yt e Va l is 0 0 0 0 0 0 0 0 h , a n d t h a t a ll


co d e ru n s in Pro t e ct e d m o d e .

.data
byteVal BYTE 1,2,3,4
wordVal WORD 1000h,2000h,3000h,4000h
dwordVal DWORD 12345678h,34567890h
aString BYTE "ABCDEFG",0

1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,byteVal

b.mov dx,wordVal

c.mov ecx,dwordVal

d.mov si,aString

e.mov esi,offset aString

f.mov al,byteVal

2 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov eax,offset byteVal

http://www.nuvisionmiami.com/books/asm/workbook/directop.htm (1 of 4) [1/15/2003 4:43:57 PM]


Direct Memory Operands

b.mov dx,wordVal+2

c.mov ecx,offset dwordVal

d.mov si,dwordVal

e.mov esi,offset aString+2

f.mov al,offset byteVal+1

3 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.mov eax,offset byteVal

b.mov dx,wordVal

c.mov ecx,dwordVal

d.mov esi,offset wordVal

e.mov esi,offset aString

f.mov al,aString+2

g. mov edi,offset dwordVal

4 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.mov eax,offset byteVal+2

b.mov dx,wordVal+4

http://www.nuvisionmiami.com/books/asm/workbook/directop.htm (2 of 4) [1/15/2003 4:43:57 PM]


Direct Memory Operands

c.mov ecx,dwordVal+4

d.mov esi,offset wordVal+4

e.mov esi,offset aString-1

Us e t h e fo llo win g d a t a d e cla ra t io n s fo r Qu e s t io n s 5 - 6 . As s u m e t h a t t h e o ffs e t o f b yt e Va l is 0 0 0 0 :

.data
byteVal BYTE 3 DUP(0FFh),2,"XY"
wordVal WORD 2 DUP(6),2
dwordVal DWORD 8,7,6,5
dwordValSiz WORD ($ - dwordVal)
ptrByte DWORD byteVal
ptrWord DWORD wordVal

5 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a. mov eax,offset wordVal

b. mov dx,wordVal+4

c. mov ecx,dwordVal+4

d. mov si,dwordValSiz

e. mov al,byteVal+4

6 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

http://www.nuvisionmiami.com/books/asm/workbook/directop.htm (3 of 4) [1/15/2003 4:43:57 PM]


Direct Memory Operands

a.mov ax,dwordVal+2

b.mov dx,wordVal-2

c.mov eax,ptrByte

d.mov esi,ptrWord

e.mov edi,offset dwordVal+2

http://www.nuvisionmiami.com/books/asm/workbook/directop.htm (4 of 4) [1/15/2003 4:43:57 PM]


Answers: Direct Memory Operands

An s w e rs : D ire c t Me m o ry Op e ra n d s
Up d a t e d 9 / 3 0 / 2 0 0 2

Us e t h e fo llo win g d a t a d e cla ra t io n s fo r Qu e s t io n s 1 - 4 . As s u m e t h a t t h e o ffs e t o f b yt e Va l is 0 0 0 0 0 0 0 0 h , a n d t h a t a ll


co d e ru n s in Pro t e ct e d m o d e .

.data
byteVal BYTE 1,2,3,4
wordVal WORD 1000h,2000h,3000h,4000h
dwordVal DWORD 12345678h,34567890h
aString BYTE "ABCDEFG",0

1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,byteVal I

b.mov dx,wordVal V

c.mov ecx,dwordVal V

d.mov si,aString I

e.mov esi,offset aString V

f.mov al,byteVal V

2 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g MOV in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov eax,offset byteVal V

b.mov dx,wordVal+2 V

http://www.nuvisionmiami.com/books/asm/workbook/directop_ans.htm (1 of 4) [1/15/2003 4:43:58 PM]


Answers: Direct Memory Operands

c.mov ecx,offset dwordVal V

d.mov si,dwordVal I

e.mov esi,offset aString+2 V

f.mov al,offset byteVal+1 I

3 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.mov ax,offset byteVal 00000000h

b.mov dx,wordVal 1000h

c.mov ecx,dwordVal 12345678h

d.mov esi,offset wordVal 00000004h

e.mov esi,offset aString 00000014h

f.mov al,aString+2 43h ('C')

g. mov edi,offset dwordVal 0000000Ch

4 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.mov eax,offset byteVal+2 00000002h

b.mov dx,wordVal+4 3000h

c.mov ecx,dwordVal+4 34567890h

http://www.nuvisionmiami.com/books/asm/workbook/directop_ans.htm (2 of 4) [1/15/2003 4:43:58 PM]


Answers: Direct Memory Operands

d.mov esi,offset wordVal+4 00000008h

e.mov esi,offset aString-1 00000013h

Us e t h e fo llo win g d a t a d e cla ra t io n s fo r Qu e s t io n s 5 - 6 . As s u m e t h a t t h e o ffs e t o f b yt e Va l is 0 0 0 0 :

.data
byteVal BYTE 3 DUP(0FFh),2,"XY"
wordVal WORD 2 DUP(6),2
dwordVal DWORD 8,7,6,5
dwordValSiz WORD ($ - dwordVal)
ptrByte DWORD byteVal
ptrWord DWORD wordVal

5 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.mov eax,offset wordVal 00000006h

b.mov dx,wordVal+4 0002h

c.mov ecx,dwordVal+4 00000007h

d.mov si,dwordValSiz 0010h

e.mov al,byteVal+4 58h('X')

6 . In d ica t e t h e h e xa d e cim a l va lu e m o ve d t o t h e d e s t in a t io n o p e ra n d b y e a ch o f t h e fo llo win g MOV in s t ru ct io n s :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.mov ax,dwordVal+2 I

http://www.nuvisionmiami.com/books/asm/workbook/directop_ans.htm (3 of 4) [1/15/2003 4:43:58 PM]


Answers: Direct Memory Operands

b.mov dx,wordVal-2 5958h ("YX")*

c.mov eax,ptrByte 00000000h

d.mov esi,ptrWord 00000006h

e.mov edi,offset dwordVal+2 0000000Eh

* Th e t w o ch a ra ct e r b y t e s a re a u t o m a t ica lly re v e rs e d w h e n lo a d e d in t o a 1 6 - b it re g is t e r.

http://www.nuvisionmiami.com/books/asm/workbook/directop_ans.htm (4 of 4) [1/15/2003 4:43:58 PM]


Indirect and Indexed Operands

Indirect and Indexed


Operands
Th is t o p ic co ve rs t h e MOV in s t ru ct io n , a p p lie d t o in d ire ct , b a s e d , a n d in d e xe d m e m o ry o p e ra n d s . Click h e re t o vie w t h e a n s we rs .

Us e t h e fo llo win g d a t a d e cla ra t io n s . As s u m e t h a t t h e o ffs e t o f b y t e Va l is 0 0 0 0 :

.data
byteVal db 1,2,3,4
wordVal dw 1000h,2000h,3000h,4000h
dwordVal dd 12345678h,34567890h
aString db "ABCDEFG",0
pntr dw wordVal

1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,byteVal[si]

b.add dx,[cx+wordVal]

c.mov ecx,[edi+dwordVal]

d.xchg al,[bx]

e.mov ax,[bx+4]

f.mov [bx],[si]

g. xchg al,byteVal[dx]

2 . In d ica t e t h e h e xa d e cim a l va lu e o f t h e fin a l d e s t in a t io n o p e ra n d a ft e r e a ch o f t h e fo llo win g co d e fra g m e n t s h a s


e xe cu t e d :

http://www.nuvisionmiami.com/books/asm/workbook/indirectop.htm (1 of 3) [1/15/2003 4:43:59 PM]


Indirect and Indexed Operands

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.mov si,offset byteVal


mov al,[si+1]

b.mov di,6
mov dx,wordVal[di]

c.mov bx,4
mov ecx,[bx+dwordVal]

d.mov si,offset aString


mov al,byteVal+1
mov [si],al

e.mov si,offset aString+2


inc byte ptr [si]

f.mov bx,pntr
add word ptr [bx],2

g. mov di,offset pntr


mov si,[di]
mov ax,[si+2]

3 . In d ica t e t h e h e xa d e cim a l va lu e o f t h e fin a l d e s t in a t io n o p e ra n d a ft e r e a ch o f t h e fo llo win g co d e fra g m e n t s h a s


e xe cu t e d :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.xchg si,pntr
xchg [si],wordVal

b.mov ax,pntr
xchg ax,si
mov dx,[si+4]

http://www.nuvisionmiami.com/books/asm/workbook/indirectop.htm (2 of 3) [1/15/2003 4:43:59 PM]


Indirect and Indexed Operands

c.mov edi,0
mov di,pntr
add edi,8
mov eax,[edi]

d.mov esi,offset aString


xchg esi,pntr
mov dl,[esi]

e.mov esi,offset aString


mov dl,[esi+2]

http://www.nuvisionmiami.com/books/asm/workbook/indirectop.htm (3 of 3) [1/15/2003 4:43:59 PM]


Answers: Indirect and Indexed Operands

An s w e rs : I n d ire c t a n d I n d e x e d Op e ra n d s
Us e t h e fo llo win g d a t a d e cla ra t io n s . As s u m e t h a t t h e o ffs e t o f b yt e Va l is 0 0 0 0 :

.data
byteVal db 1,2,3,4
wordVal dw 1000h,2000h,3000h,4000h
dwordVal dd 12345678h,34567890h
aString db "ABCDEFG",0
pntr dw wordVal

1 . In d ica t e wh e t h e r o r n o t e a ch o f t h e fo llo win g in s t ru ct io n s is va lid :

( n o t a t e : V = va lid , I = in va lid )

a.mov ax,byteVal[si] I (operand size mismatch)

b.add dx,[cx+wordVal] I (CX is not a base


or index register)

c.mov ecx,[edi+dwordVal]V

d.xchg al,[bx] V

e.mov ax,[bx+4] V

f.mov [bx],[si] I (memory to memory


not permitted)

g. xchg al,byteVal[dx] I (DX is not a base


or index register)

2 . In d ica t e t h e h e xa d e cim a l va lu e o f t h e fin a l d e s t in a t io n o p e ra n d a ft e r e a ch o f t h e fo llo win g co d e fra g m e n t s h a s


e xe cu t e d :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

http://www.nuvisionmiami.com/books/asm/workbook/indirectop_ans.htm (1 of 3) [1/15/2003 4:44:00 PM]


Answers: Indirect and Indexed Operands

a.mov si,offset byteVal


mov al,[si+1] 2

b.mov di,6
mov dx,wordVal[di] 4000h

c.mov bx,4
mov ecx,[bx+dwordVal] 34567890h

d.mov si,offset aString


mov al,byteVal+1
mov [si],al 2

e.mov si,offset aString+2


inc byte ptr [si] 44h('D')

f.mov bx,pntr
add word ptr [bx],2 1002h

g. mov di,offset pntr


mov si,[di]
mov ax,[si+2] 2000h

3 . In d ica t e t h e h e xa d e cim a l va lu e o f t h e fin a l d e s t in a t io n o p e ra n d a ft e r e a ch o f t h e fo llo win g co d e fra g m e n t s h a s


e xe cu t e d :

( If a n y in s t ru ct io n is in va lid , in d ica t e "I" a s t h e a n s we r. )

a.xchg si,pntr I (memory to memory


xchg [si],wordVal not permitted)

b.mov ax,pntr
xchg ax,si
mov dx,[si+4] dx = 3000h

http://www.nuvisionmiami.com/books/asm/workbook/indirectop_ans.htm (2 of 3) [1/15/2003 4:44:00 PM]


Answers: Indirect and Indexed Operands

c.mov edi,0
mov di,pntr
add edi,8
mov eax,[edi] 12345678h

d.mov esi,offset aStringI (esi and pntr


xchg esi,pntr have different
mov dl,[esi] sizes)

e.mov esi,offset aString


mov dl,[esi+2] 43h ('C')

http://www.nuvisionmiami.com/books/asm/workbook/indirectop_ans.htm (3 of 3) [1/15/2003 4:44:00 PM]


Mapping Variables to Memory

Mapping Variables to
Memory
Wh e n yo u 're t ryin g t o le a rn h o w t o a d d re s s m e m o ry, t h e firs t ch a lle n g e is t o h a ve a cle a r m e n t a l p ict u re o f t h e s t o ra g e ( t h e m a p p in g ) o f va ria b le s
t o m e m o ry lo ca t io n s .

Us e t h e fo llo win g d a t a d e cla ra t io n s , a n d a s s u m e t h a t t h e o ffs e t o f a rra yW is 0 0 0 0 :

.data
arrayW WORD 1234h,5678h,9ABCh
ptr1 WORD offset arrayD
arrayB BYTE 10h,20h,30h,40h
arrayD DWORD 40302010h

Click h e re t o vie w a m e m o ry m a p p in g t a b le ( GIF) . Rig h t - click h e re t o d o wn lo a d t h e s a m e t a b le a s a n Ad o b e Acro b a t


file . Prin t t h is t a b le , a n d fill in t h e h e xa ce cim a l co n t e n t s o f e ve ry m e m o ry lo ca t io n wit h t h e co rre ct 3 2 - b it , 1 6 - b it ,
a n d 8 - b it va lu e s .

http://www.nuvisionmiami.com/books/asm/workbook/memmap.htm [1/15/2003 4:44:00 PM]


http://www.nuvisionmiami.com/books/asm/workbook/memmap.gif

http://www.nuvisionmiami.com/books/asm/workbook/memmap.gif (1 of 2) [1/15/2003 4:44:01 PM]


http://www.nuvisionmiami.com/books/asm/workbook/memmap.gif

http://www.nuvisionmiami.com/books/asm/workbook/memmap.gif (2 of 2) [1/15/2003 4:44:01 PM]


MEMORY MAP
Write the names of variables next to their
corresponding memory locations

doubleword word byte


0000

0001
0002
0003
0004
0005
0006
0007
0008
0009

000A
000B
000C
000D

000E
000F
0010
0011

0012
0013
0014
0015
0016
0017
MS-DOS Function Calls - 1

MS-DOS Function Calls - 1


Re q u ire d re a d in g : Ch a p t e r 1 3

1 . Writ e a p ro g ra m t h a t in p u t s a s in g le ch a ra ct e r a n d re d is p la ys ( e ch o e s ) it b a ck t o t h e s cre e n . Hin t : Us e INT 2 1 h


fo r t h e ch a ra ct e r in p u t . S o lu t io n p ro g ra m .

2 . Writ e a p ro g ra m t h a t in p u t s a s t rin g o f ch a ra ct e rs ( u s in g a lo o p ) a n d s t o re s e a ch ch a ra ct e r in a n a rra y. Us in g


Co d e Vie w, d is p la y a m e m o ry win d o w co n t a in in g t h e a rra y. S o lu t io n p ro g ra m .

( Co n t e n t s o f m e m o ry win d o w a ft e r t h e lo o p e xe cu t e s : )

000A 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D ABCDEFGHIJKLM
0017 4E 4F 50 51 52 53 54 00 4E 4E 42 30 38 NOPQRST.NNB08

3 . Us in g t h e a rra y cre a t e d in t h e p re vio u s q u e s t io n , re d is p la y t h e a rra y o n t h e s cre e n . S o lu t io n p ro g ra m .

4 . Writ e a p ro g ra m t h a t re a d s a s e rie s o f t e n lo we rca s e le t t e rs fro m in p u t ( wit h o u t d is p la yin g it ) , co n ve rt s e a ch


ch a ra ct e r t o u p p e rca s e , a n d t h e n d is p la ys t h e co n ve rt e d ch a ra ct e r. S o lu t io n p ro g ra m .

5 . Writ e a p ro g ra m t h a t d is p la ys a s t rin g u s in g INT 2 1 h fu n ct io n 9 . S o lu t io n p ro g ra m .

http://www.nuvisionmiami.com/books/asm/workbook/msdos1.htm [1/15/2003 4:44:02 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos1-1.asm

Title MS-DOS Example (DOS1-1.ASM)

;Problem statement:
;Write a program that inputs a single character and redisplays
;(echoes) it back to the screen. Hint: Use INT 21h for the
;character input.

INCLUDE Irvine16.inc

.code
main proc
mov ax,@data
mov ds,ax

mov ah,1 ; input character with echo


int 21h ; AL = character
mov ah,2 ; character output
mov dl,al
int 21h

exit
main endp

end main

http://www.nuvisionmiami.com/books/asm/workbook/dos1-1.asm [1/15/2003 4:44:03 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos1-2.asm

Title MS-DOS Example (DOS1-2.ASM)

; Problem statement:
;Write a program that inputs a string of characters
;(using a loop) and stores each character in an array.
;Display a memory dump in CodeView showing the array.

INCLUDE Irvine16.inc

.data
COUNT = 20
charArray db COUNT dup(0),0

.code
main proc
mov ax,@data
mov ds,ax

mov si,offset charArray


mov cx,COUNT

L1: mov ah,1 ; input character with echo


int 21h ; AL = character
mov [si],al ; save in array
inc si ; next array position
Loop L1 ; repeat loop

exit
main endp
end main

http://www.nuvisionmiami.com/books/asm/workbook/dos1-2.asm [1/15/2003 4:44:03 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos1-3.asm

Title MS-DOS Example (DOS1-3.ASM)

; Problem statement:
;Write a program that inputs a string of characters
;(using a loop) and stores each character in an array.
;Redisplay the array at the end of the program.

INCLUDE Irvine16.inc

.data
COUNT = 20
charArray db COUNT dup(0),0

.code
main proc
mov ax,@data
mov ds,ax

mov si,offset charArray


mov cx,COUNT

L1: mov ah,1 ; input character with echo


int 21h ; AL = character
mov [si],al ; save in array
inc si ; next array position
Loop L1 ; repeat loop

; Redisplay the array on the screen

call Crlf ; start new line


mov si,offset charArray
mov cx,COUNT

L2: mov ah,2 ; character output


mov dl,[si] ; get char from array
int 21h ; display the character
inc si
Loop L2

call Crlf

exit
main endp
end main

http://www.nuvisionmiami.com/books/asm/workbook/dos1-3.asm [1/15/2003 4:44:04 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos1-4.asm

Title MS-DOS Example (DOS1-4.ASM)

;Problem statement:
;Write a program that reads a series of ten lowercase
;letters from input (without displaying it), converts
;each character to uppercase, and then displays the
;converted character.

INCLUDE Irvine16.inc

COUNT = 10

.code
main proc
mov ax,@data
mov ds,ax

mov cx,COUNT ; loop counter

L1: mov ah,7 ; input character, no echo


int 21h ; AL = character
sub al,20h ; convert to upper case
mov ah,2 ; character output function
mov dl,al ; character must be in DL
int 21h ; display the character
Loop L1 ; repeat loop

exit
main endp

end main

http://www.nuvisionmiami.com/books/asm/workbook/dos1-4.asm [1/15/2003 4:44:04 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos1-5.asm

Title MS-DOS Example 1 (DOS1-5.ASM)

;Problem statement:
;Write a program that displays a string using
;INT 21h function 9.

INCLUDE Irvine16.inc

.data
message db "Displaying a string",0dh,0ah,"$"

.code
main proc
mov ax,@data
mov ds,ax

mov ah,9 ; DOS function #9


mov dx,offset message ; offset of the string
int 21h ; display it

exit
main endp

end main

http://www.nuvisionmiami.com/books/asm/workbook/dos1-5.asm [1/15/2003 4:44:04 PM]


MS-DOS Function Calls - 2

MS-DOS Function Calls - 2


Re q u ire d re a d in g : Ch a p t e r 1 3

1 . Writ e a p ro g ra m t h a t in p u t s a s t rin g u s in g DOS fu n ct io n 0 Ah . Lim it t h e in p u t t o t e n ch a ra ct e rs . Re d is p la y t h e


s t rin g b a ckwa rd s . S o lu t io n p ro g ra m .

2 . Writ e a p ro g ra m t h a t in p u t s a s t rin g o f u p t o 8 0 ch a ra ct e rs u s in g DOS fu n ct io n 3 Fh . Aft e r t h e in p u t , d is p la y a


co u n t o n t h e s cre e n o f t h e a ct u a l n u m b e r o f ch a ra ct e rs t yp e d b y t h e u s e r. S o lu t io n p ro g ra m .

3 . Writ e a p ro g ra m t h a t in p u t s t h e m o n t h , d a y, a n d ye a r fro m t h e u s e r. Us e t h e va lu e s t o s e t t h e s ys t e m d a t e wit h


DOS fu n ct io n 2 Bh . Hin t : Us e t h e Re a d in t fu n ct io n fro m t h e b o o k's lin k lib ra ry t o in p u t t h e in t e g e r va lu e s . ( Un d e r
Win d o ws NT/ 2 0 0 , yo u m u s t h a ve a d m in is t ra t o r p rivile g e s t o ru n t h is p ro g ra m . ) S o lu t io n p ro g ra m .

4 . Writ e a p ro g ra m t h a t u s e s DOS fu n ct io n 2 Ah t o g e t a n d d is p la y t h e s ys t e m d a t e . Us e t h e fo llo win g d is p la y fo rm a t :


yyyy- m - d . S o lu t io n p ro g ra m .

http://www.nuvisionmiami.com/books/asm/workbook/msdos2.htm [1/15/2003 4:44:05 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos2-1.asm

title MS-DOS Function Calls - 2 (DOS2-1.ASM)

;Problem statement:
;Write a program that inputs a string using DOS
;function 0Ah. Limit the input to ten characters.
;Redisplay the string backwards

INCLUDE Irvine16.inc

.data
COUNT = 11
keyboardArea label byte
maxkeys db COUNT
charsInput db ?
buffer db COUNT dup(0)

.code
main proc
mov ax,@data
mov ds,ax

mov ah,0Ah ; buffered keyboard input


mov dx,offset keyboardArea
int 21h
call Crlf

; Redisplay the string backwards, using SI


; as an index into the string

mov ah,0
mov al,charsInput ; get character count
mov cx,ax ; put in loop counter
mov si,ax ; point past end of string
dec si ; back up one position

L1: mov dl,buffer[si] ; get char from buffer


mov ah,2 ; MS-DOS char output function
int 21h
dec si ; back up in buffer
Loop L1 ; loop through the string

call Crlf

exit
main endp

end main

http://www.nuvisionmiami.com/books/asm/workbook/dos2-1.asm [1/15/2003 4:44:06 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos2-2.asm

title MS-DOS Function Calls - 2 (DOS2-2.ASM)

;Problem statement:
;Write a program that inputs a string of up to 80
;characters using DOS function 3Fh. After the input,
;display a count on the screen of the actual number
;of characters typed by the user.

INCLUDE Irvine16.inc

.data
COUNT = 80

; create the input buffer, and allow


; for two extra characters (CR/LF)

buffer db (COUNT+2) dup(0)

.code
main proc
mov ax,@data
mov ds,ax

mov ah,3Fh ; input from file or device


mov bx,0 ; keyboard device handle
mov cx,COUNT ; max input count
mov dx,offset buffer
int 21h ; call DOS to read the input

; Display the character count in AX that was


; returned by INT 21h function 3Fh
; (minus 2 for the CR/LF characters)

sub ax,2
call Writedec ; display AX
call Crlf

exit
main endp

end main

http://www.nuvisionmiami.com/books/asm/workbook/dos2-2.asm [1/15/2003 4:44:06 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos2-3.asm

title MS-DOS Function Calls - 2 (DOS2-3.ASM)

;Problem statement:
;Write a program that inputs the month, day, and
;year from the user. Use the values to set the system
;date with DOS function 2Bh.

INCLUDE Irvine16.inc

.data
monthPrompt db "Enter the month: ",0
dayPrompt db "Enter the day: ",0
yearPrompt db "Enter the year: ",0
blankLine db 30 dup(" "),0dh,0
month db ?
day db ?
year dw ?

.code
main proc
mov ax,@data
mov ds,ax

mov dx,offset monthPrompt


call Writestring
call Readint
mov month,al
mov dx,offset blankLine
call Writestring

mov dx,offset dayPrompt


call Writestring
call Readint
mov day,al
mov dx,offset blankLine
call Writestring

mov dx,offset yearPrompt


call Writestring
call Readint
mov year,ax

mov ah,2Bh ; MS-DOS Set Date function


mov cx,year
mov dh,month
mov dl,day
int 21h ; set the date now

;(AL = FFh if the date could not be set)

exit
main endp

end main

http://www.nuvisionmiami.com/books/asm/workbook/dos2-3.asm [1/15/2003 4:44:06 PM]


http://www.nuvisionmiami.com/books/asm/workbook/dos2-4.asm

title MS-DOS Function Calls - 2 (DOS2-4.ASM)

;Problem statement:
;Write a program that uses DOS function 2Ah to
;get and display the system date. Use the
;following display format: yyyy-m-d.

INCLUDE Irvine16.inc

.data
month db ?
day db ?
year dw ?

.code
main proc
mov ax,@data
mov ds,ax

mov ah,2Ah ; MS-DOS Get Date function


int 21h ; get the date now
mov year,cx
mov month,dh
mov day,dl

mov ax,year
call Writedec

mov ah,2 ; display a hyphen


mov dl,"-"
int 21h

mov al,month ; display the month


mov ah,0
call Writedec

mov ah,2 ; display a hyphen


mov dl,"-"
int 21h

mov al,day ; display the day


mov ah,0
call Writedec
call Crlf

exit
main endp

end main

http://www.nuvisionmiami.com/books/asm/workbook/dos2-4.asm [1/15/2003 4:44:07 PM]


Error-Correcting Codes

Error Correcting Codes


Ev e n a n d Od d P a rit y
If a b in a ry n u m b e r co n t a in s a n e ve n n u m b e r o f 1 b it s , we s a y t h a t it h a s e v e n p a rit y . If t h e n u m b e r co n t a in s a n o d d
n u m b e r o f 1 b it s , it h a s o d d p a rit y .

Wh e n d a t a m u s t b e t ra n s m it t e d fro m o n e d e vice t o a n o t h e r, t h e re is a lwa ys t h e p o s s ib ilit y t h a t a n e rro r m ig h t o ccu r.


De t e ct io n o f a s in g le in co rre ct b it in a d a t a wo rd ca n b e d e t e ct e d s im p ly b y a d d in g a n a d d it io n a l p a rit y b it t o t h e e n d
o f t h e wo rd . If b o t h t h e s e n d e r a n d re ce ive r a g re e t o u s e e ve n p a rit y, fo r e xa m p le , t h e s e n d e r ca n s e t t h e p a rit y b it
t o e it h e r 1 o r ze ro s o a s t o m a ke t h e t o t a l n u m b e r o f 1 b it s in t h e wo rd a n e ve n n u m b e r:

8 - b it d a t a va lu e : 1 0 1 1 0 1 0 1
a d d e d p a rit y b it : 1
t ra n s m it t e d d a t a : 1 0 1 1 0 1 0 1 1

Or, if t h e d a t a va lu e a lre a d y h a d a n e ve n n u m b e r o f 1 b it s , t h e p a rit y b it wo u ld b e s e t t o 0 :

8 - b it d a t a va lu e : 1 0 1 1 0 1 0 0
a d d e d p a rit y b it : 0
t ra n s m it t e d d a t a : 1 0 1 1 0 1 0 0 0

Th e re ce ive r o f a t ra n s m is s io n a ls o co u n t s t h e 1 b it s in t h e re ce ive d va lu e , a n d if t h e co u n t is n o t e ve n , a n e rro r


co n d it io n is s ig n a lle d a n d t h e s e n d e r is u s u a lly in s t ru ct e d t o re - s e n d t h e d a t a . Fo r s m a ll, n o n - crit ica l d a t a
t ra n s m is s io n s , t h is m e t h o d is a re a s o n a b le t ra d e o ff b e t we e n re lia b ilit y a n d e fficie n cy. Bu t it p re s e n t s p ro b le m s in
ca s e s wh e re h ig h ly re lia b le d a t a m u s t b e t ra n s m it t e d .

Th e p rim a ry p ro b le m wit h u s in g a s in g le p a rit y b it is t h a t it ca n n o t d e t e ct t h e p re s e n ce o f m o re t h a n o n e


t ra n s m is s io n e rro r. If t wo b it s a re in co rre ct , t h e p a rit y ca n s t ill b e e ve n a n d n o e rro r ca n b e d e t e ct e d . In t h e n e xt
s e ct io n we will lo o k a t a n e n co d in g m e t h o d t h a t ca n b o t h d e t e ct m u lt ip le e rro rs a n d ca n co rre ct s in g le e rro rs .

Ha m m in g Co d e
In 1 9 5 0 , Rich a rd Ha m m in g d e ve lo p e d a n in n o va t ive wa y o f a d d in g b it s t o a n u m b e r in s u ch a wa y t h a t t ra n s m is s io n
e rro rs in vo lvin g n o m o re t h a n a s in g le b it co u ld b e d e t e ct e d a n d co rre ct e d .

Th e n u m b e r o f p a rit y b it s d e p e n d s o n t h e n u m b e r o f d a t a b it s :

Data Bits : 4 8 16 32 64 128

Parity Bits: 3 4 5 6 7 8

http://www.nuvisionmiami.com/books/asm/workbook/error_correcting.htm (1 of 5) [1/15/2003 4:44:08 PM]


Error-Correcting Codes

Codeword : 7 12 21 38 71 136

We ca n s a y t h a t fo r N d a t a b it s , ( lo g 2 N) + 1 p a rit y b it s a re re q u ire d . In o t h e r wo rd s , fo r a d a t a o f s ize 2 n b it s , n + 1


p a rit y b it s a re e m b e d d e d t o fo rm t h e co d e wo rd . It 's in t e re s t in g t o n o t e t h a t d o u b lin g t h e n u m b e r o f d a t a b it s re s u lt s
in t h e a d d it io n o f o n ly 1 m o re d a t a b it . Of co u rs e , t h e lo n g e r t h e co d e wo rd , t h e g re a t e r t h e ch a n ce t h a t m o re t h a n
e rro r m ig h t o ccu r.

P la c in g t h e P a rit y Bit s
( Fro m t h is p o in t o n wa rd we will n u m b e r t h e b it s fro m le ft t o rig h t , b e g in n in g wit h 1 . In o t h e r wo rd s , b it 1 is t h e
m o s t s ig n ifica n t b it . )

Th e p a rit y b it p o s it io n s a re p o we rs o f 2 : { 1 , 2 , 4 , 8 , 1 6 , 3 2 . . . } . All re m a in in g p o s it io n s h o ld d a t a b it s . He re is a t a b le
re p re s e n t in g a 2 1 - b it co d e wo rd :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
P P P P P

Th e 1 6 - b it d a t a va lu e 1 0 0 0 1 1 1 1 0 0 1 1 0 1 0 1 wo u ld b e s t o re d a s fo llo ws :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
P P 1 P 0 0 0 P 1 1 1 1 0 0 1 P 1 0 1 0 1

Ca lc u la t in g P a rit y
Fo r a n y d a t a b it lo ca t e d in p o s it io n N in t h e co d e wo rd , t h e b it is ch e cke d b y p a rit y b it s in p o s it io n s P 1 , P 2 , P 3 , . . . , P k if
N is e q u a l t o t h e s u m o f P 1 , P 2 , P 3 , . . . , P k . Fo r e xa m p le , b it 1 1 is ch e cke d b y p a rit y b it s 1 , 2 a n d 8 ( 1 1 = 1 + 2 + 8 ) .
He re is a t a b le co ve rin g co d e wo rd s u p t o 2 1 b it s lo n g :

D a t a Bit . . . is c h e c k e d b y p a rit y b it s
3 1, 2
5 1, 4
6 2, 4
7 1,2,4
9 1,8
10 2,8
11 1,2,8
12 4,8
13 1,4,8
14 2,4,8
15 1,2,4,8
17 1,16
18 2,16
19 1,2,16
20 4,16

http://www.nuvisionmiami.com/books/asm/workbook/error_correcting.htm (2 of 5) [1/15/2003 4:44:08 PM]


Error-Correcting Codes

21 1,4,16
(table 4)

Tu rn in g t h is d a t a a ro u n d in a m o re u s e fu l wa y, t h e fo llo win g t a b le s h o ws e xa ct ly wh ich d a t a b it s a re ch e cke d b y


e a ch p a rit y b it in a 2 1 - b it co d e wo rd :

P a rit y Bit Ch e c k s t h e fo llo w in g D a t a Bit s Hin t *


1 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 u s e 1 , s kip 1 , u s e 1 , s kip 1 , . . .
2 2, 3, 6, 7, 10, 11, 14, 15, 18, 19 u s e 2 , s kip 2 , u s e 2 , s kip 2 , . . .
4 4, 5, 6, 7, 12, 13, 14, 15, 20, 21 u s e 4 , s kip 4 , u s e 4 , ...
8 8, 9, 10, 11, 12, 13, 14, 15 u s e 8 , s kip 8 , u s e 8 , ...
16 16, 17, 18, 19, 20, 21 u s e 1 6 , s kip 1 6 , . . .

(table 5)

It is u s e fu l t o vie w e a ch ro w in t h is t a b le a s a b it g ro u p . As we will s e e la t e r, e rro r co rre ct in g u s in g t h e Ha m m in g


e n co d in g m e t h o d is b a s e d o n t h e in t e rs e ct io n s b e t we e n t h e s e g ro u p s , o r s e t s , o f b it s .

* S o m e o f t h e h in t s ( 3 rd co lu m n ) o n ly m a ke s e n s e fo r la rg e r co d e wo rd s .

En c o d in g a D a t a Va lu e
No w it 's t im e t o p u t a ll o f t h is in fo rm a t io n t o g e t h e r a n d cre a t e a co d e wo rd . We will u s e e ve n p a rit y fo r e a ch b it
g ro u p , wh ich is a n a rb it ra ry d e cis io n . We m ig h t ju s t a s e a s ily h a ve d e cid e d t o u s e o d d p a rit y. Fo r t h e firs t e xa m p le ,
le t 's u s e t h e 8 - b it d a t a va lu e 1 1 0 0 1 1 1 1 , wh ich will p ro d u ce a 1 2 - b it co d e wo rd . Le t 's s t a rt b y fillin g in t h e d a t a
b it s :

1 2 3 4 5 6 7 8 9 10 11 12
P P 1 P 1 0 0 P 1 1 1 1

Ne xt , we b e g in ca lcu la t in g a n d in s e rt in g e a ch o f t h e p a rit y b it s .

P 1 : To ca lcu la t e t h e p a rit y b it in p o s it io n 1 , we s u m t h e b it s in p o s it io n s 3 , 5 , 7 , 9 , a n d 1 1 : ( 1 + 1 + 0 + 1 + 1 = 4 ) . Th is
s u m is e ve n ( in d ica t in g e v e n p a rit y ) , s o p a rit y b it 1 s h o u ld b e a s s ig n e d a va lu e o f 0 . By d o in g t h is , we a llo w t h e
p a rit y t o re m a in e ve n :

1 2 3 4 5 6 7 8 9 10 11 12
0 P 1 P 1 0 0 P 1 1 1 1

P 2 : To g e n e ra t e t h e p a rit y b it in p o s it io n 2 , we s u m t h e b it s in p o s it io n s 3 , 6 , 7 , 1 0 , a n d 1 1 : ( 1 + 0 + 0 + 1 + 1 = 3 ) .
Th e s u m is o d d , s o we a s s ig n a va lu e o f 1 t o p a rit y b it 2 . Th is p ro d u ce s e ve n p a rit y fo r t h e co m b in e d g ro u p o f b it s 2 ,
3, 6, 7, 10, and 11:

1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 P 1 0 0 P 1 1 1 1

P 4 : To g e n e ra t e t h e p a rit y b it in p o s it io n 4 , we s u m t h e b it s in p o s it io n s 5 , 6 , 7 , a n d 1 2 : ( 1 + 0 + 0 + 1 = 2 ) . Th is
re s u lt s in e v e n p a rit y, s o we s e t p a rit y b it 4 t o ze ro , le a vin g t h e p a rit y e ve n :

1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 0 1 0 0 P 1 1 1 1

http://www.nuvisionmiami.com/books/asm/workbook/error_correcting.htm (3 of 5) [1/15/2003 4:44:08 PM]


Error-Correcting Codes

P 8 : To g e n e ra t e t h e p a rit y b it in p o s it io n 8 , we s u m t h e b it s in p o s it io n s 9 , 1 0 , 1 1 a n d 1 2 : ( 1 + 1 + 1 + 1 = 4 ) . Th is
re s u lt s in e v e n p a rit y, s o we s e t p a rit y b it 8 t o ze ro , le a vin g t h e p a rit y e ve n :

1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 0 1 0 0 0 1 1 1 1

All p a rit y b it s h a ve b e e n cre a t e d , a n d t h e re s u lt in g co d e wo rd is : 0 1 1 0 1 0 0 0 1 1 1 1 .

D e t e c t in g a S in g le Erro r
Wh e n a co d e wo rd is re ce ive d , t h e re ce ive r m u s t ve rify t h e co rre ct n e s s o f t h e d a t a . Th is is a cco m p lis h e d b y co u n t in g
t h e 1 b it s in e a ch b it g ro u p ( m e n t io n e d e a rlie r) a n d ve rifyin g t h a t e a ch h a s e ve n p a rit y. Re ca ll t h a t we a rb it ra rily
d e cid e d t o u s e e ve n p a rit y wh e n cre a t in g co d e wo rd s . He re a re t h e b it g ro u p s fo r a 1 2 - b it co d e va lu e :

P a rit y Bit Bit Gro u p


1 1, 3, 5, 7, 9, 11
2 2, 3, 6, 7, 10, 11
4 4, 5, 6, 7, 12
8 8, 9, 10, 11, 12

If o n e o f t h e s e g ro u p s p ro d u ce s a n o d d n u m b e r o f b it s , t h e re ce ive r kn o ws t h a t a t ra n s m is s io n e rro r o ccu rre d . As


lo n g a s o n ly a s in g le b it wa s a lt e re d , it ca n b e co rre ct e d . Th e m e t h o d ca n b e b e s t s h o wn u s in g co n cre t e e xa m p le s .

Ex a m p le 1 : S u p p o s e t h a t t h e b it in p o s it io n 4 wa s re ve rs e d , p ro d u cin g 0 1 1 1 1 0 0 0 1 1 1 1 . Th e re ce ive r wo u ld d e t e ct
a n o d d p a rit y in t h e b it g ro u p a s s o cia t e d wit h p a rit y b it 4 . Aft e r e lim in a t in g a ll b it s fro m t h is g ro u p t h a t a ls o a p p e a r
in o t h e r g ro u p s , t h e o n ly re m a in in g b it is b it 4 . Th e re ce ive r wo u ld t o g g le t h is b it , t h u s co rre ct in g t h e t ra n s m is s io n
e rro r.

Ex a m p le 2 : S u p p o s e t h a t b it 7 wa s re ve rs e d , p ro d u cin g 0 1 1 0 1 0 1 0 1 1 1 1 . Th e b it g ro u p s b a s e d o n p a rit y b it s 1 , 2 ,
a n d 4 wo u ld h a ve o d d p a rit y. Th e o n ly b it t h a t is s h a re d b y a ll t h re e g ro u p s ( t h e in t e rs e ct io n o f t h e t h re e s e t s o f
b it s ) is b it 7 , s o a g a in t h e e rro r b it is id e n t ifie d :

P a rit y Bit Bit Gro u p


1 1, 3, 5, 7, 9, 11
2 2, 3, 6, 7, 10, 11
4 4, 5, 6, 7, 12
8 8, 9, 10, 11, 12

Ex a m p le 3 : S u p p o s e t h a t b it 6 wa s re ve rs e d , p ro d u cin g 0 1 1 0 1 1 0 0 1 1 1 1 . Th e g ro u p s b a s e d o n p a rit y b it s 2 a n d 4
wo u ld h a ve o d d p a rit y. No t ice t h a t t wo b it s a re s h a re d b y t h e s e t wo g ro u p s ( t h e ir in t e rs e ct io n ) : 6 a n d 7 :

P a rit y Bit Bit Gro u p


1 1, 3, 5, 7, 9, 11
2 2, 3, 6, 7, 10, 11
4 4, 5, 6, 7, 12
8 8, 9, 10, 11, 12

http://www.nuvisionmiami.com/books/asm/workbook/error_correcting.htm (4 of 5) [1/15/2003 4:44:08 PM]


Error-Correcting Codes

Bu t t h e n , b u t 7 o ccu rs in g ro u p 1 , wh ich h a s e ve n p a rit y. Th is le a ve s b it 6 a s t h e o n ly ch o ice a s t h e in co rre ct b it .

Mu lt ip le Erro rs
If t wo e rro rs we re t o o ccu r, we co u ld d e t e ct t h e p re s e n ce o f a n e rro r, b u t it wo u ld n o t b e p o s s ib le t o co rre ct t h e
e rro r. Co n s id e r, fo r e xa m p le , t h a t b o t h b it s 5 a n d 7 we re in co rre ct . Th e b it g ro u p s b a s e d o n p a rit y b it 2 wo u ld h a ve
o d d p a rit y. Gro u p s 1 a n d 4 , o n t h e o t h e r h a n d , wo u ld h a ve e ve n p a rit y b e ca u s e b it s 5 a n d 7 wo u ld co u n t e ra ct e a ch
o t h e r:

P a rit y Bit Bit Gro u p


1 1, 3, 5, 7
2 2, 3, 6, 7
4 4, 5, 6, 7

Th is wo u ld in co rre ct ly le a d u s t o t h e co n clu s io n t h a t b it 2 is t h e cu lp rit , a s it is t h e o n ly b it t h a t d o e s n o t o ccu r in


g ro u p s 1 a n d 4 . Bu t t o g g lin g b it 2 wo u ld n o t t o fix t h e e rro r- - it wo u ld s im p ly m a ke it wo rs e .

Fo r a n e xce lle n t in t ro d u ct o ry d is cu s s io n o f e rro r- co rre ct in g co d e s , s e e Ta n e n b a u m , An d re w. S t ru c t u re d Co m p u t e r


Org a n iz a t io n , Fo u rt h Ed it io n ( 1 9 9 9 ) , p p . 6 1 - 6 4 .

If yo u wo u ld like t o le a rn h o w t o co n s t ru ct yo u r o wn e rro r- co rre ct in g co d e s , h e re is a g o o d e xp la n a t io n o f t h e


m a t h e m a t ics : La u fe r, He n ry B. D is c re t e Ma t h e m a t ic s a n d Ap p lie d Mo d e rn Alg e b ra . Ch a p t e r 1 : Gro u p Co d e s .
Prin d le , We b e r & S cm id t , 1 9 8 4 .

http://www.nuvisionmiami.com/books/asm/workbook/error_correcting.htm (5 of 5) [1/15/2003 4:44:08 PM]


Boolean and Comparison Instructions

Boolean and Comparison Instructions


Click h e re t o vie w t h e An s we rs

AN D a n d OR I n s t ru c t io n s
1 . Writ e in s t ru ct io n s t h a t ju m p t o a la b e l n a m e d Ta rg e t if b it s 0 , 1 , a n d 2 in t h e AL re g is t e r a re a ll s e t ( t h e re m a in in g
b it s a re u n im p o rt a n t ) .

2 . Writ e in s t ru ct io n s t h a t will ju m p t o a la b e l n a m e d Ta rg e t if e it h e r b it 0 , 1 , o r 2 is s e t in t h e AL re g is t e r ( t h e
re m a in in g b it s a re u n im p o rt a n t ) .

3 . Cle a r b it s 4 - 6 in t h e BL re g is t e r wit h o u t a ffe ct in g a n y o t h e r b it s .

4 . S e t b it s 3 - 4 in t h e CL re g is t e r wit h o u t a ffe ct in g a n y o t h e r b it s .

http://www.nuvisionmiami.com/books/asm/workbook/boolean.htm [1/15/2003 4:44:09 PM]


Answers: Boolean and Comparison Instructions

An s w e rs : Bo o le a n a n d Co m p a ris o n I n s t ru c t io n s

AN D a n d OR I n s t ru c t io n s
1 . Me t h o d o n e : Cle a r a ll n o n e s s e n t ia l b it s a n d co m p a re t h e re m a in in g o n e s wit h t h e m a s k va lu e :

and AL,00000111b
cmp AL,00000111b
je Target

Me t h o d t wo : Us e t h e b o o le a n ru le t h a t a ^ b ^ c = = ~ ( ~ a v ~ b v ~ c)

not AL
test AL,00000111b
jz Target

2.
test AL,00000111b
jnz Target

3.

and BL,10001111b

4.

or CL,00011000b

http://www.nuvisionmiami.com/books/asm/workbook/boolean_ans.htm [1/15/2003 4:44:10 PM]


Decoding 12-Bit FAT

Decoding a 12-bit File Allocation Table


In t h is s e ct io n we p re s e n t a s im p le p ro g ra m t h a t lo a d s t h e file a llo ca t io n t a b le a n d ro o t d ire ct o ry fro m a d is ke t t e ( in
d rive A) , a n d d is p la ys t h e lis t o f clu s t e rs o wn e d b y e a ch file . Le t 's lo o k a t p a rt o f a s a m p le 1 2 - b it FAT in ra w fo rm
( s h o wn b y De b u g ) s o we ca n d e co d e it s s t ru ct u re :

F0 FF FF FF 4F 00 05 60-00 07 80 00 09 A0 00 0B
C0 00 0D E0 00 0F 00 01-11 20 01 13 40 01 15 60

A d e co d e d fo rm o f e n t rie s 2 t h ro u g h 9 is s h o wn h e re :
En t ry: 2 3 4 5 6 7 8 9 . . .
Va lu e : < FFF> < 0 0 4 > < 0 0 5 > < 0 0 6 > < 0 0 7 > < 0 0 8 > < 0 0 9 > < 0 0 A> . . .

Yo u ca n ca n t ra ck d o wn a ll clu s t e rs a llo ca t e d t o a p a rt icu la r file b y fo llo win g wh a t is ca lle d a clu s t e r ch a in . Le t 's


fo llo w t h e clu s t e r ch a in s t a rt in g wit h clu s t e r 3 . He re is h o w we fin d it s m a t ch in g e n t ry in t h e FAT, u s in g t h re e s t e p s :

1 . Divid e t h e clu s t e r n u m b e r b y 2 , re s u lt in g in a n in t e g e r q u o t ie n t . Ad d t h e s a m e clu s t e r n u m b e r t o t h is


q u o t ie n t , p ro d u cin g t h e o ffs e t o f t h e clu s t e r's e n t ry in t h e FAT. Us in g clu s t e r 3 a s a s a m p le , t h is re s u lt s in
In t ( 3 / 2 ) + 3 = 4 , s o we lo o k a t o ffs e t 4 in t h e FAT.
2 . Th e 1 6 - b it wo rd a t o ffs e t 4 co n t a in s 0 0 4 Fh ( 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 ) . We n e e d t o e xa m in e t h is e n t ry t o
d e t e rm in e t h e n e xt clu s t e r n u m b e r a llo ca t e d t o t h e file .
3 . If t h e cu rre n t clu s t e r n u m b e r is e ve n , ke e p t h e lo we s t 1 2 b it s o f t h e 1 6 - b it wo rd . If t h e cu rre n t clu s t e r
n u m b e r is o d d , ke e p t h e h ig h e s t 1 2 b it s o f t h e 1 6 - b it wo rd . Fo r e xa m p le , o u r clu s t e r n u m b e r ( 3 ) is o d d , s o
we ke e p t h e h ig h e s t 1 2 b it s ( 0 0 0 0 0 0 0 0 0 1 0 0 ) , a n d t h is in d ica t e s t h a t clu s t e r 4 is t h e n e xt clu s t e r.

We re t u rn t o s t e p 1 a n d ca lcu la t e t h e o ffs e t o f clu s t e r 4 in t h e FAT t a b le : Th e cu rre n t clu s t e r n u m b e r is 4 , s o we


ca lcu la t e In t ( 4 / 2 ) + 4 = 6 . Th e wo rd a t o ffs e t 6 is 6 0 0 5 h ( 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 1 ) . Th e va lu e 6 is e ve n , s o we t a ke
t h e lo we s t 1 2 b it s o f 6 0 0 5 h , p ro d u cin g a n e w clu s t e r n u m b e r o f 5 . Th e re fo re , FAT e n t ry 4 co n t a in s t h e n u m b e r 5 .

Fo rt u n a t e ly, a 1 6 - b it FAT is e a s ie r t o d e co d e , b e ca u s e e n t rie s d o n o t cro s s b yt e b o u n d a rie s .


In a 1 6 - b it FAT, clu s t e r n is re p re s e n t e d b y t h e e n t ry a t o ffs e t n * 2 in t h e t a b le .

Fin d in g t h e S t a rt in g S e c t o r
Give n a clu s t e r n u m b e r, we n e e d t o kn o w h o w t o ca lcu la t e it s s t a rt in g s e ct o r n u m b e r:

1 . S u b t ra ct 2 fro m t h e clu s t e r n u m b e r a n d m u lt ip ly t h e re s u lt b y t h e d is k's s e ct o rs p e r clu s t e r. A 1 . 4 4 MB d is k


h a s o n e s e ct o r p e r clu s t e r, s o we m u lt ip ly b y 1 .
2 . Ad d t h e s t a rt in g s e ct o r n u m b e r o f t h e d a t a a re a . On a 1 . 4 4 MB d is k, t h is is s e ct o r 3 3 . Fo r e xa m p le , clu s t e r
n u m b e r 3 is lo ca t e d a t s e ct o r 3 4 : ( ( 3 - 2 ) * 1 ) + 3 3 = 3 4

Clu s t e r D is p la y P ro g ra m
In t h is s e ct io n , we will d e m o n s t ra t e a p ro g ra m t h a t re a d s a 1 . 4 4 MB d is ke t t e in d rive A, lo a d s it s file a llo ca t io n t a b le
a n d ro o t d ire ct o ry in t o a b u ffe r, a n d d is p la ys e a ch file n a m e a lo n g wit h a lis t o f a ll clu s t e rs a llo ca t e d t o t h e file . Th e
fo llo win g is a s a m p le o f t h e p ro g ra m ’s o u t p u t :

http://www.nuvisionmiami.com/books/asm/workbook/DecodingFAT12.htm (1 of 6) [1/15/2003 4:44:11 PM]


Decoding 12-Bit FAT

Th e m a in p ro ce d u re d is p la ys a g re e t in g , lo a d s t h e d ire ct o ry a n d FAT in t o m e m o ry, a n d lo o p s t h ro u g h e a ch d ire ct o ry


e n t ry. Th e m o s t im p o rt a n t t a s k h e re is t o ch e ck t h e firs t ch a ra ct e r o f e a ch d ire ct o ry e n t ry t o s e e if it re fe rs t o a
file n a m e . If it d o e s , we ch e ck t h e file 's a t t rib u t e b yt e a t o ffs e t 0 Bh t o m a ke s u re t h e e n t ry is n o t a vo lu m e la b e l o r
d ire ct o ry n a m e . We s cre e n o u t d ire ct o ry e n t rie s wit h a t t rib u t e s o f 0 0 h , E5 h , 2 Eh , a n d 1 8 h .

Re g a rd in g t h e a t t rib u t e b yt e : Bit 3 is s e t if t h e e n t ry is a vo lu m e n a m e , a n d b it 4 is s e t if it is a d ire ct o ry n a m e . Th e


TES T in s t ru ct io n u s e d h e re s e t s t h e Ze ro fla g o n ly if b o t h b it s a re cle a r.

Lo a d FATa n d Dir lo a d s t h e d is k d ire ct o ry in t o d irb u f, a n d it lo a d s t h e FAT in t o fa t t a b le . Dis p la yClu s t e rs co n t a in s a lo o p


t h a t d is p la ys a ll clu s t e r n u m b e rs a llo ca t e d t o a s in g le file . Th e d is k d ire ct o ry h a s a lre a d y b e e n re a d in t o d irb u f, a n d
we a s s u m e t h a t S I p o in t s t o t h e cu rre n t d ire ct o ry e n t ry.

Th e Ne xt _ FAT_ En t ry p ro ce d u re u s e s t h e cu rre n t clu s t e r n u m b e r ( p a s s e d in AX) t o ca lcu la t e t h e n e xt clu s t e r n u m b e r,


wh ich it re t u rn s in AX. Th e S HR in s t ru ct io n in t h is p ro ce d u re ch e cks t o s e e if t h e clu s t e r n u m b e r is e ve n b y s h ift in g
it s lo we s t b it in t o t h e Ca rry fla g . If it is , we re t a in t h e lo w 1 2 b it s o f DX; o t h e rwis e , we ke e p t h e h ig h 1 2 b it s . Th e
n e w clu s t e r n u m b e r is re t u rn e d in AX.

Here is the complete program listing:

TITLE Cluster Display Program (Cluster.asm)

; This program reads the directory of drive A, decodes


; the file allocation table, and displays the list of
; clusters allocated to each file.

INCLUDE Irvine16.inc

; Attributes specific to 1.44MB diskettes:

http://www.nuvisionmiami.com/books/asm/workbook/DecodingFAT12.htm (2 of 6) [1/15/2003 4:44:11 PM]


Decoding 12-Bit FAT

FATSectors = 9 ; num sectors, first copy of FAT


DIRSectors = 14 ; num sectors, root directory
DIR_START = 19 ; starting directory sector num

SECTOR_SIZE = 512
DRIVE_A = 0
FAT_START = 1 ; starting sector of FAT
EOLN equ <0dh,0ah>

Directory STRUCT
fileName BYTE 8 dup(?)
extension BYTE 3 dup(?)
attribute BYTE ?
reserved BYTE 10 dup(?)
time WORD ?
date WORD ?
startingCluster WORD ?
fileSize DWORD ?
Directory ENDS
ENTRIES_PER_SECTOR = SECTOR_SIZE / (size Directory)

.data
heading LABEL byte
BYTE 'Cluster Display Program (CLUSTER.EXE)'
BYTE EOLN,EOLN,'The following clusters are allocated '
BYTE 'to each file:',EOLN,EOLN,0

fattable WORD ((FATSectors * SECTOR_SIZE) / 2) DUP(?)


dirbuf Directory (DIRSectors * ENTRIES_PER_SECTOR) DUP(<>)
driveNumber BYTE ?

.code
main PROC
call Initialize
mov ax,OFFSET dirbuf
mov ax,OFFSET driveNumber
call LoadFATandDir
jc A3 ; quit if we failed
mov si,OFFSET dirbuf ; index into the directory

A1: cmp (Directory PTR [si]).filename,0 ; entry never used?


je A3 ; yes: must be the end
cmp (Directory PTR [si]).filename,0E5h ; entry deleted?
je A2 ; yes: skip to next entry
cmp (Directory PTR [si]).filename,2Eh ; parent directory?
je A2 ; yes: skip to next entry
cmp (Directory PTR [si]).attribute,0Fh ; extended filename?
je A2

http://www.nuvisionmiami.com/books/asm/workbook/DecodingFAT12.htm (3 of 6) [1/15/2003 4:44:11 PM]


Decoding 12-Bit FAT

test (Directory PTR [si]).attribute,18h ; vol or directory name?


jnz A2 ; yes: skip to next entry
call displayClusters ; must be a valid entry

A2: add si,32 ; point to next entry


jmp A1
A3: exit
main ENDP

;----------------------------------------------------------
LoadFATandDir PROC
; Load FAT and root directory sectors.
; Receives: nothing
; Returns: nothing
;----------------------------------------------------------
pusha
; Load the FAT
mov al,DRIVE_A
mov cx,FATsectors
mov dx,FAT_START
mov bx,OFFSET fattable
int 25h ; read sectors
add sp,2 ; pop old flags off stack
; Load the Directory
mov cx,DIRsectors
mov dx,DIR_START
mov bx,OFFSET dirbuf
int 25h
add sp,2
popa
ret
LoadFATandDir ENDP

;----------------------------------------------------------
DisplayClusters PROC
; Display all clusters allocated to a single file.
; Receives: SI contains the offset of the directory entry.
;----------------------------------------------------------
push ax
call displayFilename ; display the filename
mov ax,[si+1Ah] ; get first cluster
C1: cmp ax,0FFFh ; last cluster?
je C2 ; yes: quit
mov bx,10 ; choose decimal radix
call WriteDec ; display the number
call writeSpace ; display a space
call next_FAT_entry ; returns cluster # in AX
jmp C1 ; find next cluster
C2: call Crlf

http://www.nuvisionmiami.com/books/asm/workbook/DecodingFAT12.htm (4 of 6) [1/15/2003 4:44:11 PM]


Decoding 12-Bit FAT

pop ax
ret
DisplayClusters ENDP

;----------------------------------------------------------
WriteSpace PROC
; Write a single space to standard output.
;----------------------------------------------------------
push ax
mov ah,2 ; function: display character
mov dl,20h ; 20h = space
int 21h
pop ax
ret
WriteSpace ENDP

;----------------------------------------------------------
Next_FAT_entry PROC
; Find the next cluster in the FAT.
; Receives: AX = current cluster number
; Returns: AX = new cluster number
;----------------------------------------------------------
push bx ; save regs
push cx
mov bx,ax ; copy the number
shr bx,1 ; divide by 2
add bx,ax ; new cluster OFFSET
mov dx,fattable[bx] ; DX = new cluster value
shr ax,1 ; old cluster even?
jc E1 ; no: keep high 12 bits
and dx,0FFFh ; yes: keep low 12 bits
jmp E2
E1: shr dx,4 ; shift 4 bits to the right
E2: mov ax,dx ; return new cluster number
pop cx ; restore regs
pop bx
ret
Next_FAT_entry ENDP

;----------------------------------------------------------
DisplayFilename PROC
; Display the file name.
;----------------------------------------------------------
mov byte ptr [si+11],0 ; SI points to filename
mov dx,si
call Writestring
mov ah,2 ; display a space
mov dl,20h
int 21h

http://www.nuvisionmiami.com/books/asm/workbook/DecodingFAT12.htm (5 of 6) [1/15/2003 4:44:11 PM]


Decoding 12-Bit FAT

ret
DisplayFilename ENDP

;----------------------------------------------------------
Initialize PROC
; Set upt DS, clear screen, display a heading.
;----------------------------------------------------------
mov ax,@data
mov ds,ax
call ClrScr
mov dx,OFFSET heading ; display program heading
call Writestring
ret
Initialize ENDP
END main

http://www.nuvisionmiami.com/books/asm/workbook/DecodingFAT12.htm (6 of 6) [1/15/2003 4:44:11 PM]

You might also like