Sie sind auf Seite 1von 12

"Don't study, don't know - Studying you will know!

"

NGUYEN TRUNG HOA
Chng 1. H thng s m va khai nim v ma Trang 1
Chuong 1


H THNG S DM VA KHAI NIM V MA
1.1. H THNG S DM
1.1.1. H dm
1.1.1.1. Khai nim
H m la tp hp cac phng phap goi va biu din cac con s
bng cac k hiu co gia tr s lng xac nh goi la ch s.
1.1.1.2. Phn loai
Chia lam hai loai:
a. H m theo v tr:
La h m ma trong o gia tr s lng cua ch s con phu thuc
vao v tr cua no ng trong con s.
V du: 1991 (H thp phn)
1111 (H nh phn)
b. H m khng theo v tr:
La h m ma trong o gia tr s lng cua ch s khng phu thuc
vao v tr cua no tng ng (ng) trong con s.
V du: H m La ma I, II, III . . . . .
1.1.2. Co s cua h dm
Mt s A bt ky co th biu din bng day sau:
A a
m-1
a
m-2
. . . . .a
0
a
-1
. . . . . . . . .a
-n

Trong o: a
i
( 1 m n i = ) la cac ch s; i: cac hang s, i nho:
hang tre, i ln: hang gia.
Gia tr s lng cua cac ch s a
i
se nhn mt gia tr nao o cua con
s N sao cho thoa man bt ng thc sau:
1 N a 0
i

Va a
i
nguyn, th N c goi la c s cua h m.
Bai giang Ky Thut S Trang 2
V du: N =10 a
i
= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
N =8 a
i
= 0, 1, 2, 3, 4, 5, 6, 7.
N =16 a
i
= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C,D, E, F.
N =2 a
i
= 0, 1.
Khi a xut hin c s N, ta co th biu din s A di dang mt a
thc theo c s N, ky hiu la A
(N)
:

A
(N)
a
m-1
.N
m-1
a
m-2
.N
m-2
. . .. a
0
.N
0
a
-1
.N
-1
. . a
-n
.N
-n

Hay:

=
=
1 m
n i
i
i (N)
N a A

Vi N=10:
A
(10)
a
m-1
.10
m-1
a
m-1
.10
m-1
. . . . . a
0
.10
0
. . . a
-n
.10
-n

V du: 1999,999 =1.10
3
+9.10
2
+9.10
1
+9.10
-1
+9.10
-2
+9.10
-3

Vi N=2:
A
(2)
=a
m-1
.2
m-1
+ . . .+a
-n
2
-n

V du: 1111.110 = 1.2
3
+1.2
2
+ 1.2
1
+ 1.2
0
+ 1.2
-1
+ 1.2
-2
+ 0.2
-3

Vi N=16:
A
(16)
a
m-1
.16
m-1
a
m-2
16
m-2
. . . a
0
.16
0
..a
-1
16
-1
. . . a
-n
16
-n
V du: 3FFH = 3.16
2
+ 15.16
1
+ 15.16
0

1.1.3. Di co s
1.1.3.1. Di tu co s d sang co s 10
V phng phap, ngi ta khai trin con s trong c s d di dang
a thc theo c s cua no.
V du: A
(2)
= 1101, i sang thp phn la:
1101
(2)
= 1.2
3
+ 1.2
2
+ 0.2
1
+ 1.2
0
=13
(10)

1.1.3.2. Di co s 10 sang co s d
V nguyn tc, ngi ta ly con s trong c s chia lin tip cho c
s d n khi thng s bng khng th thi.
Chng 1. H thng s m va khai nim v ma Trang 3
V du:




1
1
13 2
2
1
2
2
3
6
0
15
16
0
16
16
3
63
15
1023

3
0 1

A
(10)
=13 A
(2)
=1101 A
(10)
=1023 A
(16)
=3FFH

Kt lun: Goi d
1
, d
2
, . . . . ..,d
n
ln lt la d s cua phep chia s thp
phn cho c s d ln th 1, 2, 3, 4, . . . . ., n th kt qua se la d
n
d
n-1
d
n-2

.. d
1
, ngha

la d s sau cung la bt co trong s cao nht (MSB), con
d s u tin la bt co trong s nho nht (LSB).
1.2. H DM NHJ PHN VA KHAI NIM V MA
1.2.1. H dm nhj phn
1.2.1.1. Khai nim
H m nh phn con goi la h m c s 2 la h m ma trong o
ngi ta ch s dung hai k hiu 0 va 1 biu din tt ca cac s. Hai
ky hiu o goi chung la bit hoc digit va no c trng cho mach in
t co hai trang thai n nh hay con goi la 2 trang thai bn FLIP-
FLOP (ky hiu la FF).
Mt nhom 4 bt goi la nibble.
Mt nhom 8 bt goi la byte.
Nhom nhiu bytes goi la t (word).
Xet s nh phn 4 bt: a
3
a
2
a
1
a
0
. Biu din di dang a thc theo c
s cua no la:
a
3
a
2
a
1
a
0
a
3
.2
3
a
2
. 2
2
a
1
.2
1
a
0
.2
0
Trong o:
- 2
0
, 2
1
, 2
2
, 2
3
(hay 1, 2, 4, 8) c goi la cac trong s.
- a
0
c goi la bit co trong s nho nht, hay con goi bit co y
ngha nho nht (LSB: Least Significant Bit) .
Bai giang Ky Thut S Trang 4
- a
3
c goi la bit co trong s ln nht, hay con goi la bt co y
ngha ln nht (MSB: Most Significant Bit).

Nh vy, vi s nh phn 4 bit a
3
a
2
a
1
a
0
ma trong o mi ch s a
i

ch nhn c hai gia tr {0,1}, luc o ta co 2
4
= 16 thp nh phn.

S thp phn a
3
a
2
a
1
a
0
S thp luc phn
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F

Chu y: Khi biu din s nh phn nhiu bit trn may tnh th thng
tranh sai sot, ngi ta thng biu din thng qua s thp phn
hoc thp luc phn, bat phn.
V du:
3
1 3 7 7 6

1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0
B E F E

Co th biu din : 137376
( 8 )
hoc 0BEFE
(H)
.
Chng 1. H thng s m va khai nim v ma Trang 5
1.2.1.2. Cac phep tinh trn s nhj phn
a. Phep cng
cng hai s nh phn, ngi ta da trn qui tc cng nh sau:
0 0 0 nho 0
0 1 1 nho 0
1 0 1 nho 0
1 1 0 nho 1
V du: 3 0011
2 0010
+ +
5 0101
b. Phep tr
0 - 0 0 muon 0
0 - 1 1 muon 1
1 - 0 1 muon 0
1 - 1 0 muon 0
V du: 7 0111
5 0101
-
-
2 0010 = 1.2
2
+ 0.2
1
+ 1.2
0
= 2

c. Phep nhn
0 . 0 0
0 . 1 0
1 . 0 0
1 . 1 1
V du: 7 0111
5 0101
x x
35 0111
0000
0111
0000
0100011 = 1.2
5
+ 1.2
1
+ 1.2
0
= 35

Bai giang Ky Thut S Trang 6
d. Phep chia
0 : 0 0
1 : 1 1
V du: 10 5 1010 101
2 101 10 = 2
00
0
ng dung thanh ghi dch thc hin phep toan nhn hai, chia hai:


Dch trai (nhn hai)
0 0 0 0 0 1 0 1 1
0 0 0 0 0 0 1 1
Dch phai (chia hai)
d
Thanh ghi sau khi nhn 2
Thanh ghi sau khi chia 2
1 1 1
Thanh ghi ban u
0 0 0 0 0 0

1


1.2.2. Khai nim v ma
1.2.2.1. Dai cuong
Trong i sng hang ngay, con ngi giao tip vi nhau thng qua
mt h thng ngn ng qui c, nhng trong may tnh ch x ly cac d
liu nh phn. Do o, mt vn t ra la lam th nao tao ra mt giao
din d dang gia ngi va may tnh, ngha la may tnh thc hin c
nhng bai toan do con ngi t ra.
thc hin iu o, ngi ta t ra vn v ma hoa d liu. Nh
vy, ma hoa la qua trnh bin i nhng ky hiu quen thuc cua con
ngi sang nhng ky hiu quen thuc vi may tnh.
Cac lnh vc ma hoa gm :
- S thp phn
- Ky t
- T p lnh
- Ting noi
- Hnh anh
- ..v..v..
Chng 1. H thng s m va khai nim v ma Trang 7
1.2.2.2. Ma hoa s thp phn
a. Khai nim
Trong thc t ma hoa s thp phn, ngi ta s dung cac s nh
phn 4 bit.
V du: 0 0000 ; 5 0101
1 0001 ; 6 0110
2 0010 ; 7 0101
3 0011 ; 8 1000
4 0100 ; 9 1001
Vic s dung cac s nh phn ma hoa cac s thp phn goi la cac
s BCD (Binary Code Decimal: S thp phn c ma hoa bng s
nh phn).
b. Phn loai
Khi s dung s nh phn 4 bit ma hoa cac s thp phn tng ng
vi 2
4
= 16 t hp ma nh phn phn bit.
Do vic chon 10 t hp trong 16 t hp ma hoa cac ky hiu thp
phn t 0 n 9 ma trong thc t xut hin nhiu loai ma BCD khac
nhau.
Mc du tn tai nhiu loai ma BCD khac nhau, nhng trong thc t
ngi ta chia lam hai loai chnh: BCD co trong s va BCD khng co
trong s.

b1. Ma BCD co trong s: gm co ma BCD t nhin, ma BCD s hoc.
Ma BCD t nhin o la loai ma ma trong o cac trong s thng
c sp xp theo th t tng dn.
V du: Ma BCD 8421 , ma BCD 5421
Ma BCD s hoc la loai ma ma trong o co tng cac trong s lun
lun bng 9.
V du: Loai ma: BCD 2421, BCD 5121, BCD 8 4-2-1
Suy ra ma BCD s hoc co c trng: tm t ma thp phn cua
mt s thp phn nao o ta ly bu (ao) t ma nh phn cua s bu 9
tng ng.
Bai giang Ky Thut S Trang 8
V du: 3 0011
Ma s 6 la bu 9 cua 3:
6 1100
Ly nghch ao ta co: 0011 = 3
Vy, c trng cua ma BCD s hoc la co tnh cht i xng qua mt
ng trung gian.

b2. Ma BCD khng co trong s: la loai ma khng cho phep phn tch
thanh a thc theo c s cua no.
V du: Ma Gray, Ma Gray tha 3.
c trng cua ma Gray la loai b ma ma trong o hai t ma nh
phn ng k tip nhau bao gi cung ch khac nhau 1 bit.
V du:
Con i vi ma BCD 8421:
3 0011
4 0100

Ma Gray: 2 0011
3 0010
4 0110




Cac bang di y trnh bay mt s loai ma thng dung:

Bang 1: Cac ma BCD t nhin.

BCD 8421 BCD 5421 BCD qua 3
a
3
a
2
a
1
a
0
b
3
b
2
b
1
b
0
c
3
c
2
c
1
c
0
S thp
phn
0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 1 0 0 0 1 0 1 0 0 1
0 0 1 0 0 0 1 0 0 1 0 1 2
0 0 1 1 0 0 1 1 0 1 1 0 3
0 1 0 0 0 1 0 0 0 1 1 1 4
0 1 0 1 1 0 0 0 1 0 0 0 5
0 1 1 0 1 0 0 1 1 0 0 1 6
0 1 1 1 1 0 1 0 1 0 1 0 7
1 0 0 0 1 0 1 1 1 0 1 1 8
1 0 0 1 1 1 0 0 1 1 0 0 9

Chng 1. H thng s m va khai nim v ma Trang 9
Bang 2: Cac ma BCD s hoc

BCD 2421 BCD 5121 BCD 84-2-1
a
3
a
2
a
1
a
0
b
3
B
2
b
1
b
0
c
3
c
2
c
1
c
0
S thp
phn
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1 0 1 1 1 1
0 0 1 0 0 0 1 0 0 1 1 0 2
0 0 1 1 0 0 1 1 0 1 0 1 3
0 1 0 0 0 1 0 0 0 1 0 0 4
1 0 1 1 1 0 0 0 1 0 1 1 5
1 1 0 0 1 1 0 0 1 0 1 0 6
1 1 0 1 1 1 0 1 1 0 0 1 7
1 1 1 0 1 1 1 0 1 0 0 0 8
1 1 1 1 1 1 1 1 1 1 1 1 9

Bang 3: BCD t nhin va ma Gray.

BCD 8421 BCD qua 3 Ma Gray Gray qua 3
a
3
a
2
a
1
a
0
c
3
c
2
c
1
c
0
G
3
G
2
G
1
G
0
g
3
g
2
g
1
g
0
S thp
phn
0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 1
0 0 1 0 0 1 0 1 0 0 1 1 0 1 1 1 2
0 0 1 1 0 1 1 0 0 0 1 0 0 1 0 1 3
0 1 0 0 0 1 1 1 0 1 1 0 0 1 0 0 4
0 1 0 1 1 0 0 0 0 1 1 1 1 1 0 0 5
0 1 1 0 1 0 0 1 0 1 0 1 1 1 0 1 6
0 1 1 1 1 0 1 0 0 1 0 0 1 1 1 1 7
1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 0 8
1 0 0 1 1 1 0 0 1 1 0 1 1 0 1 0 9

Chu y: Ma Gray c suy ra t ma BCD 8421 bng cach: cac bit 0,1
ng sau bit 0 ( ma BCD 8421) khi chuyn sang ma Gray th c
gi nguyn, con cac bit 0,1 ng sau bit 1 ( ma BCD 8421) khi
chuyn sang ma Gray th c i ngc lai, ngha la t bit 1 thanh
bit 0 va bit 0 thanh bit 1.
Bai giang Ky Thut S Trang 10
1.2.2.3. Mach nhn dang s BCD 8421 :




a
3
a
2
a
1
Mach nhn
dang s BCD
y
+ y = 1 a
3
a
2
a
1
a
0
khng phai s BCD 8421
+ y = 0 a
3
a
2
a
1
a
0
la s BCD 8421
Suy ra nhn dang mt s nh phn 4 bit khng phai la mt s
BCD 8421 th ngo ra y = 1, ngha la: bit a
3
lun lun bng 1 va bit a
1

hoc a
2
bng 1.
Phng trnh logic : y = a
3
(a
1
+ a
2
) = a
3
a
1
+ a
3
a
2
S logic:
a
1
y

a
2


a
3

Do vic xut hin s BCD nn co hai cach nhp d liu vao may
tnh: nhp s nh phn, nhp bng ma BCD.
nhp s BCD thp phn hai ch s th may tnh chia s thp
phn thanh cac cac va mi cac c biu din bng s BCD
tng ng.
V du: 11 (thp phn) co th c nhp vao may tnh theo 2 cach:
- S nh phn: 1011
- Ma BCD : 0001 0001
1.2.2.4. Cac phep tinh trn s BCD
a. Phep cng
S thp phn la 128 th:
- S nh phn la: 10000000
- S BCD la: 0001 0010 1000
Do s BCD ch co t 0 n 9 nn i vi nhng s thp phn ln
hn, no chia s thp phn thanh nhiu cac, mi cac c biu
din bng s BCD tng ng.
Chng 1. H thng s m va khai nim v ma Trang 11
5 0101 7 0111 7 0111
3 011 5 0101 3 0011 5 0101 0
+ + + +
8 1000 12 1100 8 1000 12 1100
0110 0110
S hiu chnh
+
0001 0010 0001 0010
1 2 1 2
b. Phep tr b. Phep tr
A - B = A + B
7 0111 0111
5 0101 1010
- - +
Bu 1 cua 5
2 0010 10001
1
+
Bu 2 cua 5
0010

Bu 1 la bit 0 thanh 1, bit 1 thanh 0.
Bu 2 la bu 1 cng thm 1.
Xet cac trng hp m rng:
- Thc hin tr 2 s BCD 1 cac ma s b tr nho hn s tr.
- M rng cho cng va tr 2 s BCD nhiu cac.