Vous êtes sur la page 1sur 110

TRNG AI HOC A LAT

F 7 G







GIAO TRNH
HP NG



NGUYEN HU LOC



2002
7cp qa z

MUC LU C

LI M AU ..................................................................................................................... 4
CHNG 1: GII THIEU CHUNG................................................................................... 5
I. CAC THANH GHI TRONG VI X LY HO INTEL: ................................................... 5
1. Cac thanh ghi thong dung: ....................................................................................... 6
2. Cac thanh ghi phan oan ......................................................................................... 7
3. Cac thanh ghi ch muc va con tro:............................................................................ 7
4. Thanh ghi trang thai (FLAG) ................................................................................... 8
II. A CH O NH: ....................................................................................................... 8
III. MOT SO LENH C BAN: ........................................................................................ 9

CHNG 2: GOI NGAT VA TRNH DEBUG..................................................................11
I. CAC LENH GOI NGAT: ...........................................................................................11
II. TRNH DEBUG:........................................................................................................12
III. VAI CHNG TRNH NG DUNG :.....................................................................15

CHNG 3: HP NG DUNG TRONG PC.....................................................................19
I. DONG LENH:.............................................................................................................19
II. D LIEU:...................................................................................................................20
1. Hang: ......................................................................................................................20
2. Bien: .......................................................................................................................21
III. CAU TRUC CHNG TRNH:...............................................................................22

CHNG 4: PHNG PHAP LAP TRNH......................................................................27
I. TONG QUAN: ............................................................................................................27
II. CAC LENH LIEN QUAN EN RE NHANH:...........................................................27
1. Cac lenh re nhanh co ieu kien: ............................................................................28
2. Cac lenh re nhanh khong ieu kien: ......................................................................29
III. CAC CAU TRUC IEU KHIEN:.............................................................................29

CHNG 5: NGAN XEP VA THU TUC..........................................................................38
I. NGAN XEP: ................................................................................................................38
II. THU TUC: .................................................................................................................38
III. S DUNG PTR VA LABEL: ...................................................................................40
IV. VAI CHNG TRNH NG DUNG:......................................................................41
1. Hien so thap phan: ..................................................................................................41
2. Hien so nh phan va thap luc phan..........................................................................44
3. oc t ban phm......................................................................................................46

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa +

CHNG 6: MOUN CHNG TRNH & CAC KIEU NH V A CH .............51
I. MOUN CHNG TRNH .......................................................................................51
1. nh hng bien dch extrn.....................................................................................51
2. nh hng bien dch public ...................................................................................52
3. Lap th vien - tao v lenh (macro) .........................................................................54
II. CAC KIEU NH V A CH..................................................................................56
III. CHNG TRNH NG DUNG...............................................................................60
IV. TAO TAP TIN .COM...............................................................................................67
1- Dang cua mot chng trnh COM...........................................................................68
2- ng dung tap tin .com............................................................................................69

CHNG 7: S DUNG PHAN OAN TOAN PHAN.....................................................76
I- S DUNG CAC NH HNG BIEN DCH............................................................76
1. Segment ..................................................................................................................76
2. nh hng bien dch assume.................................................................................79
II - DANG CUA CHNG TRNH .EXE S DUNG TRONG CAC PHNG PHAP
CU: .................................................................................................................................79
III- V DU CHNG TRNH COM..............................................................................83

CHNG 8: CHNG TRNH NG DUNG..................................................................85
I. IN NOI DUNG VAN BAN RA MAY IN: ...................................................................85
1. S dung ham 40h ngat 21h (DOS) e thc hien......................................................85
2. S dung ham 01h ngat 17h (ROMBIOS) ................................................................85
II. EM SO T TRONG MOT TAP TIN ......................................................................86
III- OI NGC CHUOI.............................................................................................89
IV. CHNG TRNH HIEN TH THI GIAN .............................................................90
V. CHNG TRNH THNG TRU BO NH............................................................97

Phu luc A: MOT SO CHNG TRNH PHUC VU CUA DOS.......................................105
Phu luc B: MOT SO CHNG TRNH PHUC VU CUA ROM-BIOS............................108
TAI LIEU THAM KHAO.................................................................................................110


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o


LI M AU

Hp ng la mot mon hoc danh cho sinh vien cac chuyen nganh ky thuat.
La mot ngon ng can thiep sau vao he thong may tnh, hp ng thng c s
dung trong nhng phan viec tng oi tinh te nh ieu khien he thong, vi ieu
khien ...

Tuy vay vi nhng ngi lan au tien lam quen vi ngon ng nay, viec
tiep thu qua that rat kho khan bi le ngon ng nay gan vi ngon ng may va s
dung cac ky t gi nh nen kho hieu hn s dung cac ngon ng cap cao nh
Pascal, C..v.v...Ngoai ra, ngi hoc khong nhng phai biet phan mem ma con
phai hieu phan cng mc o nao o na. ieu nay tao ra mot ap lc rat ln gay
kho khan cho ngi hoc.

Trong nhieu nam giang day mon nay, toi nhan thay hien co rat nhieu
sach, tai lieu tham khao viet ve hp ng song e co mot tai lieu ap ng yeu cau
cua sinh vien trong viec tiep thu mon hp ng tuan t t de en kho co phng
phap th that hiem.

Do vay toi thc hien giao trnh nay vi mong c co mot tai lieu ap ng
yeu cau tren. Trong giao trnh, toi co a vao rat nhieu v du va ng dung t thap
en cao e sinh vien co the tiep thu de dang mon hoc nay.

Tuy vay tai lieu nay khong sao tranh khoi nhng sai sot, rat mong cac
ong nghiep, sinh vien va ban oc gop y

Hoan thanh giao trnh nay, toi xin chan thanh cam n en cac ong
nghiep trong Khoa Vat ly Trng ai hoc Dalat a ong vien, gop y va tao ieu
kien cho toi trong qua trnh thc hien. Toi cung xin ghi nhan s ong gop cua
nhieu the he sinh vien khoa vat ly, khoa toan - tin e giao trnh nay c sap xep
ngay cang de hieu va hp ly hn.

Nguyen Hu Loc

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa s


CHNG 1: GII THIEU CHUNG


Tai sao chung ta phai hoc lap trnh vi hp ng trong khi hien nay
co rat nhieu ngon ng cap cao nh C hoac Pascal dung e lap trnh de dang
hn? Co the noi co nhieu cau tra li cho cau hoi nay. Nhng ban au, chung
ta co the xet oi vi hp ng th moi lenh tng ng vi mot lenh ngon ng
may. Noi cach khac, neu moi dong lenh trong chng trnh la mot lenh hp
ng va mot t may tng ng vi mot lenh may nh vay n dong trong chng
trnh se tao ra mot chng trnh may gom n t. Nh vay hp ng co the xem
la ngon ng rat gan vi ngon ng may, chung ta co the tiep can en tat ca cac
ngoc ngach cua may va quan trong hn la chung ta thc hien chng trnh
viet bang hp ng nhanh hn so vi viet bang nhng ngon ng cap cao khac.
Tuy vay cung can thay rang rat nhieu ngon ng cap cao ho tr giao dien vi
hp ng, noi cach khac la chung ta co the viet 1 oan hp ng trong chng
trnh viet bang ngon ng cap cao nh C hoac Pascal... ieu nay cho phep
chung ta lap trnh hieu qua hn vi nhng phan viec sat vi he thong va oi
hoi thi gian nhanh.

Trong kien truc may tnh nhieu cap, viec chuyen mot chng trnh
cua ngi s dung t mot ngon ng nay sang chng trnh dung ngon ng
khac goi la chng trnh dch. Ngon ng s dung e viet chng trnh ban au
goi la ngon ng nguon con ngon ng sau khi dch goi la ngon ng ch.

Ngi ta phan biet cac chng trnh dch ra lam hai loai tuy theo
moi quan he gia ngon ng nguon va ngon ng ch. Khi ngon ng nguon
gom nhng ky t gi nh lenh cua ngon ng may th chng trnh dch se goi
la hp dch (assembler) va ngon ng nguon la hp ng (assembly language).
Trong khi o ngon ng nguon la ngon ng cap cao nh C hoac Pascal th
chng trnh dch se goi la bie n dch.

I. CAC THANH GHI TRONG VI X LY HO INTEL:


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o

Bo vi x ly 8088 co the a ch hoa en 2
20
byte bat au t a ch 0. Nhng
lenh cua 8088 co the thc hien vi nhng t 8 bit hay byte cung nh vi nhng t 16 bits.
8088 s dung nhng a ch 20 bits e truy cap en 2
20
byte o nh. V cac thanh ghi co
chieu dai la 16 bits nen cac a ch dung 20 bits se co kho khan trong thc hien. e giai
quyet van e nay, nhng nha san xuat bo vi x ly a s dung 4 thanh ghi phan oan (mot
dung cho lenh, mot cho d lieu, mot cho ngan xep va mot d tr)

Nhng thanh ghi co the xem la nhng o nh nam ben trong bo vi x ly co thi
gian truy xuat cao, vi 8088, chung ta co 14 thanh ghi chia ra lam 4 loai:

- Cac thanh ghi lam viec;
- Cac thanh phan oan;
- Cac thanh ghi bc nhay (offset);
- Thanh ghi trang thai (C: FLAG)
1. Cac thanh ghi thong dung:

Bao gom 4 thanh ghi. Moi thanh ghi eu co nhng chc nang rieng mac du
chung cung la nhng ni lu tr nhng ket qua trung gian:

AX la thanh ghi 16 bit goi la thanh ghi tch luy (Accumulator) s dung khi
thc hien cac phep tnh so hoc.

BX la thanh ghi 16 bit goi la thanh ghi nen (Base register)

CX la thanh ghi 16 bit goi la thanh ghi em (Counter)

DX la thanh ghi 16 bit goi la thanh ghi d lieu (Data)

Nen biet rang moi thanh ghi nay eu co the phan ra lam hai thanh ghi 8 bit s
dung rieng re c:

AX = AH va AL;
BX = BH va BL;
CX = CH va CL;
DX = DH va DL.

Cung nen nh rang nhng phep toan thc hien vi cac thanh ghi luon nhanh
khi chung thc hien vi cac o nh trong bo nh.

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?

2. Cac thanh ghi phan oan

Gom 4 thanh ghi:

CS viet tat t "Code Segment", la ni lu tr a ch cua phan oan bo nh
tai o cac lenh trong chng trnh nap vao (ma may).

DS viet tat t "Data Segment", la ni lu tr a ch cua phan oan bo nh
tai o at d lieu (data) cua chng trnh.

SS viet tat t "Stack Segment", la ni lu tr a ch cua phan oan ngan
xep.

ES viet tat t "Extra Segment", la ni lu tr a ch cua phan oan d tr
thng dung ghi gi d lieu.

3. Cac thanh ghi ch muc va con tro:

Cac thanh ghi nay gi gia tr bc nhay (offset ) ket hp vi a ch phan oan.
Tong cong co 5 thanh ghi:

SI viet tat t "Source Index". Thanh ghi nay la thanh ghi 16 bit s dung khi
thc hien cac phep toan vi chuoi (day ky t); thng c dung ket hp vi thanh ghi
phan oan DS.
DI viet tat t "Destination Index". Thanh ghi nay cung la thanh ghi
16 bit s dung khi thc hien cac phep toan vi chuoi (day ky t); thng c
dung ket hp vi thanh ghi phan oan DS. Tuy nhien trong trng hp
chuyen chuoi th no se ket hp vi thanh ghi phan oan ES.

BP hoac "Base Pointer". Thanh ghi 16 bit nay ket hp vi thanh ghi phan oan
SS (SS:BP) e a d lieu vao trong ngan xep khi goi chng trnh con (CALL)

SP hoac "Stack Pointer". Thanh ghi 16 bit nay cung ket hp vi thanh ghi phan
oan SS (SS:SP) e ch ra phan t cuoi cua ngan xep.

IP goi la con tro lenh (Instruction Pointer). Thanh ghi 16 bit nay ket hp vi
thanh ghi phan oan CS (CS:IP) e ch en lenh tiep theo se thc hien. Noi dung cua

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa S

thanh ghi nay khong the thay oi trc tiep; no ch b thay oi mot cach gian tiep thong qua
cac lenh nhay bc, qua cac chng trnh con va qua cac ngat.

4. Thanh ghi trang thai (FLAG)

Thanh ghi trang thai la thanh ghi gom 16 bit (C). Gia tr cua tng bit mot
trong thanh ghi nay oc lap vi cac bit khac trong cung thanh ghi. Ngi ta thng x ly
tng bit mot va mot so bit co anh hng e n hoat ong cua chng trnh. Nhng bit nay
goi la cac ch bao (indicator) co the chia ra lam 2 loai:

Cac ch bao trang thai:

bit y ngha viet tat
0 Nh (Carry) CF
2 Chan le (Parity) PF
4 Nh phu (Auxiliairy carry) AF
6 Khong (Zero) ZF
7 Dau (Sign) SF
11 Tran (Overflow) OF
Cac ch bao ieu khien

bit y ngha viet tat
8 Bay (Trap) TF
9 Ngat (Interrupt) IF
10 Hng (Direction) DF

Cac bit 1, 5, 12, 13, 14, 15 cua thanh ghi trang thai 16 bit khong c s dung.
II. A CH O NH:

Bo nh cua 1 may tnh bao gom 1 day tuan t cac o nh c anh so trong o
lu gi 1 gia tr so nao o. So tng ng cua 1 o nh goi la a ch, moi o nh gi 8 bit.
Tren nhng may tnh IBM PC, bo nh c chia thanh cac vung 65536 byte goi la phan
oan, a ch cua 1 o nh se bao go m 2 phan: so phan oan va bc nhay (offset) tnh t v
tr ban au cua phan oan; no se co dang:

Phan oan: bc nhay
(Segment:Offset)


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa v

Cac lenh chiem mot hoac nhieu o nh ke tiep trong bo nh, ieu nay phu
thuoc vao o phc tap cua lenh.
III. MOT SO LENH C BAN:
MOV: la mot trong nhng lenh chnh cua hp ng. Noi dung cua toan hang ben
phai se chuyen sang toan hang ben trai
V du:
MOV AX, 10
MOV AX, BX
MOV AX, [BX]
LEA:(Load Effective Address) tng t nh MOV ....,OFFSET....
V du:LEA DX,MSG thay v MOV DX, OFSET MSG
ADD: cong 2 so 8 bit hoac 16 bit, ket qua a vao bo nh hoac thanh ghi.
V du:
ADD AX, BX
ADD AH, AL
INC: them mot n v vao thanh ghi cho trc, ket qua nhan c se tng t nh
CONG vi 1.
V du:
INC AX
INC BL
ADC: thc hien lenh cong nh ADD sau o them gia tr co trong ch bao CARRY
vao ket qua nhan c
V du:
ADC DL,30
SUB: thc hien tr 1 lng t 1 so, ket qua c at vao bo nh hoac thanh ghi.
V du:
SUB AL, BL
MUL: nhan toan hang cho trc vi mot lng at trong thanh AX hoac AL
(accumulator). Ket qua se at vao AH va AL neu cac toan hang la byte va at trong
DX va AX neu toan hang la t (word).
V du:
MUL BH
MUL BX
DIV: chia 1 t (so b chia trong AX) bi 1 toan hang 8 bit (so chia) hoac chia 1 t
oi - double word (trong DX, AX) b i mot toan hang 16 bit. Thng so se at trong
AL va so d at trong AH neu toan hang la mot byte va thng so se at trong AX va
so d at trong DX neu s dung 1 t-word.
V du: DIV BX
DIV BH

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 10

CMP: cho phep so sanh noi dung hai thanh ghi hoac o nh. So sanh la thc hien 1
phep tr tuy nhien muc ch at c khong pha i la ket qua cua phep tr ma chu
yeu la so sanh hai toan hang do vay sau lenh so sanh thng thay lenh nhay bc co
ieu kien phu thuoc vao cac ch bao trong qua trnh so sanh.
V du: CMP DL, 3A
JNZ: nhay bc en mot nhan (label) cho trc neu ch bao zero la 0.
V du:
JNZ Tiep
JLE: nhay bc en mot nhan (label) cho trc neu ket qua la nho hn hoac bang 0.
V du:
JLE Lap

LOOP: lap lai luc nay thanh ghi CX giam 1 n v va thc hien mot s re nhanh
en mot nhan cho trc cho en khi CX bang 0.
V du:
LOOP Lap
RCL: dch chuyen tat ca cac bit ve pha trai mot v tr hoac nhieu v tr. Nhng bit
ra khoi ben TRAI se vao ch bao CARRY trc khi a lai vao ben phai cua toan
hang.
V du:
RCL AH,1
SHR: dch chuyen tat ca cac bit ve pha phai mot v tr hoac nhieu v tr.
V du:
SHR AH,1


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 11


CHNG 2: GOI NGAT VA TRNH DEBUG

I. CAC LENH GOI NGAT:

Mot may tnh thng co rat nhieu loai thiet b ngoai vi quan trong nh ban
phm, man hnh, a, may in, v.v... Muon ieu khien cac thiet b nay oi hoi chung ta phai
viet chng trnh bang cac lenh may dai dong va phc tap. Nh s giup cua he ieu
hanh MSDOS hoac ROM-BIOS chung ta co the tac ong mot thiet b ngoai vi bang cach
ch s dung 1 ham c anh so vao chng trnh kem theo mot so cac thong so. Tat ca
ieu nay eu thc hien thong qua cac thanh ghi. Nhng ham nay goi la ngat.

Lenh INT la lenh goi ngat c anh so tai v tr toan hang c xem nh goi
mot chng trnh con. Mot ngat co the bao gom nhieu dch vu, so cua dch vu c a
vao thanh ghi AH trc khi goi ngat.

V du 1: Hien 1 ky t ASCII len man hnh

MOV DL,41h
MOV AH,02h
INT 21h

V du 2: oc ky t tiep t ban phm

MOV AH,0
INT 16h

V du 3: Hien 1 thong bao len man hnh

Chao DB 'Chao cac ban $'

MOV AH,09h
MOV DX, OFFSET Chao
INT 21h
V du 4: Ket thuc chng trnh

INT 20h


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1z

Chung ta co the tham khao cac ham nay trong tai lieu "Cam nang cho ngi
lap trnh may vi tnh IBM PC" - tac gia Peter Norton
II. TRNH DEBUG:

Mot trong hai cach e viet chng trnh bang hp ng la s dung phan mem
DEBUG. Chung ta can phai co mot tap tin ten DEBUG.EXE s dung trong he ieu hanh
MSDOS

Co the xem DEBUG la mot cong cu giup chung ta lap trnh bang hp ng ban
au v nhng ly do sau:

- DEBUG s dung de hn MASM.
- Chung ta co the th nghiem cac chng trnh nho de dang trc khi a
chung vao trong nhng chng trnh ln, phc tap hn.

DEBUG co 19 lenh. Cac lenh nay dung mau t au cua t tieng
Anh e gi nh y ngha cua lenh.

LENH T TIENG ANH CHC NANG
1 2 3
A Assemble Hp dch 1 chng trnh
C Compare So sanh 2 vung cua bo nh
D Dump Trnh bay mot vung cua bo nh bang hex va
ASCII
E Enter Nhap ma may vao bo nh
F Fill Lap ay mot vung nh
G Go Thc hien mot chng trnh
H Hexadecimal Cong va tr 2 so hex
I Input Nhap va trnh bay t cong
L Load Nhap mot tap tin t a vao bo nh
1 2 3
M Move Chuyen mot khoi nh
N Name at ten mot tap tin
O Out a ra cong
P Proceed Thc hien thu tuc hay mot ngat
Q Quit Thoat khoi Debug
R Register Trnh bay gia tr thanh ghi

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1+

S Search Tm mot chuoi byte trong bo nh
T Trace Cho chng trnh chay tng lenh mot
U Unassemble Dch ngc
W Write Ghi len a

e khi ong trnh DEBUG, ngi ta anh lenh sau:

DEBUG

- Mot gach ngang (hyphen) xuat hien thong bao cho chung ta biet DEBUG
ang i len tiep.

Nhng lenh DEBUG thng s dung:

Q (Quit): Dng thc hien DEBUG, ve lai he ieu hanh

D (Dump Memory): Hien noi dung mot vung nh.

V du: D 100

xxxx:0100 E9 2D 0D BA DA E7 F5 53 - 00 75 1B 0A 7D 3E 89 90
xxxx:0110 00 67 8A C9 65 3E 7F 1D -5A 6B 7E 90 8B 5A 8F 39
xxxx:0120 E9 2D 0D BA DA E7 F5 53 - 00 75 1B 0A 7D 3E 89 90
xxxx:0130 00 67 8A C9 65 3E 7F 1D -5A 6B 7E 90 8B 5A 8F 39

U (UnAssemble): Cho hien ma may chng trnh.

V du: U 100
xxxx:0100 E92D0D JMP 0E30
xxxx:0103 BADA0A MOV DX,0ADA
xxxx:0106 3D0500 CMP AX,0005
xxxx:0109 741B JZ 0126

R (Registers): Hien hoac thay oi noi dung cac thanh ghi cua bo vi x ly. Lenh
nay cho hien:

- Noi dung cua cac thanh ghi trong he c so 16 (hexadecimal).
- Noi dung cua thanh ghi trang thai

Ch bao =0 =1
OF NV OV

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1o

DF UP DN
IF EI DI
SF PL NG
ZF NZ ZR
AF NA AC
PF PO PE
CF NC CY

V du: R
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=6800 ES=6800 SS=6800 CS=6800 IP=0100 NV UP EI PL NZ NA PO NC

6800:0100 E92D0D JMP 0E30

A (Assemble): nhap lenh hp ng. Chu y, DEBUG ch dung so trong he c so
16 ch khong dung he c so nao khac.

V du: A 100
6800:0100 MOV AX,FFFF
6800:0103 MOV AH,5
6800:0105 MOV BH,6
6800:0107 MOV AL,BH
6800:0109

E (Entry): Thay oi noi dung o nh.
V du: E 120

G (Go): Thc hien chng trnh


N (Name): at ten tap tin
V du: N DIAMOND.COM

W (Write): Ghi lai tap tin len a, chu y trc o phai nhap CX (so byte s
dung trong chng trnh) va BX= 0 (a A)

L (Load): Lay lai noi dung tap tin vao bo nh, chu y trc o phai xac nh ten
tap tin, o a ang lu gi


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1s

T (Trace): thc hien chng trnh tuan t tng lenh mot co thong bao noi dung
cua cac thanh ghi thay oi sau moi lenh.

III. VAI CHNG TRNH NG DUNG :

Chng trnh 1: Hien 1 ky t () len man hnh

-A
xxxx:0100 MOV DL,4; maASCII
xxxx:0102 MOV AH,2; ky t
xxxx:0104 INT 21
xxxx:0106 INT 20
-G
-U
-N DIAMOND.COM
-R CX (=8)
-R BX (=0)
-W
-Q


Chng trnh 2: Hien 1 chuoi (Chao cac ban) len man hnh

Cach 1:
-E 120
43 6F 68 61 20 63 61 63 20
62 61 6E

-A
xxxx:0100 MOV BX,120
xxxx:0103 MOV CX,0C
xxxx:0106 MOV DL,[BX]
xxxx:0108 MOV AH,2
xxxx:010A INT 21
xxxx:010C INC BX
xxxx:010D LOOP 106
xxxx:010F INT 20
-U
-G

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1o


* Sinh vien co the at ten roi lu vao a
Cach 2: S dung gia lenh DB

-A
xxxx:0100 MOV AH,9
xxxx:0102 MOV DX,109
xxxx:0105 INT 21
xxxx:0107 INT 20
xxxx:0109 DB 'Chao cac ban! $'

-U
-G

Chng trnh 3: Cong 10 so nguyen au tien

-A
xxxx:0100 MOV CX,1
xxxx:0103 MOV AX,0
xxxx:0106 ADD AX,CX
xxxx:0108 INC CX
xxxx:0109 CMP CX,0A
xxxx:010C JLE 106
xxxx:010E INT 20

-U
-T
Chng trnh 4: Hien 5 ky t ABCDE len man hnh
-A
xxxx:0100 MOV DL,41
xxxx:0102 MOV CX,5
xxxx:0105 CALL 10E
xxxx:0108 LOOP 105
xxxx:010A MOV AH,4C
xxxx:010C INT 21
xxxx:010E MOV AH,2
xxxx:0110 INT 21
xxxx:0112 INC DL
xxxx:0114 RET

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1?

-N HIEN.COM
-R CX (=F)
-R BX (=0)
-W
-G

DEBUG
-N HIEN.COM
-L
-U

Chng trnh 5: Thc hien cac phep toan + , - , x , /

1 byte:

-A
xxxx:0100 MOV AL,05
MOV BL,03
ADD AL,BL
MOV BL,02
SUB AL,BL
MOV CL,03
MUL CL
MOV CL,02
DIV CL
INT 20
-U
-T

1 word = 2 byte

-A
xxxx:0100 MOV AX,3A62
MOV CX,2B14
ADD AX,CX
MOV CX,1002
SUB AX,CX
MUL CX
MOV AX,A034

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1S

MOV DX,0001
MOV BX,1002
DIV BX
INT 20
-U
-T




















qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 1v

CHNG 3: HP NG DUNG TRONG PC

Hien nay khi viet chng trnh bang hp ng, chung ta thng s dung trnh
dch MICROSOFT MACRO ASSEMBLER (MASM). Cung giong tat ca cac ngon ng lap
trnh khac nh Basic, Pascal, C eu phai thong qua dch th may tnh mi hieu c lenh
sau khi a tao ra ma nguon trong mot moi trng ket hp soan thao. e soan thao mot
chng trnh bang hp ng chung ta ch can s dung mot trnh soan thao van ban ASCII v
du nh Sidekick, phan soan thao trong Norton Commander...v.v... va sau o se lu gi vao
tap tin ASCII tren a. Bc tiep theo la dch ma nguon sang ma may nh trnh dch
(Assembler).

Chng trnh c lu tren a la mot tap tin co phan m rong ASM c soan
thao bang mot phan mem soan thao van ban (ASCII) trong o cac ch cai viet HOA eu
c x ly nh nhng ch cai viet thng tr trng hp at trong 1 chuoi (day ky t).

Cac chng trnh bao gom nhng dong lenh. Moi dong lenh la mot lenh c
dch sang ma may bang cac trnh dch.
I. DONG LENH:

Moi dong lenh gom 4 vung

- Ten (identifier)
- Phep toan
- Toan hang
- Chu thch

Thng theo thoi quen ngi ta chia lam 4 cot dung cho 4 vung e chng trnh c ro
rang mac du khong co mot s bat buoc nao ve viec chia nay ca.

TEN PHEP TOAN TOAN HANG ; CHU THCH

V du:
Batdau: MOV CX,5 ; at so em ban au
Cac vung khac nhau phai ngan cach bi t nhat mot khoang trong (space) hoac
TAB, cac toan hang trong mot lenh phai cac nhau bi 1 dau phay (trong trng hp nay
khong can thiet phai co khoang trong).


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa z0

Cac ten c lap trnh vien t do at e xac nh nhan, bien, thu tuc, v.v...Ten
gom cac ky t theo yeu cau sau: Ky t th nhat la ch cai, so ky t toi a la 31 va khong
co khoang trong trong ten.

Nhng phep toan la nhng ky t gi nh lenh: MOV, ADD, INC... hoac gia
lenh (pseudo-instruction): DB, PROC...

Nhng toan hang trong mot lenh se ngan cach bang mot dau phay.
Neu co 2 toan hang th toang hang th nhat la ch con toan hang th hai la
nguon.

Chung ta cung thay co the a them chu thch e de theo doi oc chng
trnh. Dau ";" phai c s dung trc khi ghi chu thch.

II. D LIEU:
1. Hang:

Hang la mot gia tr xac nh bang mot lenh hay ung hn la mot gia lenh
hp ng, gia tr nay c lap trnh vien nh ngha la khong thay oi trong qua trnh thc
hien chng.

Gia lenh EQU la lenh hp ng dung e nh ngha mot hang ng vi mot
ten c lap trnh vien at cho.

V du: Dung EQU 1
Sai EQU 0
Nhng hang tc thi (immediate) la:

. Nhng ch so do lap trnh vien at ra co the s dung trong cac he c so
khac nhau. Do vay nen them ch cai "h" sau nhng so dung trong he c so 16, va them
ch cai "b" sau tat ca cac so nh phan v trnh dch hp ng mac nhien ch lam viec trong
he c so 10 (thap phan) (can chu y la trnh DEBUG ch lam viec vi he c so 16).

V du: 1B4Dh, 1100b

Nhng ch so trong he c so 16 (hexadecimal) bat au bang mot ch cai
th phai them vao mot so "0" neu khong trnh dch hp ng se xem nh mot TEN va
thong bao loi.

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa z1


. Nhng ky t: Nhng ky t hoac chuoi (day ky t) eu phai
c anh dau nhay n hoac oi.

V du: "A", 'Hello'

2. Bien:

Cac bien se chiem cac vung nh trong qua trnh dch nh s dung cac lenh gia.

Lenh gia DB khai bao vung nh theo byte, lap trnh vien co the at ten cho
phan au vung nh. Lenh gia nay cung xac nh vung nh c dung cho ch so hay cac
ch cai.

Chu y: neu co nhieu phan t du ng trong lenh nay th nhng phan t
nay phai c ngan cach bang nhng dau phay.

Loai BYTE c dung cho cac so co gia tr trong khoang t 0 en
255.
V du:
- Khai bao mot bien loai BYTE co gia tr ban au la 0, Ten la "So".
So DB 0
- Khai bao vung nh co 3 phan t loai BYTE
XYZ DB 1,2,3

- Khai bao mot bien khong co gia tr ban au
Khac DB ?

- Khai bao mot mang co 5 phan t khong co gia tr ban au loai BYTE:
Mang DB 5 DUP(?)

- Khai bao mot mang co 25 phan t, phan t th nhat la 1, th hai la 2, v.v...:
Mangkhac DB 5 DUP (1,2,3,4,5)

DUP bao cho trnh dch biet la thc hiep lap lai (DUPlicate). Nh
vay nhng gia tr co trong ngoac n se lap lai cho en het.


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa zz

Lenh gia DW khai bao vung nh gom cac t (word)

Lenh gia DD khai bao vung nh gom cac t oi (double word)

Lenh gia DF, DP -> 6byte, DQ -> 8byte; DT-> 10byte

Chuoi ky t: Lenh gia DB cung c s dung e nh ngha chuoi ky. Mot
chuoi ky t luon c at trong dau nhay; neu chuoi c cho hien len man hnh th
phai ket thuc bang dau '$'

V du:
Thongbao DB 'Chao cac ban $'
Baoloi DB 'Co loi, e ngh lam lai $'

Mang: Mang la mot tap hp cac d lieu cung loai (byte, word...). Mang se giup
cho lap trnh vien de dang lap cac vong lap n gian trong mot chng trnh, muc
ch la truy cap tuan t en cac d lieu can thiet theo th t.
V du 1:
Vi khai bao SO DB 5,2,7

Chung ta co:

SO - 5 hoac xxxx:0100 - 5
SO+1 - 2 hoac xxxx:0101 - 2
SO+2 - 7 hoac xxxx:0102 - 7
V du 2:
MANG DW 12F9h, 15AEh
th
MANG - F9h
MANG+1 - 12h
MANG+2 - AEh
MANG+3 - 15h
III. CAU TRUC CHNG TRNH:

Cac chng trnh viet bang ma may bao gom cac phan ma phep toan, d lieu,
ngan xep. Moi phan chiem mot phan oan nh. Chng trnh viet bang hp ng
cung theo cau truc nh vay. Luc ay d lieu va ngan xep c ghi thanh cac phan
oan trong chng trnh . Moi phan oan cua chng trnh se c dch sang mot
phan oan nh trong qua trnh dch.

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa z+


1. Loai nh:

Ngi ta co the s dung nh hng bien dch (directive) e xac nh loai nh:

CU PHAP: .MODEL loai nh

Thng ngi ta s dung cac loai sau:

SMALL, MEDIUM, COMPACT, LARGE....

SMALL: Cac ma phep toan at trong mot phan oan
Cac d lieu at trong mot phan oan.
MEDIUM: Cac ma phep toan at trong nhieu phan oan (hn 1)
Cac d lieu at trong mot phan oan

COMPACT: Cac ma phep toan at trong mot phan oan Cac d lieu at
trong nhieu phan oan (hn 1)

LARGE: Cac ma phep toan at trong nhieu phan oan (hn 1)
Cac d lieu at trong nhieu phan oan (hn 1)

Khong the co cac mang chiem nhieu hn 64Kbyte

2. Phan oan d lieu:

Phan oan d lieu trong mot chng trnh bao gom tat ca nhng bien xac nh.
Ngi ta s dung nh hng bien .DATA e nh ngha vung cho bien va hang.
V du:
.DATA
WORD1 DW 2
MSG DB 'Chao'
SO EQU 10101111b

3. Phan oan ngan xep:

Ngi ta thng danh mot phan cua bo nh e tao ngan xep.
.STACK kch thc
Kch thc c at tuy y theo byte.

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa zo


V du:
.STACK 100h
Neu chung ta khong at kch thc cho phan oan nay th mac nhien phan
oan ngan xep se lay kch thc la 1KByte

4. Phan oan chng trnh

.CODE tenchngtrnh

"tenchngtrnh" la mot ten do lap trnh vien at cho chng trnh. Thng ten
chng trnh co the khong can at nhat la trong trng hp loai nh la SMALL.

Co rat nhieu phng phap e lap chng trnh, tuy vay tot nhat nen chia nho
chng trnh thanh nhieu chng trnh con (thu tuc)

Khi tao mot thu tuc ngi ta thng s dung lenh gia PROC au cua thu tuc
va ENDP sau lenh cuoi cua thu tuc.

Dang chung khi s dung cac lenh gia nay la:

tenthutuc PROC
;lenh
tenthutuc ENDP

V du .CODE
MAIN PROC
; cac lenh cua chng trnh chnh
MAIN ENDP
; nhng thu tuc khac

Tom lai:e viet mot chng trnh chung ta co the co cau truc n gian chung
nh sau

.MODEL SMALL
.STACK 100h
.DATA
;nh ngha d lieu
.CODE

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa zs

MAIN PROC
; lenh
MAIN ENDP
; cac thu tuc khac
END MAIN

5. Cac cong cu can thiet:
Co rat nhieu cach e ma hoa mot chng trnh hp ng. Cach au tien la ma
hoa trc tiep cac lenh trong bo nh bang trnh DEBUG. Cach th hai la ma hoa ma
"nguon" trong mot tap tin roi se dch e tao ra mot chng trnh thc hien c.
Cach nay la cach ma ngi ta thng dung nhat va gom cac bc sau:

a- Chuan b lap trnh, tao cau truc, xac lap cac phep tnh,.. va xac nh lu o
thuat toan. Bc nay ch can viet tren giay.

b- Viet chng trnh bang cac lenh nh mot phan mem soan thao van ban
ASCII nh EDLIN, Sidekick hoac mot phan mem soan thao bat ky mien sao ghi lai
c mot tap tin theo ma.
Tap tin phai co phan m rong la ASM.

c- Dch ma nguon sang "ma may" bang cac s dung trnh dch hp ng
(MASM). Trong bc nay thng xuat hien cac thong bao loi, trong trng hp o
phai sa loi roi lai tiep tuc dch cho en khi khong con mot loi nao na.
Sau bc nay chung ta se thu nhan c mot tap tin co phan m rong la OBJ

d- e tao mot chng trnh thc hien c, chung ta s dung trnh khac la
LINK.EXE
Sau bc nay chung ta se thu nhan c mot tap tin co phan m rong la EXE

e- Kiem tra lai chng trnh co thc hien tot khong bang cach go phan ten tap
tin va nhan ENTER.

Meo nho:
o Chung ta co the viet nhng chng trnh nho chay trong trnh
DEBUG tng lenh mot e hieu ro cach x ly tng lenh mot sau o
co the ghep lai trong mot chng trnh ln hn nham tao tap tin
ma nguon nh neu tren.
o Chung ta co the thc hien cac bc tren tuan t mot cach t ong
neu s dung tap tin bo lenh vi phan m rong la BAT

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa zo


Cung can nen biet rang khi mot chng trnh thng tru trong bo nh, DOS se
danh 256 byte cho phan bat au PSP (Program Segment Prefix). PSP gi toan bo
cac thong tin cua chng trnh. DS va ES gi a ch cua PSP trc khi thc hien
chng trnh. Nh vay DS khong gi a ch cua phan oan d lieu. e giai quyet
van e nay neu co s du ng a ch phan oan d lieu ntrong chng trnh th phai s
dung them 2 dong lenh sau:
MOV AX,@DATA
MOV DS,AX

V du: Viet mot chng trnh vi ten tap tin la CHAO.ASM dung phan mem
soan thao van ban ASCII bat ky

.MODEL SMALL
.DATA
MSG DB 'Chao cac ban!$'
.STACK 100h
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG
MOV AH,9 ;Hien thong bao
INT 21h
MOV AH,4Ch ;Dngchng trnh
INT 21h
MAIN ENDP
END MAIN

Sau khi ghi tap tin vao a, thc hien cac bc sau:
MASM CHAO;
LINK CHAO;
Sau o e thc hien chng trnh, go CHAO

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa z?

CHNG 4: PHNG PHAP LAP TRNH

I. TONG QUAN:

Lap trnh bang hp ng cung giong nh rat nhieu ngon ng lap trnh khac
khong kho nhng can phai to chc tot. Thng chung ta thay cac chng trnh a ra
that kho oc va khong tuan theo luat le nao ca, moi lap trnh vien lam theo kieu cua
ho va ket qua la chng trnh ch co the hieu c bi ch chnh tac gia cua no ma
thoi

Co rat nhieu phng phap c a ra ngo hau tao ra mot so qui tac e lap
trnh. Trong so cac phng phap nay, co mot phng phap bat chc cach lap trnh
bang ngon ng cap cao nh C, Pascal trong o co nhieu lenh CHUAN cho phep lap
trnh en 90% van e:

IF . . . THEN . . . ELSE

CASE . . . OF

FOR

WHILE . . .

REPEAT . . .

Co the thay rang cac lenh hp ng co chc nang cua:

. IF lap lai nh nhan IF1, THEN1, ELSE1, ENDIF1; lenh IF tiep theo nh IF2,
THEN2, ELSE2, ENDIF2,...;

. FOR se lap lai nh nhan FOR1.



II. CAC LENH LIEN QUAN EN RE NHANH:

CMP ch, nguon
TEST ch, nguon

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa zS


Lenh so sanh CMP thc hien giong phep tr SUB nhng ket qua lai khong gi
lai, ngha la toan han nguon va ch eu khong b anh hng. Nhng bit trong thanh
ghi trang thai eu b tac ong nh khi thc hien phep tr. Lenh nay thng c s
dung e tm ra moi quan he gia hai toan hang (so sanh).

Lenh TEST thc hien giong phep VA logic (AND) nhng ket qua khong gi
lai. ieu nay co ngha la toan hang ch va nguon khong oi ch co cac bit trong
thanh ghi trang thai se thay oi. Chung chu yeu s dung e xac nh la 1 bit nao o
co gia tr la 1 hoac la 0 .
1. Cac lenh re nhanh co ieu kien:

Nhng lenh re nhanh co ieu kien eu thc hien sau nhng lenh tac ong en
thanh ghi trang thai. Nhng lenh en sau re nhanh se nam trong khoang t -128 en
+127 byte tnh t lenh ke tiep lenh re nhanh. Do vay e m rong pham vi re nhanh
co ieu kien, chung ta co the ket hp vi lenh re nhanh khong ieu kien.

Jcc ch

Nhng lenh re nhanh co ieu kien nay luon kiem tra cac bit trong thanh ghi
trang thai a c xac nh nh lenh trc o. Neu ieu kien ung, bc nhay se
thc hien en a ch ch. Trnh dch hp ng se tnh toan khoang cach en ch e
xac nh so byte khoang cach t lenh ke tiep sau re nhanh co ieu kien en ch.




ieu kien khong dau
ieu kien Mo ta Kiem tra
A/NBE Tren / Khong di lan bang CF ou ZF = 0
AE/NB Tren hoac bang/ Khong di CF=0
B/NAE Di / Khong tren lan bang CF=1
BE/NA Di hoac bang / Khong tren CF ou ZF = 1
C Nh bang 1 CF=1
NC Nh bang 0 CF=0
P/PE Chan PF=1
NP/PO Le PF=0


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa zv

ieu kien co dau
ieu kien Mo ta Kiem tra
G/NLE Ln hn / khong nho hn lan bang ((SF xor OF) or ZF)= 0
GE/NL Ln hn hoac bang / khong nho hn (SF xor OF)=0
L/NGE Nho hn / khong ln hn lan bang (SF xor OF)=1
LE/NG Nho hn hoac bang / Khong ln hn ((SF xor OF) or ZF)= 1
O Tran bang 1 OF=1
NO Tran bang 0 OF=0
S Dau dng SF=1
NS Dau am SF=0

ieu kien zero
ieu kien Mo ta Condition testee
E/Z Bang / zero ZF = 1
NE/NZ Khong bang / Khong zero ZF = 0

2. Cac lenh re nhanh khong ieu kien:

o la nhng lenh nhay bc en mot v tr nao o trong chng trnh.
Lenh nhay khong ieu kien co dang:

JMP ch
Lenh nay co the nhay en bat ky mot v tr nao trong chng trnh.
III. CAC CAU TRUC IEU KHIEN:
1- La chon
a. IF THEN

ieu kien
Lenh 1

Sai



ung






qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa +0


Neu <ieu kien> Th <lenh 1>

V du: Neu AX > 0 Th AX -AX
Hp ng:
. . .
CMP AX , 0
JNG END_IF
NEG AX
. . .
END_IF:

b. IF THEN ELSE



qaqeo 7aa 1ce 7hco 7ot 1q








Neu <ieu kien> Th <Lenh 1>
Con lai <lenh 2>
Lenh 2
Lenh 1
ung
ieu kien
Sai

V du: Neu AL <= BL Th hien BL Con lai
hien AL

Hp ng:
. . .
CMP AL , BL
JNBE EL_SE
MOV DL , BL
JMP HIEN
EL_SE:
MOV DL, AL
HIEN:
MOV AH,2

7cp qa +1

INT 21h
. . .
END_IF:

c. CASE OF

Bieu thc
chon






qaqeo 7aa 1ce 7hco 7ot 1q




Lenh 1 Lenh 2 Lenh 3 Lenh n
. . .
Trng hp
BX=1: AX <- 1
BX=2: AX <- 5
BX=3: AX <- 10
Con lai
AX <- 0
HetTruonghop
Hp ng

Case1:
Case1C1:
CMP BX, 1
JNZ Case1C2
MOV AX, 1
JMP EndCase1
Case1C2:
CMP BX, 2
JNZ Case1C3
MOV AX, 5
JMP EndCase1
Case1C3:
CMP BX, 3
JNZ Other1
MOV AX, 10

7cp qa +z

JMP EndCase1
Other1:
MOV AX, 0
ENDCase1:


d. Cac ieu kien phc:

Dang: ieu kien 1 VA ieu kien 2
ieu kien 1 HOAC ieu kien 2

* ieu kien VA:

ieu kien phc (VA) ung ch khi ca 2 ieu kien eu ung. Ngc lai, neu
mot trong cac trng hp la sai th ieu kien phc (VA) la sai.

V du: oc mot ky t. Neu la ch cai HOA th se cho hien len.


oc mot ky t
Neu ( ky t >='A' ) VA (ky t <= 'Z' )
Th Hien ky t
HetNeu

Hp ng
; oc mot ky t
MOV AH, 1
INT 21h
; Neu ( 'A' <= ky t ) VA (ky t <= 'Z' )
CMP AL, 'A'
JNAE FSI
CMP AL, 'Z'
JNBE FSI
MOV DL,AL
MOV AH,2
INT 21h
HetNeu:

* ieu kien HOAC

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ++


(ieu kien 1 HOAC ieu kien 2) la ung neu ieu kien 1 hoac ieu kien 2
ung. ieu kien phc se sai neu ca hai ieu kien eu sai.

V du: oc mot ky t. Neu ky t la 'C' hoac 'c', cho hien ky t ay. Con
khong th dng chng trnh.

oc mot ky t

Neu (ky t='C') hoac (ky t='c')

Th hien ky t

Con khong th dng chng trnh

Hp ng:
; oc mot ky t
MOV AH,1
INT 21h
; Neu (ky t='C') hoac (ky t='c')

CMP AL, 'C'
JE THI
CMP AL, 'c'
JE THI
JMP CONKHONG
THI:
MOV AH,2
MOV DL,AL
INT 21h
JMP HET
CONKHONG:
MOV AH,4Ch
INT 21h
HET:

2- Vong lap:


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa +o

Hp ng co nhng lenh ac biet cho phep tao cac vong lap trong mot
chng trnh.

LOOP ch

Lenh LOOP lam giam noi dung cua thanh ghi CX mot n v va nhay en
a ch ch neu noi dung CX khong bang 0. Neu thanh ghi CX bang 0, vong lap
ket thuc va lenh ke tiep lenh LOOP se c thc hien. a ch ch can nam trong
khoang -128 va 127 byte. Lenh nay khong tac ong en cac bit cua thanh ghi
trang thai

JCXZ ch

Lenh JCXZ thc hien nhay bc en a ch ch neu thanh ghi CX bang 0.
Lenh nay thng s dung au mot vong lap.
a - Vong lap FOR:

So vong lap la co nh.
CHO so ban au THC HIEN lenh
KET THUC

Lenh thc hien
em <- em -1
em=0?
Xac nh so vong lap













Sai

ung


; at CX = so vong lap

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa +s

Dau:
;lenh

LOOP Dau

V du: FOR 80 DO hien '*'

Hp ng
MOV CX,80
MOV AH,2
MOV DL,'*'
Dau:
INT 21h
LOOP Dau
b - Vong lap WHILE

Vong lap nay thc hien theo mot ieu kien.

KHI ieu kien THC HIEN
lenh
KET THUC



ieu kien
lenh
ung








Sai




V du: BX <-- 5

Khi BX>0 thc hien BX <-- BX -1

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa +o


Ket thuc

Hp ng
MOV BX, 5
While1:
CMP BX,0
JLE EndWhile1
DEC BX
JMP While1
EndWhile1:


c - Vong lap REPEAT... UNTIL




qaqeo 7aa 1ce 7hco 7ot 1q









V du: BX <-- 10
lenh
ieu kien
Sai
ung
Lap lai
BX < BX-1
en khi BX <= 0

Hp ng

MOV BX,10
Repeat1:
DEC BX
CMP BX, 0
JNLE Repeat1
EndRepeat1:

7cp qa +?


Thc hien lap trnh theo trnh t nh tren oi hoi mot s can man nao o
nhng lai giup lap trnh vien thay c moi th rat ro rang. Chnh v vay viec lap
trnh tr nen n gian hn rat nhieu. Moi lap trnh vien co the co mot phng
phap lap trnh song can nen biet rang ne u chung ta lam viec theo nhom hoac mot
chng trnh c phan thanh nhieu moun th viec tuan theo nhng qui c la
cc ky can thiet: v du nh tat ca cac nhan at ra phai gi nh va tuan theo qui
c giup cho nhng ngi khac de hieu va s dung viet cac moun chung.


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa +S

CHNG 5: NGAN XEP VA THU TUC

I. NGAN XEP:

Ngan xep la ni lu tr d lieu tc thi trong bo nh. Dung lng bo nh
danh cho ngan xep c lap trnh vien at ra trong chng trnh nguon.

Nguyen tac hoat ong cua ngan xep la d lieu vao sau th ra trc goi la
ngan xep LIFO (Last In, First Out). D lieu a vao trong ngan xep chong len
nhau do vay ngi ta con goi la xep chong va thanh ghi SP (Stack pointer) c
s dung nh con tro ngan xep xac nh v tr cuoi trong ngan.

Ngan xep lu gi noi dung cac thanh ghi hoac cac bien.
Lenh e a d lieu vao ngan xep la:

PUSH nguon

Lenh PUSH giam gia tr cua thanh ghi xuong 2 n v neu nguon la thanh
ghi 16 bit va luc ay v tr nh cua ngan xep se ghi trong con tro ngan xep SP.

Lenh e lay d lieu ra khoi ngan xep la:

POP ch

Lenh POP tra lai noi dung lu gi trong ngan xep cho cac thanh ghi. Noi
dung thanh ghi SP tang len 2 n v neu thanh ghi nhan d lieu 16 bit.

II. THU TUC:
Cac thu tuc (hoac chng trnh con) bao gom mot day cac dong lenh noi
tiep nhau e thc hien mot cong viec a xac nh. Cong viec nay co the c goi
en nhieu lan trong mot chng trnh. Thay v c viet lap lai oan lenh nay trong
chng trnh, lap trnh vien at tat ca cac dong lenh nay vao mot khoi goi la thu
tuc. Sau o khi can lap trnh vien c goi thu tuc nay bat c luc nao tuy thch.

Cac thu tuc c chia ra lam hai loai:

- Cac thu tuc NEAR c khai bao trong cung phan oan ma cua chng
trnh goi. a ch tr ve ghi trong ngan xep ch la IP (ma 16 bit).

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa +v


- Cac thu tuc FAR s dung phan oan ma khac vi phan oan ma cua
chng trnh goi. a ch tr ve ghi trong ngan xep la CS va IP (ma 16 bit)

Cac thu tuc c goi bang lenh CALL. Lenh nay gi a ch tr ve cua
chng trnh trong ngan xep va giam gia tr con tro ngan xep 2 n v neu thu tuc
la NEAR. Sau o nhay bc en v tr a xac nh. Con tro ngan xep se giam 4
neu thu tuc la FAR.

Mot thu tuc c s dung vi cac nh hng bien dch PROC...ENDP. e
co the tr ve chng trnh chnh, cuoi moi thu tuc can co them lenh RET. ieu
nay giup lay lai a ch a ghi vao trong ngan xep va tang gia tr con tro ngan xep
len 2 neu thu tuc la NEAR va len 4 neu thu tuc la FAR.

Cau truc chung cua mot thu tuc nh sau:

Ten PROC NEAR (hoac FAR)
;
; than thu tuc
;
;
RET
Ten ENDP


Chng trnh chnh thu tuc

TEMPO PROC
CALL TEMPO


CALL TEMPO
RET
TEMPO ENDP
Goi thu tuc



qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o0

III. S DUNG PTR VA LABEL:
a- Neu mot a ch v du [BX] xuat hien nh mot so hang, 8088 phai biet
chon x ly 1 hoac 2 byte tai a ch o, e cho ro rang can phai dung BYTE PTR
hoac WORD PTR

V du 1: MOV [BX],1

ngha la

MOV BYTE PTR [BX], 1
hoac
MOV WORD PTR [BX], 1

V du 2:
.DATA
....
DOLLARS DB 1Ah
CENTS DB 52h
....
.CODE
...
MOV AX,WORD PTR DOLLARS
; nap 521A ---> AX
b- Ngi ta con s dung nh hng bien dch LABEL e nh ngha lai mot
bien theo loai khac

Dang: ten LABEL loai

V du 1: WORD_ARR LABEL WORD
BYTE_ARR DB 100 DUP (0)

WORD_ARR nh ngha mot mang 50 t va BYTE_ARR nh ngha mot
mang 100 byte.

V du 2: .DATA
...
MONEY LABEL WORD
DOLLARS DB 1Ah
CENTS DB 52h

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o1

...
.CODE
...
MOV AX, MONEY
ieu nay giong nh
MOV AL, DOLLARS
MOV AH,CENTS
IV. VAI CHNG TRNH NG DUNG:
1. Hien so thap phan:

e hien mot so len man hnh chung ta can phai tao mot thu tuc. e lam
viec nay chung ta can phai phan tch so sau o hien tuan t tng ch so mot. Do
o chung ta can co 2 thu tuc:
Hien mot ch so;
Hien mot so.

a- Hien mot ch so:
Ham 2 ngat 21h dung e hien mot ky t len man hnh trong o ma ASCII
cua ky t c a vao DL, v vay chung ta can chuyen ch so sang ma ASCII
bang cach cong them vao so 48:

Ma ASCII Ky t
48 (30h) 0
49 (31h) 1
50 (32h) 2
...

Hienchuso PROC
PUSH AX ;giAX
PUSH DX
ADD AL, 48 ;Ch so 0...9
MOV DL, AL
MOV AH, 2 ;Hien ky t
INT 21h
POP DX
POP AX
RET
Hienchuso ENDP

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oz


b- Hien so:
Bay gi chung ta phan tch so cho hien. Chung ta s dung phep chia cho 10,
at so b chia vao AX (neu nh so nay tng ng vi mot t). Thng so se nam
trong AL va so d trong AH. Neu chia 1 so la t oi (double word) chung ta at
so o trong DX:AX va nh vay thng so se at trong AX va so d trong DX.
Nh vay,
8765 DIV 10 = 876 d 5
876 DIV 10 = 87 d 6
87 DIV 10 = 8 d 7
8 DIV 10 = 0 d 8

Chung ta thay cac so d la nhng ch so tao thanh ket qua bang so tuy vay
chung ta khong the cho hien cac so d ngay sau khi chia v nh vay se ao so
(5678), chung ta can phai nap cac ch so tuan t vao ngan xep sau o lay ra va
cho hien len man hnh th mi ap ng c yeu cau.

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o+

HienSoTP PROC
PUSH BX
PUSH CX
MOV BX, 10 ; so chia
; tach so va at nhng ch so theo th t phu hp trong ngan xep
MOV CX,0
Tach:
MOV DX,0
DIV BX
PUSH DX
INC CX
CMP AX, 0
JNZ Tach
Hienlen:
POP AX
CALL Hienchuso
LOOP Hienlen
POP CX
POP BX
RET
HienSoTP ENDP

c- Cac so am:

Thu tuc tren khong dung e hien cac so co dau. Nen nh rang
neu mot so co dau th bit ln nhat la bit dau, can phai kiem tra xem
neu so la so am th phai them dau '-' ong thi phai tnh bu 2 cho so
o

HienAscii PROC
PUSH AX
PUSH DX
MOV DL,AL
MOV AH, 2

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oo

INT 21h
POP DX
POP AX
RET
HienAscii ENDP

e kiem tra xem mot so am chung ta co the cong them so 0 roi
kiem tra bit ch bao SF e biet dau cua ket qua:

HienDauTP PROC
PUSH AX
PUSHF
ADD AX, 0
JNS Khongdau
PUSH AX
MOV AL,'-'
CALL HienAscii
POP AX
NEG AX
Khongdau:
CALL HienSoTP
POPF
POP AX
RET
HienDauTP ENDP

2. Hien so nh phan va thap luc phan

a- Hien so nh phan:

Trong mot so trng hp, chung ta lai muon bieu dien mot so nh
phan. Chung ta phai lay tng bit ra sau o cho hien len man hnh.


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa os


HienNP PROC
PUSHF
PUSH BX
PUSH CX
MOV BL,AL
MOV CX,8
Hienbit:
RCL BL,1
MOV AL,0
ADC AL,30h
CALL HienAscii
LOOP Hienbit
POP CX
POP BX
POPF
RET
HienNP ENDP

b- Hien Thap Luc Phan

Nhng ch so thap luc phan thng c s dung trong cac
chng trnh hp ng, trong thc te oi khi can thiet phai hien so di
dang nay.

HienTLP PROC
PUSH CX
PUSH DX
MOV DH,DL ; DL la mot ky t
MOV CX,4
SHR DL,CL
CALL HienChusoTLP

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oo

MOV DL,DH
AND DL,0FH
CALL HienChuSoTLP
POP DX
POP CX
RET
HienTLP ENDP
HienChuSoTLP PROC
PUSH DX
CMP DL,10
JAE ChuSoTLP
ADD DL,'0'
JMP HienChuSo
ChuSoTLP:
ADD DL,'A'-10
HienChuSo:
MOV AL, DL
CALL HienAscii
POP DX
RET
HienChuSoTLP ENDP

3. oc t ban phm

a- oc mot chuoi ky t:

e oc mot chuoi ky t, chung ta goi ngat 21h, dch vu 0Ah cho
phep at cac ky t go tren ban phm vao vung em DS:DX (buffer).
Ham nay ket thuc khi nhan RETURN, trong vung em gom cac thong
tin sau:

Byte Noi dung
0 Gia tr qui nh so toi a cac ky t co the oc, gia

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o?

tr nay phai c at trc khi goi ham
1 So ky t thc te oc vao (Khong tnh RETURN),
gia tr nay c at khi ham thc hien.
2 T v tr nay chung ta thay cac ky t oc vao.

Phai nh ngha mot vung d lieu (vung em) trong chng trnh
e gi cac ky t.

DocChuoi PROC
PUSH BX
PUSH CX
MOV BX,DX ; Offset cua vung em
PUSH [BX];gi 2byteau cua vungem
MOV [BX], AL
MOV AH, 0Ah
INT 21h
MOV AL,[BX+1]
POP CX
MOV [BX],CH
POP CX
POP BX
RET
DocChuoi ENDP

Neu chung ta tao ra mot chng trnh nho:

.MODEL SMALL
.STACK 100h
.DATA
VungDem DB 255 DUP (?) ; Vung gi ky t oc
Thongbao DB 'Ky t oc ', 13,10, '$'
.CODE
Main PROC
MOV AX,@Data

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oS

MOV DS, AX
MOV DX, OFFSET VungDem
MOV AL, 5 ; oc toi a 5 ky t
CALL DocChuoi
MOV AH,0
CALL HienSoTP
MOV DX, Offset Thongbao
CALL Hienchuoi
MOV AH,4Ch
INT 21h
Main ENDP

Hienchuoi PROC
PUSH AX
MOV AH, 09h
INT 21h
POP AX
RET
Hienchuoi ENDP
....
END MAIN

Bay gi, chung ta thc hien chng trnh, neu chung ta go vao
tren 5 ky t, ngat 21h gay ra 1 tieng BP; chung ta co the s dung
phm BACKSPACE e xoa 1 ky t trong qua trnh go.

b- oc mot so thap phan

e oc mot so thap phan, chung ta se s dung thu tuc
Docchuoi va sau o chuyen chuoi thanh so.

NB
So <-- 0
Lap Ch so <-- 1 ky t cua chuoi

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ov

So <-- So * 10 + ch so
NB <-- NB-1

Neu NB > 0 en Lap

V du, e chuyen '827', ta co
So <--0
Ch so <-- 8
So <-- So *10 + ch so = 0*10 +8 = 8
Ch so <-- 2
So <-- So * 10 + ch so = 8*10 +2 = 82
Ch so <-- 7
So <-- So * 10 + ch so = 82*10 +7 = 827

Giatri PROC
PUSH BX
PUSH CX
PUSH DX
PUSH SI
MOV AX,0
MOV BX,DX ; Bx ch en au vung em
MOV CX,[DX+1]
MOV SI, 2
Laplai:
MOV DX,10
MUL DX
MOV DL, [BX+SI]
INC SI
SUB DL,48
MOV DH,0
ADD AX, DX
LOOP Laplai
POP SI
POP DX

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa s0

POP CX
POP BX
RET
Giatri ENDP

Chu y: thu tuc tren khong kiem tra xem day ky t co trong
vung em co ung nh yeu cau khong? ngha la tat ca eu la ch so
co gia tr t 0 en 9. Ngoai ra trong thu tuc nay cung khong giai
quyet c xem gia tr cua so co vt qua 65.535: can phai kiem
tra xem phep nhan cuoi cung co tao ra nh khong? ngha la DX co
gia tr khac 0 )

DocSoTP PROC
PUSH DX
MOV AL, 5
MOV DX, Offset temp1
CALL DocChuoi
CALL Giatri
POP DX
RET
DocSoTP ENDP













qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa s1

CHNG 6: MOUN CHNG TRNH &
CAC KIEU NH V A CH

I. MOUN CHNG TRNH

oi vi cac chng trnh ln vi rat nhieu thu tuc th that tien
li neu chung ta at cac thu tuc trong cac file rieng biet. Co 2
nguyen nhan chu yeu e lam ieu o:

. Cac thu tuc co the c viet, hp dch va kiem tra rieng biet
bang cac lap trnh vien khac nhau

. Khi cac thu tuc c hp dch tach biet, chung co the s dung
lai cac ten cho cac bien hay cac nhan lenh. S d co c ieu o la
v trnh bien dch cho phep cac ten mang tnh chat cuc bo oi vi
moi file va no se khong xung ot oi vi cac ten giong nh vay
trong mot file khac.

1. nh hng bien dch extrn

Trong khi bien dch mot moun, trnh bien dch phai c
thong bao nhng ten c dung trong moun hien thi nhng a
khai bao trong cac mod8un khac, neu khong chung se b coi la cac
ten cha khai bao.

EXTRN ten:loai [,ten:loai]

bao cho trnh dch hp ng co mot ten ngoai tru ngha la co
mot ten a nh ngha trong mot moun khac. Loai tng ng phai
giong nh loai a nh ngha trong moun khac. Nhng loai nay la:
NEAR, FAR, PROC, BYTE, WORD...



qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa sz


2. nh hng bien dch public

Mot thu tuc hay bien phai c khai bao vi nh hng bien
dch PUBLIC neu no c s dung trong cac moun khac nhau

PUBLIC ten[, ten]

ay 'ten' la mot bien, mot nhan trong moun gi nh hng
bien dch PUBLIC

ng dung:

Tao mot chng trnh gom 2 thu tuc e noi chuoi, moi thu tuc
nam trong 1 tap tin khac nhau.


Tap tin th 1 co ten MAIN.ASM
Noi dung la

EXTRN NoiChuoi:NEAR
public ChuoiCuoi
.MODEL SMALL
.STACK 200H
.DATA
Chuoi1 DB 'Chao ',0
Chuoi2 DB 'cac ban ',0dh,0ah,'$',0
Chuoicuoi DB 50 DUP (?)


.CODE
ProgramStart PROC
mov ax,@data
mov ds,ax

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa s+

mov ax,OFFSET Chuoi1
mov bx,OFFSET Chuoi2
call NoiChuoi ; Noi 2 chuoi lai vi nhau
mov ah,9
mov dx,OFFSET ChuoiCuoi
int 21h ; Hien chuoi
mov ah,4ch
int 21h
ProgramStart ENDP
END ProgramStart


Tap tin th 2 co ten SUB.ASM co noi dung la

EXTRN ChuoiCuoi:byte
PUBLIC NoiChuoi
.MODEL SMALL
.CODE
NoiChuoi PROC
cld ; Chuoi tang dan
mov di,SEG ChuoiCuoi
mov es,di
mov di,OFFSET ChuoiCuoi ;ES:DI ch en ch
mov si,ax ; Chuoi au c chep i
LapChuoi1:
lodsb ; nhan trong chuoi 1 ky t
and al,al ; bang 0?
jz Chuoitiep ;ung th chuyen sang chuoi tiep
stosb ; lu 1 ky t vao chuoi
jmp LapChuoi1
Chuoitiep:
mov si,bx ; Chep chuoi th hai
LapChuoi2:
lodsb ; lay 1 ky t t chuoi 2

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa so

stosb ; gi ky t cua chuoi 2 vao
; (ke ca so 0 neu co)
and al,al ; bang 0?
jnz LapChuoi2 ; khong, nap ky t khac
ret ; xong
NoiChuoi ENDP
END

3. Lap th vien - tao v lenh (macro)

Thay v lien ket cac moun oi tng rieng biet e tao thanh
mot file .EXE , chung ta co the tao ra mot file th vien cua cac
moun oi tng cha thu tuc. Khi th vien c xac nh trong
qua trnh lien ket, chng trnh LINK lay ra bat ky thu tuc nao ma
chng trnh can th vien va tao ra file EXE.

Chung ta co dang

Ten MACRO {thong so [ ,thong so] }

Lenh

ENDM

Tao tap tin van ban ASCII la th vien

V du:

THUVIEN.MAC

Thoat MACRO
MOV AH,4Ch
INT 21h
ENDM

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ss

HienCh MACRO Chuoi1
MOV AH,9
LEA DX, Chuoi1
INT 21h
ENDM
Dockt MACRO
MOV AH,1
INT 21h
ENDM
HienKt MACRO
MOV AH,2
INT 21H
ENDM

ng dung: Hien so nh phan

EXTRN HienSoNP: NEAR
.MODEL SMALL
.STACK 100H
.DATA
Thongbao1 DB 'Nhap vao mot ky t: $'
Thongbao2 DB 'Ma ASCII so nh phan: $'
.CODE
INCLUDE THUVIEN.MAC
Batdau PROC
MOV AX, @Data
MOV DS, AX
HienCh Thongbao1
DocKt
HienCh Thongbao2
Call HienSoNP
Thoat
Batdau ENDP
END Batdau

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa so


II. CAC KIEU NH V A CH

Chung ta a biet, moi lenh eu co 2 phan chnh:

Phan phep toan thc hien (cong, tr, dch chuyen,...)
Phan d lieu lien quan. Cac d lieu nay nam trong cac thanh
ghi hoac trong bo nh.

e lay d lieu trong bo nh, bo vi x ly phai biet a ch va so
byte dung cho no.

Sau ay la cac loai nh v a ch khac nhau:

1. Kieu nh v a ch tc thi

Thanh ghi <--- Gia tr tc thi

Mot gia tr tc thi (co the la mot hang) se a vao noi dung
cua thanh ghi

V du: MOV AH,12

1- Kieu nh v a ch trc tiep:

Thanh ghi <-- bo nh(a ch trc tiep)

Noi dung cua mot vung nh c at ten se ghi vao trong
thanh ghi 8 bit hoac 16 bit phu thuoc vao loai d lieu va kch thc
thanh ghi, neu 2 yeu to nay khong bang nhau th trnh dch hp ng
se bao loi.

V du:

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa s?


Id1 DB 2 ; 1 byte
Id2 DW 5 ; 1 word = 2 byte
......
MOV AH,Id1
MOV BX,Id2

3. Kieu nh v a ch nen:

Thanh ghi <-- [BX] a ch nen da tren DS

Lay noi dung trong vung bo nh a ch DS:[BX] a vao
trong thanh ghi. Trong trng hp nay [BX] cho biet la lay noi dung
cua o nh co phan offset cua a ch la . Co the thc hien vi 8 hoac
16 bit.

V du: MOV BX,8
MOV AX, [BX]

AX <-- noi dung cua vung nh co a ch la DS:0008

4. Kieu nh v a ch ch so:

Thanh ghi <-- [SI] hoac [DI] a ch ch so

Kieu nh v a ch nay thng dung cho chuoi ky t hay cac
phan t cua mang. Thng nen la DS.

V du: MOV SI, 0
MOV AX, [SI]

AX <-- noi dung cua vung bo nh a ch DS:0

5- Kieu nh v a ch nen-ch so:

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa sS


Thanh ghi <-- [BX][SI] a ch nen-ch so da tren
DS
Thanh ghi <-- [BX][DI] a ch nen-ch so da tren DS

Loai a ch nay s dung BX va, SI hoac DI, nen la DS

Ghi chu: ngi ta co the dung qui c sau:
Thanh ghi <-- [BX+SI]

V du: MOV BX, 2
MOV SI, 5
MOV AH, [BX] [SI]

AH <-- noi dung cua o nh co a ch la DS:7 ngha la DS:5+2

6- Kieu nh v a ch nen-dch chuyen:

Thanh ghi <-- d lieu[BX] a ch nen da tren DS
Thanh ghi <-- d lieu[BX] a ch nen da tren SS

Loai nh v a ch nay la to hp cua cac loai tren.

Ghi chu: Co the s dung qui c sau:

Thanh ghi <-- [BX+d lieu]

V du:
Id1 DW 5 DUP (?)
....

MOV BX,2
MOV AX, Id1[BX]


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa sv

AX <-- phan t th 3 cua mang Id1

7. nh v a ch ch so-dch chuyen:

Thanh ghi <-- d lieu[SI]
Thanh ghi <-- d lieu[DI]

Loai nh v nay la to hp cua cac loai tren.

Ghi chu: Chung ta co the s dung qui c sau:

Thanh ghi <-- [SI+d lieu]

V du: .....
MOV SI,4
MOV AX, [SI+1]

AX <-- noi dung cua vung nh tai a ch DS:4+1
8- Kieu nh v a ch nen-ch so-dch chuyen

Thanh ghi <-- d lieu[BX][SI]
Thanh ghi <-- d lieu[BX][DI]
Thanh ghi <-- d lieu[BP][SI]
Thanh ghi <-- d lieu[BP][DI]

Loai nh v nay la to hp cua cac loai tren.

Ghi chu: Co the s dung ky hieu sau:

Thanh ghi <-- [BX+SI+d lieu]

V du: Id1 DB 15 DUP(?)
.....
MOV BX,5

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o0

MOV SI,4
MOV AX, Id1[BX][SI]
AX <-- phan t th 9 cua mang Id1
III. CHNG TRNH NG DUNG
TAP TIN - TH MUC
Chung ta tham khao mot so thu tuc lien quan en cac tap tin
bang cac goi ngat 21h:
Tao mot tap tin;
M mot tap tin;
ong mot tap tin;
oc mot tap tin;
Viet vao mot tap tin....

a - Can biet:
. Mot so thu tuc s dung mot t e ma hoa thuoc tnh cua tap
tin. Cac bit cua t nay co y ngha nh sau:
Bit Y ngha
0 Ch oc
1 An
2 He thong
5 Lu
. Mot so thu tuc s dung the (handle), o la mot so do he ieu
hanh MSDOS gan cho mot tap tin khi m hoac tao tap tin, con so
nay se can thiet trong qua trnh oc, viet, ong .v.v... tap tin.

Handle DW ?

Chu y: neu mot chng trnh x ly nhieu tap tin th phai co
nhieu the, moi the ng vi moi tap tin.

b - Tao, m, ong tap tin e co the ghi noi dung vao tap tin.
.MODEL SMALL
.STACK 100H

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o1

.DATA
TBAO1 DB 'a noi dung van ban vao: ',13,10,'$'
NOIDUNG DB 10000 DUP(?)
TENTT DB 'A:\THUCTAP.TXT',0
SO DW ?
LFCR DB 13,10,'$'
.CODE
INCLUDE THUVIEN.MAC
MAIN PROC
PRODATA
HIENCH TBAO1
XOR BX,BX
MOV AH,1 ;
LAP1:
INT 21H ;
CMP AL,1AH ; CTRL+Z
JE CONT
MOV NOIDUNG[BX],AL
CMP AL,13 ; ENTER
JNE NHAY1
MOV NOIDUNG[BX],0Dh
INC BX
MOV NOIDUNG[BX],0Ah
LEA DX,LFCR
MOV AH,9
INT 21H
MOV AH,1
NHAY1:
INC BX
JMP LAP1
TIEP: MOV SO,BX
MOV AH,3CH ;
LEA DX,TENTT ; TAO MOT TAP TIN
MOV CX,20H ; THUOC TI'NH

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oz

INT 21H ;
MOV BX,AX
LEA DX,TENTT ;
MOV AL,1 ;
MOV AH,3DH ; M TAP TIN
INT 21H ;
PUSH AX ; GHI THE E S DUNG SAU NAY
MOV BX,AX
MOV AH,40H ; GHI VAO TAP TIN
LEA DX,NOIDUNG; EM
MOV CX,SO ;SO BYTE OC
INT 21H ;
POP BX ;
MOV AH,3EH ;ONG TAP TIN
INT 21H
THOAT
MAIN ENDP
END MAIN

c- oc noi dung tap tin

EXTRN WRITE_HEX:NEAR
.MODEL SMALL
.STACK 100H
.DATA
NOIDUNG DB 10000 DUP(?)
TENTT DB 50 DUP (?)
CAUHOI DB 'NHAP DUONG DAN: '
LFCR DB 10,13,'$'
.CODE
INCLUDE THUVIEN.MAC
MAIN PROC
PRODATA
HIENCH CAUHOI

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o+

XOR BX,BX
MOV AH,1
LAP1:
INT 21H
MOV TENTT[BX],AL
INC BX
CMP AL,13
JNE LAP1
DEC BX
MOV TENTT[BX],'0'
HIENCH LFCR
LEA DX,TENTT
MOV AL,2
MOV AH,3DH ; THE
INT 21H ;
PUSH AX ; LU THE
MOV BX,AX ; DCH VU 3F : BX <-- THE
MOV AH,3FH ; OC TAP TIN
LEA DX,NOIDUNG;
MOV CX,0FFFFH ; SO BYTE
INT 21H ;
POP BX
PUSH AX ;
CALL DONG
XOR BX,BX
POP CX
TIEP1:
MOV DL,NOIDUNG[BX]
CALL HIENSOTLP
CALL TRONG
INC BX
LOOP TIEP1
THOAT
MAIN ENDP

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oo

DONG PROC
MOV AH,3Eh
INT 21h
RET
DONG ENDP
TRONG PROC
PUSH DX
PUSH AX
MOV DL,' '
MOV AH,2
INT 21H
POP AX
POP DX
RET
TRONG ENDP
END MAIN

d-OI TEN, XOA TAP TIN

oi ten tap tin: Ham 56h - Ngat 21h
Xoa tap tin: Ham 41h - Ngat 21h

.MODEL SMALL
.STACK 100H
.DATA
VUNGDEM DB 20
LOAI DB ?
TENTT DB 50 DUP (0),0,'$'
DUONG DB '[A:] <DUONG> TEN TAP TIN[.EXT] $'
LOI DB 'KHONG THE XOA TAP TIN NAY$'
.CODE
INCLUDE THUVIEN.MAC

MAIN PROC

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa os

PRODATA
HIENCH DUONG
MOV AH,0Ah
LEA DX,VUNGDEM
INT 21h
XOR BX,BX
MOV BL,LOAI
ADD BX,OFFSET TENTT
MOV BYTE PTR [BX],0
LEA DX,TENTT
MOV AH,41h
INT 21h
JNC RA
HIENCH LOI
RA: THOAT
MAIN ENDP
END MAIN

e- THAY OI THUOC TNH - CHEP TAP TIN:

THAY OI THUOC TNH TAP TIN: Ham 43h - Ngat 21h

CHEP TA P TIN
.MODEL SMALL
.DATA
BUFFER DB 14
TYPE1 DB 0
FILE1 DB 12 DUP(00),0
FILE2 DB 8 DUP(00),".TXT",0
HANDLE1 DW 0
HANDLE2 DW 0
THONGBAO DB 'VAO TEN PHAI COPY : $'
DATA1 DB 10000 DUP(?)


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oo

.STACK 100H
.CODE
INCLUDE THUVIEN.MAC
MAIN PROC
PRODATA
HIENCH THONGBAO
MOV AH,0AH
LEA DX,BUFFER
INT 21H
MOV [FILE1+12],0
MOV CX,08
LEA SI,FILE1
MOV DI, SEG FILE2
MOV ES, DI
LEA DI,FILE2
REP MOVSB
LEA DX,FILE1
MOV AX,3D02H
INT 21H
MOV HANDLE1,AX
MOV DX,OFFSET FILE2
MOV AH,3CH
MOV CX,0
INT 21H
MOV HANDLE2,AX
MOV AH,3FH
MOV CX,10000
MOV DX,OFFSET DATA1
MOV BX,HANDLE1
INT 21H
MOV CX,AX
MOV AH,40H
MOV BX,HANDLE2
INT 21H

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa o?

MOV AH,3EH
MOV BX,HANDLE1
INT 21H
MOV BX,HANDLE2
INT 21H
THOAT
MAIN ENDP
END MAIN
f. CAC TH MUC:
Dch vu 39h cua ngat 21h s dung e tao th muc mi
(MKDIR)
Dch vu 3Ah cua ngat 21h s dung e xoa th muc (RMDIR)
Dch vu 3Bh cua ngat 21h s dung e chuyen th muc
(CHDIR)
IV. TAO TAP TIN .COM

Nhng chng trnh trong o d lieu va lenh eu nam trong
cung mot phan oan eu la nhng chng trnh COM. ieu nay co
ngha la dung lng cua chng trnh co gii han la 64KByte. Ngay
t khi thc hien chng trnh loai nay, MSDOS a vao bo nh
toan bo tap tin nay va at gia tr ban au cho cac thanh ghi CS, DS,
SS, ES nh nhau. Thanh ghi SP (Stack Pointer) c at la FFFEh.
Thanh ghi IP c at la 0100h v moi chng trnh at au bo
nh mot vung100h byte (PSP), ieu nay giai thch v sao lenh th
nhat cua chng trnh (iem vao) phai at au cua chng trnh
va chung ta phai s dung nh hng bien dch ORG 100h. V vay
chung ta co hnh anh cua bo nh nh sau:

Offset PSP

100h JMP Batdau

D lieu

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa oS


IP ---> Batdau:



SP=FFFE Ngan xep

1- Dang cua mot chng trnh COM

.MODEL SMALL
.CODE
ORG 100h
BATDAU:
JMP CHINH
; at d lieu ay
CHINH PROC
; Cac lenh
MOV AH,4Ch
INT 21h
CHINH ENDP
; Cac thu tuc khac
END BATDAU

V du:

Tap tin KIEMTRA.ASM

.MODEL SMALL
.CODE
ORG 100h
BATDAU:
JMP CHINH
TBAO DB 'Chao cac ban! $'

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ov

CHINH PROC
LEA DX,TBAO
MOV AH,09
INT 21h
MOV AH,4Ch
INT 21h
CHINH ENDP
END BATDAU

Hp dch chng trnh nay:

MASM KIEMTRA;
LINK KIEMTRA;

e chuyen tap tin EXE sang COM s dung EXE2BIN
EXE2BIN KIEMTRA KIEMTRA.COM
2- ng dung tap tin .com

a- ng dung 1:
Xem so a cng, so au t, so t tru, so cung tren ranh

.MODEL SMALL
.CODE
ORG 100h
BATDAU:

JMP DIACUNG

SODIA DB 10,13,'So a cng: $'
SODAU DB 10,13,'So au t : $'
SOTUTRU DB 10,13,'So t tru : $'
SOCUNG DB 10,13,'So cung : $'


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?0

DIACUNG PROC
MOV AH,8
MOV DL,80H
INT 13H
PUSH CX
PUSH CX
PUSH DX
PUSH DX
LEA DX,SODIA
CALL HIENCHUOI
POP DX
AND DX,00FFH
CALL HIENSOTP
LEA DX,SODAU
CALL HIENCHUOI
POP DX
AND DX,0FF00H
MOV DL,DH
MOV DH,00
ADD DL,1
CALL HIENSOTP
LEA DX,SOTUTRU
CALL HIENCHUOI
POP CX
AND CX,1111111111000000B
XCHG CH,CL
MOV DX,CX
MOV CX,2
ROL DH,CL
ADD DL,2
CALL HIENSOTP
LEA DX,SOCUNG
CALL HIENCHUOI
POP CX

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?1

AND CX,0000000000111111B
MOV DX,CX
CALL HIENSOTP
MOV AH,4CH
INT 21H
DIACUNG ENDP
; Nhng thu tuc khac

END BATDAU


b - ng dung 2: oc noi dung cua mot cung

DOS bat au oc t cung 1, t tru 0, mat 1 goi la cung 0
moi ranh co 17 cung
* Moi tap tin chiem nhieu nhom cung do vay phai co mot bang
quan ly tap tin -> FAT File Allocation Table co 16 BIT ->
2
16
=65536 nhom cung

* Vi a mem:
Mat 0, ranh 0, cung 1 -> lu phan khi ong DOS BOOT
RECORD (DBR)

* Vi a cng:
Bat au la phan MASTER BOOT RECORD (MBR)
PARTITION
sau o mat 1, ranh 0, cung 1 -> lu phan khi ong DOS BOOT
RECORD (DBR)

Tap tin DIACUNG.ASM

.MODEL SMALL
.CODE
ORG 100h

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?z

BATDAU:
JMP DOCCUNG
VUNGDEM DB 512 DUP (?)
DB '$'
THONGBAO DB 'oc tot ',13,10,'$'
BAOLOI DB 'Khong the oc',13,10,'$'
TRONG DB ' $'
GACH DB ' - $'
DEM DW ?
CHU DW ?
HANG DW ?
LFCR DB 13,10,'$'

DOCCUNG PROC
MOV DS,AX
XOR AX,AX
MOV AH,2
MOV AL,1 ; 1 Cung
MOV DL,1 ; a mem B
MOV DH,1 ; au t 1
MOV CH,0 ; ranh 0
MOV CL,9 ; cung 9
LEA BX,VUNGDEM
INT 13H
JNC TOT
LEA DX,BAOLOI
MOV AH,9
INT 21H
JMP RA
TOT:
LEA DX,THONGBAO
MOV AH,9
INT 21H
RA:

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?+

MOV CX,300
MOV DEM,0
MOV CHU,0
MOV HANG,0
LAP9:
MOV BX,DEM
MOV DX,WORD PTR VUNGDEM[BX]
CALL HIENSOTLP
LEA DX,TRONG
CALL HIENCHUOI
INC DEM
INC CHU
INC HANG
CMP CHU,10
JNE TIEP1
MOV CHU,0
CMP HANG,10
JA TIEP1
LEA DX,GACH
CALL HIENCHUOI
TIEP1:
CMP HANG,20
JNE TIEP2
MOV HANG,0
LEA DX,LFCR
CALL HIENCHUOI
TIEP2:
LOOP LAP9
CALL THOAT
DOCCUNG ENDP

; Cac thu tuc khac

END BATDAU

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?o



c - ng dung 3:

Ghi 1 cung

.MODEL SMALL
.CODE
ORG 100h
BATDAU:
JMP GHICUNG

VUNGDEM DB 512 DUP (00)
DB '$'
THONGBAO DB 13,10,'oc tot ',13,10,'$'
BAOLOI DB 13,10,'Khong the oc ',13,10,'$'
NHAP DB 'Go vao ban phm :'
LFCR DB 13,10,'$'
DEM DW ?

GHICUNG PROC
LEA DX,NHAP
CALL HIENCHUOI
MOV DEM,0
MOV AH,1
LAP9:
INT 21H
CMP AL,1AH ; CTRL+Z
JE TIEP1
MOV BX,DEM
MOV VUNGDEM[BX],AL
INC DEM
JNE LAP9
TIEP1:

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?s

XOR AX,AX
MOV AH,3
MOV AL,1 ; 1 Cung
MOV DL,1 ; a mem B
MOV DH,1 ; au t 1
MOV CH,0 ; Ranh 0
MOV CL,9 ; Cung 9
LEA BX,VUNGDEM
INT 13H
JNC TOT
LEA DX,BAOLOI
CALL HIENCHUOI
JMP THOATRA
TOT:
LEA DX,THONGBAO
CALL HIENCHUOI
THOATRA:
CALL THOAT
GHICUNG ENDP

;Cac thu tuc khac

END BATDAU













qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?o

CHNG 7: S DUNG PHAN OAN TOAN PHAN

Phng phap nh ngha phan oan n gian hoa ma chung ta van
thng s dung t trc en nay a ap dung c hau het cac muc tieu.
Trong phan nay chung ta xem xet cach nh ngha phan oan ton phan. Di
ay la nguyen nhan chnh e s dung phng phap nay:

. Vi cac trnh bien dch phien ban 5.0 tr ve trc chung ta ch co the
s dung phng phap nh ngha phan oan toan phan.

. Vi phng phap nh ngha phan oan toan phan, lap trnh vien co
the ieu khien c th t cac phan oan , viec ket hp chung vi nhau va s
lien ket gia chung trong bo nh

I- S DUNG CAC NH HNG BIEN DCH
1. Segment

Ten SEGMENT lien ket ket hp 'lp'
;
;
Ten ENDS

V du:
D_SEG SEGMENT

; cac d lieu

D_SEG ENDS

a - Loai lien ket (align):

PARA Phan oan bat au t phan oan trc o.
BYTE Phan oan bat au t byte trc o.

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ??

WORD Phan oan bat au t t trc o.
PAGE Phan oan bat au t trang trc o.

Mac nh ngi ta s dung loai PARA

V du:

SEG1 SEGMENT PARA
DB 11h DUP (1)
SEG1 ENDS

SEG2 SEGMENT PARA
DB 10H DUP (2)
SEG2 ENDS

1010:0000 01 01 01 01 01 01 - 01 01 01 01 01
1010:0010 01 00 00 00 00 00 - 00 00 00 00 00
1010:0020 02 02 02 02 02 02 - 02 02 02 02 02

SEG1 SEGMENT PARA
DB 11h DUP (1)
SEG1 ENDS
SEG2 SEGMENT BYTE
DB 10H DUP (2)
SEG2 ENDS

1010:0000 01 01 01 01 01 01 - 01 01 01 01 01
1010:0010 01 02 02 02 02 02 - 02 02 02 02 02
1010:0020 02 00 00 00 00 00 - 00 00 00 00 00

b - Loai ket hp (combine):

PUBLIC:Nhng phan oan co cung ten eu gom lai trong cung mot
khoi nh ke tiep nhau va thong nhat.


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?S

COMMON: Nhng phan oan co cung ten se gom lai tai mot v tr
bo nh va xep chong len nhau.

STACK: Tac dung gan nh PUBLIC ngoai tr tat ca cac a ch
nhay bc (offset) cua cac lenh va cac d lieu c tnh
tng oi so vi thanh ghi SS. SP c at ban au tro
en cuoi phan oan.

AT paragraph: Phan oan c bat au t mot phan oan xac nh

Thng ngi ta s dung loai PUBLIC e gom cac phan oan ma
co cung ten trong nhng moun khac nhau e tao ra mot phan oan
duy nhat. ieu nay cung co ngha la chung ta co nhng thu tuc
NEAR trong chng trnh.

c - Loai lp (class):

Loai lp chung ta nh ngha ch ra th t v tr cua cac phan oan
trong bo nh. Chung ta can at loai nay vao trong dau nhay ( ' ).

V du:

Neu chung ta nh ngha THUNHAT.ASM:

C_SEG SEGMENT 'CODE'
; thu tuc chnh
C_SEG ENDS

Va trong THUHAI.ASM

C1_SEG SEGMENT 'CODE'
; cac thu tuc khac
C1_SEG ENDS


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa ?v


Sau khi thc hien lenh

LINK THUNHAT+THUHAI
C1_SEG se nam sau C_SEG trong bo nh.

2. nh hng bien dch assume

Khi mot chng trnh c hp dch, trnh hp dch phai biet phan
oan nao la phan oan ma, phan oan nao la phan oan d lieu, phan oan
nao la phan oan ngan xep. Muc tieu cua nh hng bien dch ASSUME la
lien ket cac thanh ghi CS, DS, SS va ngay ca ES vao cac phan oan a biet.

II - DANG CUA CHNG TRNH .EXE S DUNG TRONG CA C
PHNG PHAP CU:
S_SEG SEGMENT STACK
DB 100h
S_SEG ENDS
D_SEG SEGMENT
; d lieu
D_SEG ENDS
C_SEG SEGMENT
ASSUME CS:C_SEG, SS:S_SEG, DS: D_SEG
MAIN PROC
;Xac nh ban au DS
MOV AX, D_SEG
MOV DS, AX
; cac lenh khac
MAIN ENDP
; cac thu tuc khac
C_SEG ENDS
END MAIN
Mot v du chng trnh EXE

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa S0


oi ra ch HOA:

* Tap tin CTR11.ASM

EXTRN CONVERS:FAR
S_SEG SEGMENT STACK
DB 100 DUP (0)
S_SEG ENDS

D_SEG SEGMENT
MSG DB 'Nhap vao mot ch cai thng: $'
D_SEG ENDS

C_SEG SEGMENT

ASSUME CS:C_SEG, DS: D_SEG, SS: S_SEG

MAIN PROC
MOV AX, D_SEG
MOV DS, AX
MOV AH, 09
LEA DX, MSG
INT 21H
MOV AH, 1
INT 21H
CALL CONVERS
MOV AH,4CH
INT 21H
MAIN ENDP
C_SEG ENDS
END MAIN



qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa S1

Tap tin CTR21.ASM

PUBLIC CONVERS

D_SEG SEGMENT
MSG DB 0DH,0AH, 'Ch HOA la: '
CARAC DB -20h,' $'
D_SEG ENDS

C_SEG SEGMENT
ASSUME CS: C_SEG, DS: D_SEG
CONVERT PROC FAR
PUSH DS
PUSH DX
MOV DX, D_SEG
MOV DS, DX
ADD CARAC, AL
MOV AH, 09
LEA DX, MSG
INT 21H
POP DX
POP DS
RET
CONVERS ENDP
C_SEG ENDS
END

Co the toi u hoa chng trnh tren:

* CTR12.ASM

EXTRN CONVERS:NEAR
S_SEG SEGMENT STACK
DB 100 DUP (0)

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa Sz

S_SEG ENDS
D_SEG SEGMENT BYTE PUBLIC 'DATA'
MSG DB 'Nhap vao mot ch cai thng: $'
D_SEG ENDS
C_SEG SEGMENT BYTE PUBLIC 'CODE'
ASSUME CS:C_SEG, DS: D_SEG, SS: S_SEG
MAIN PROC
MOV AX, D_SEG
MOV DS, AX
MOV AH, 09
LEA DX, MSG
INT 21H
MOV AH, 1
INT 21H
CALL CONVERS
MOV AH,4CH
INT 21H
MAIN ENDP
C_SEG ENDS
END MAIN

Tap tin CTR22.ASM

PUBLIC CONVERS
D_SEG SEGMENT BYTE PUBLIC 'DATA'
MSG DB 0DH,0AH, 'Ch HOA la : '
CARAC DB -20h,' $'
D_SEG ENDS
C_SEG SEGMENT BYTE PUBLIC 'CODE'
ASSUME CS: C_SEG, DS: D_SEG
CONVERT PROC NEAR
PUSH DX
ADD CARAC, AL
MOV AH, 09

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa S+

LEA DX, MSG
INT 21H
POP DX
RET
CONVERS ENDP
C_SEG ENDS
END

MASM CTR21;
MASM CTR22;
LINK CTR21+CTR22

III- V DU CHNG TRNH COM

MyProg SEGMENT
Org 100h
ASSUME CS:MyProg, DS:MyData
Start:
JMP Main
Main PROC
MOV AH,9
LEA DX,CS:MSG
INT 21h
INT 20h
Main ENDP
MyProg ENDS
MyData SEGMENT
MSG DB 'Chao cac ban, hen gap lai $'
MyData ENDS
END Start


MASM CTCUOI;

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa So

LINK CTCUOI;
EXE2BIN CTCUOI CTCUOI.COM



qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa Ss

CHNG 8: CHNG TRNH NG DUNG

I. IN NOI DUNG VAN BAN RA MAY IN:
1. S dung ham 40h ngat 21h (DOS) e thc hien

.MODEL SMALL
.STACK 100h
.DATA
HelloMessage DB 'Hello, world',13,10,12 ; 12 Form Feed
HELLO_MESSAGE_LENGTH EQU $ - HelloMessage
.CODE
mov ax,@data
mov ds,ax ;set DS to point to the data segment
mov ah,40h ;DOS write to device function #
mov bx,4 ;printer handle
mov cx,HELLO_MESSAGE_LENGTH ;number of characters to print
mov dx,OFFSET HelloMessage ;string to print
int 21h ;print "Hello, world"
mov ah,4ch ;DOS terminate program function #
int 21h ;terminate the program
END

2. S dung ham 01h ngat 17h (ROMBIOS)

.model small
.stack 100h

.data
printe equ 17h
prin_first equ 0

.code
printe_char proc
mov ax,@data
mov ds,ax
mov dx,prin_first
mov ah,01
int printe
mov cx,0ffh
conti:
mov al,41h
call pri
dec cx
cmp cx,0
jnz conti
mov al,0dh
call pri
mov ah,4ch

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa So

int 21h
printe_char endp
pri proc
mov ah,0
int printe
ret
pri endp
end printe_char

II. EM SO T TRONG MOT TAP TIN

;
; Program to count the number of words in a file. Words are
; delimited by whitespace, which consists of spaces, tabs,
; carriage returns, and linefeeds.
.MODEL SMALL ;code and data each fit in 64K
.STACK 200h ;512-byte stack
.DATA
Count DW 0 ;used to count words
InWhitespace DB ? ;set to 1 when the last
; character read was whitespace
TempChar DB ? ;temporary storage used by
; GetNextCharacter
Result DB 'So tu la: ', 5 DUP (?)
;string printed to report the count
CountInsertEnd DB 0dh,0ah,'$'
;DOS fn #9 expects strings to end with
; a dollar sign
.CODE
ProgramStart:
mov ax,@data
mov ds,ax ;point DS to the .DATA segment
mov [InWhitespace],1 ;assume we're in whitespace, since
; the first non-whitespace we'll find
; will mark the start of a word
CountLoop:
call GetNextCharacter ;get next character to check
jz CountDone ;...if any
call IsCharacterWhitespace ;is it whitespace?
jz IsWhitespace ;yes
cmp [InWhitespace],0 ;character is not
; whitespace-are we currently in
; whitespace?
jz CountLoop ;we're not in whitespace, and the
; character isn't whitespace, so
; we're done with this character
inc [Count] ;we are in whitespace, and the
; character is not whitespace, so
; we just found the start of a new word
mov [InWhitespace],0 ;mark that we're no longer
;in whitespace
jmp CountLoop ;do the next character
IsWhitespace:

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa S?

mov [InWhitespace],1 ;mark that we're in whitespace
jmp CountLoop ;do the next character
;
; We're done counting--report the results.
CountDone:
mov ax,[Count] ;number to convert to a string
mov bx,OFFSET CountInsertEnd-1 ;point to the end of
; the string to put the number in
mov cx,5 ;number of digits to convert
call ConvertNumberToString ;make the number a string
mov bx,OFFSET Result ;point to result string
call PrintString ;print the count
mov ah,4ch ;DOS terminate program fn #
int 21h ;end the program
;
; Subroutine to get the next character from the standard input.
;
; Input: None
;
; Output:
; AL = character, if one was available
; Z flag = 0 (NZ) if character available,
; = 1 (Z) if end of file reached
;
; Registers destroyed: AH, BX, CX, DX
;
GetNextCharacter PROC
mov ah,3fh ;DOS read from file fn #
mov bx,0 ;standard input handle
mov cx,1 ;read one character
mov dx,OFFSET TempChar ;put the character in TempChar
int 21h ;get the next character
jc NoCharacterRead ;if DOS reports an error, then treat
; it as the end of the file
cmp [TempChar],1ah ;was it Control-Z?
; (marks end of some files)
jne NotControlZ ;no
NoCharacterRead:
sub ax,ax ;mark no character read
NotControlZ:
and ax,ax ;set Z flag to reflect whether a
; character was read (NZ), or the
; end of the file was reached (Z).
; Note that DOS fn #3fh sets AX to
; the number of characters read
mov al,[TempChar] ;return the character read
ret ;done
GetNextCharacter ENDP
;
; Subroutine to report whether a given character is whitespace.
;
; Input:
; AL = character to check
;

qaqeo 7aa 1ce 7hco 7ot 1q
; Output:

7cp qa SS

; Z flag = 0 (NZ) if character is not whitespace
; = 1 (Z) if character is whitespace
;
; Registers destroyed: none
;
IsCharacterWhitespace PROC
cmp al,' ' ;is it a space?
jz EndIsCharacterWhitespace ;if so, it's whitespace
cmp al,09h ;is it a tab?
jz EndIsCharacterWhitespace ;if so, it's whitespace
cmp al,0dh ;is it a carriage return?
jz EndIsCharacterWhitespace ;if so, it's whitespace
cmp al,0ah ;is it a linefeed? If so,
; it's whitespace, so return Z;
; if not, it's not whitespace,
; so return NZ as set by cmp
EndIsCharacterWhitespace:
ret
IsCharacterWhitespace ENDP
;
; Subroutine to convert a binary number to a text string.
;
; Input:
; AX = number to convert
; DS:BX = pointer to end of string to store text in
; CX = number of digits to convert
;
; Output: None
;
; Registers destroyed: AX, BX, CX, DX, SI
;
ConvertNumberToString PROC
mov si,10 ;used to divide by 10
ConvertLoop:
sub dx,dx ;convert AX to doubleword in DX:AX
div si ;divide number by 10. Remainder is in
; DX--this is a one-digit decimal
; number. Number/10 is in AX
add dl,'0' ;convert remainder to a text character
mov [bx],dl ;put this digit in the string
dec bx ;point to the location for the
; next most-significant digit
loop ConvertLoop ;do the next digit, if any
ret
ConvertNumberToString ENDP
;
; Subroutine to print a string on the display.
;
; Input:
; DS:BX = pointer to string to print
;
; Output: None
;
; Registers destroyed: None

qaqeo 7aa 1ce 7hco 7ot 1q
;

7cp qa Sv

;
;
PrintString PROC
push ax
push dx ;reserve registers in this subroutine
mov ah,9 ;DOS print string function #
mov dx,bx ;point DS:DX to the string to print
int 21h ;invoke DOS to print the string
pop dx ;restore registers we changed
pop ax
ret
PrintString ENDP
END ProgramStart

III- OI NGC CHUOI

.MODEL SMALL
.STACK 100h
.DATA
MAXIMUM_STRING_LENGTH EQU 1000
StringToReverse DB MAXIMUM_STRING_LENGTH DUP(?)
ReverseString DB MAXIMUM_STRING_LENGTH DUP(?)
.CODE
mov ax,@data
mov ds,ax ;set DS to point to the data segment
mov ah,3fh ;DOS read from handle function #
mov bx,0 ;standard input handle
mov cx,MAXIMUM_STRING_LENGTH ;read up to maximum
;number of characters
mov dx,OFFSET StringToReverse ;store the string here
int 21h ;get the string
and ax,ax ;were any characters read?
jz Done ;no, so you're done
mov cx,ax ;put string length in CX, where
; you can use it as a counter
push cx ;save the string length
mov bx,OFFSET StringToReverse
mov si,OFFSET ReverseString
add si,cx
dec si ;point to the end of the
; reverse string buffer
ReverseLoop:
mov al,[bx] ;get the next character
mov [si],al ;store the characters in reverse order
inc bx ;point to next character
dec si ;point to previous location in reverse buffer
loop ReverseLoop ;move next character, if any
pop cx ;get back the string length
mov ah,40h ;DOS write from handle function #
mov bx,1 ;standard output handle
mov dx,OFFSET ReverseString ;print this string
int 21h ;print the reversed string
Done:

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa v0

mov ah,4ch ;DOS terminate program function #
int 21h ;terminate the program
END




IV. CHNG TRNH HIEN TH THI GIAN

Chng trnh bao gom cac bc sau ay:

1- Lay thi gian
2- oi gi, phut, giay sang dang ch so ASCII
3- Hien th cac ch so ASCII

Tap tin th 1: PGM1_1.ASM

EXTRN GET_TIME:NEAR
.MODEL SMALL
.STACK 100h
.DATA
TIME_BUF DB '00:00:00$' ; bo em thi gian
;hr:min:sec
.CODE
MAIN PROC
MOV AX,@DATA
MOV DX,AX ; khi tao DS
;lay va hien th thi gian
LEA BX,TIME_BUF
CALL GET_TIME ;a thi gian
;hien tai va o DX
LEA DX,TIME_BUF
MOV AH,09H
INT 21H

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa v1

;tr ve DOS
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN



Tap tin th 2: PGM1_2.ASM

PUBLIC GET_TIME
.MODEL SMALL
.CODE
GET_TIME PROC
;lay thi gian trong ngay va cha cac ch so ASCII ;trong bo
em thi gian
MOV AH,2CH
INT 21H
;oi gi sang dang ASCII va lu tr
MOV AL,CH
CALL CONVERT
MOV [BX],AX
;oi phut sang dang ASCII va lu tr
MOV AL,CL
CALL CONVERT
MOV [BX+3],AX
;oi giay sang dang ASCII va lu tr
MOV AL,CH
CALL CONVERT
MOV [BX+6],AX
RET
GET_TIME ENDP
;
;

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa vz

CONVERT PROC
;
; oi so kieu byte (0-59) sang cac ch so ASCII
; vao: AL = so
; ra:AX = cac ch so ASCII, AL = ch so hang chuc
; AH = ch so hang n v
;
MOV AH,0
MOV DL,10
DIV DL
OR AX, 0303H ; oi sang dang ASCII
RET
CONVERT END
END

Chng trnh hien th thi gian va ket thuc

* e tao chng trnh thi gian thu v hn, chung ta cho hien th thi
gian va cap nhat lai sau moi giay

Thu tuc SETUP_INT trong chng trnh PGM2_2.ASM cat vect ngat
cu va thiet lap vect ngat mi. No nhan so hieu ngat trong AL, bo em e cat
vect cu tai DS:DI va bo em cha vect ngat mi tai DS:SI. Bang viec hoan
oi hai bo em, SET_INT co the c dung e phuc hoi lai vect cu.

Tap tin PGM2_2.ASM

PUBLIC SETUP_INT
.MODEL SMALL
.CODE
SETUP_INT PROC
;cat vect cu
MOV AH,35H
INT 21H ;ES:BX=vect ngat

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa v+

MOV [DI],BX ; cat offset
MOV [DI+2],ES ;cat a ch phan oan
; thiet lap vect mi
MOV DX,[SI] ;DX cha offset
PUSH DS ;cat DS
MOV DS,[SI+2];DS cha so hieu oan
MOV AH,25H
INT 21H
POP DS ; phuc hoi DS
RET
SETUP_INT END
END

ieu khien con tro:

Moi khi hien th thi gian bang ham 9 ngat 21h, con tro b dch i.
Lan hien th sau se xuat hien tai mot v tr khac tren man hnh. Nh vay
e thay thi gian cap nhat lai cung mot v tr tren man hnh, chung ta
phai phuc hoi con tro tai v tr nguyen thuy cua no trc khi hien th thi
gian. Muon thc hien ieu nay, trc het chung ta phai xac nh v tr
hien tai cua con tro, tiep o sau moi thao tac in chuoi chung ta lai
chuyen con tro tro ve v tr ban au cua no.

Chung ta s dung cac ham 3 va 2 cua ngat 10h e cat v tr cua
con tro va di chuyen con tro en v tr ban au cua no sau moi thao tac in
chuoi.

Thu tuc ngat:

Khi mot thu tuc ngat c kch hoat, khong the xem thanh ghi DS
cha a ch phan oan d lieu cua chng trnh. V the neu muon s
dung bat c bien nao, phai khi tao thanh ghi DS. Thanh ghi DS phai
c phuc hoi lai trc khi ket thuc trnh phuc vu ngat vi lenh IRET
(Interrupt Return)

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa vo


PGM2_1.ASM cha thu tuc MAIN va thu tuc ngat TIME_INT.

Cac bc cua thu tuc MAIN gom:
Cat v tr con tro hien tai
Thiet lap vect ngat cho TIME_INT
i vao 1 phm
phuc hoi lai ngat cu va ket thuc

Thc hien bc th 2, chung ta s dung toan t gia OFFSET va
SEG e lay offset va a ch phan oan cua thu tuc TIEME_INT, vect
nay sau o c lu vao trong bo em NEW_VEC. Thu tuc SETUP_INT
c goi e thiet lap vect cho ngat nhp thi gian 1Ch. Ngat 16h, ham 0
c dung trong bc 3 e vao 1 phm. Thu tuc SETUP_INT lai c
goi mot lan na trong bc 4, lan nay SI tro en vect cu va DI tro en
vect cua thu tuc TIME_INT.

Cac bc cua thu tuc TIME_INT gom

(1) Thiet lap DS
(2) Lay thi gian mi
(3) Hien th thi gian
(4) phuc hoi v tr con tro
(5) Phuc hoi DS

Chng trnh c tien hanh nh sau: sau khi thiet lap con tro va
cac vect ngat, thu tuc MAIN i 1 phm nhan. Khi ay thu tuc ngat
TIME_INT cap nhat thi gian mi moi khi co xung nhp cua mach thi
gian. Sau khi mot phm c nhan, vect cu c phuc hoi va chng
trnh ket thuc.


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa vs

PGM2_1.ASM

EXTRN GET_TIME:NEAR, SETUP_INT:NEAR
.MODEL SMALL
.STACK 100H
.DATA
TIME_BUF DB '00:00:00$'
CURSOR_POS DW ?
NEW_VEC DW ?,?
OLD_VEC DW ?,?
;
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX ;Khi tao DS
;cat v tr con tro
MOV AH,3
MOV BH,0 ;trang 0
INT 10H
MOV CURSOR_POS,DX; cat v tr con tro
;thiet lap thu tuc ngat bang cach
; a segment:offset cua TIME_INT vao NEW_VEC
MOV NEW_VEC, OFFSET TIME_INT
MOV NEW_VEC+2,SEG TIME_INT
LEA DI, OLD_VEC ;DI tro en bo em vect
LEA SI, NEW_VEC ;SI tro en vect mi
MOV AL,1CH ; ngat thi gian
CALL SETUP_INT ; thiet lap vect mi
; oc ban phm
MOV AH,0
INT 16H
;phuc hoi vect ngat cu
LEA DI,NE_VEC
LEA SI,OLD_VEC

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa vo

MOV AL,1CH
CALL SETUP_INT
;tr ve DOS
MOV AH,4CH
INT 21H
MAIN ENDP
;
TIME_INT PROC
;thu tuc ngat
;kch hoat bi mach thi gian
PUSH DS; cat gia tr tc thi cua DS
MOV AX,@DATA
MOV DS,AX ;DS tro en oan d lieu
;lay thi gian mi
LEA BX, TIME_BUF ; BX tro en bo em
;thi gian
CALL GET_TIME ; cat thi gian trong boem
;hien th thi gian
LEA DX,TIME_BUF
MOV AH,09H
INT 21H
;phuc hoi v tr con tro
MOV AH,2
MOV BH,0
MOV DX,CURSOR_POS
INT 10H
IRET
TIME_INT ENDP
;
END MAIN

Lenh LINK gom cac moun PGM2_1.ASM, PGM1_2.ASM,
PGM2_2.ASM


qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa v?

V. CHNG TRNH THNG TRU BO NH.

Cung van hien th gi nhng bay gi la thng tru trong bo nh
(terminate and stay resident)

Thong thng khi ket thuc mot chng trnh, bo nh ma no chiem
gi c DOS s dung e nap chng trnh khac. Tuy nhien khi mot
chng trnh TSR ket thuc, bo nh khong b thu hoi lai. Chnh v the mot
chng trnh TSR con c goi la chng trnh thng tru bo nh
(memory resident program)

e tr ve DOS, mot chng trnh TSR ket thuc bang cach s dung
ngat 27h hay ngat 21h (ham 31h)

Chung ta se tao mot chng trnh .COM v e s dung ngat 27h
chung ta can xac nh so byte c lai thng tru trong bo nh. Cau
truc cua mot chng trnh .COM khien viec tnh toan de dang hn v no
ch co mot phan oan chng trnh.

Mot khi muon ket thuc hoan toan ngha la chng trnh TSR
khong hoat ong. Chng trnh nay phai c kch hoat bi cac tac ong
ngoai nh mot to hp phm nao o (V du:Ctrl+Shift). u iem cua
chng trnh thng tru la chung co the c kch hoat khi ang chay
mot chng trnh nao khac.

Chng trnh gom 2 phan: Phan khi tao thiet lap vect ngat va
phuc vu ngat

Tap tin PGM3_1.ASM

EXTRN MAIN:NEAR, SETUP_INT:NEAR
EXTRN NEW_VEC:DWORD, OLD_VEC:DWORD
PUBLIC INITIALIZE

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa vS

C_SEG SEGMENT PUBLIC
ASSUME CS:C_SEG
INITIALIZE PROC
;thiet lap vect ngat
MOV NEW_VEC,OFFSET MAIN; lu a ch
MOV NEW_VEC+2,CS
LEA DI, OLD_VEC ; DI tro ve vect cu
LEA SI, NEW_VEC ; SI tro ve vect mi
MOV AL,09H ;ngat ban phm
CALL SETUP_INT ; thiet lap vect ngat
;tr ve DOS
LEA DX, INITIALIZE
INT 27H ; Ket thuc va lai thng tru
INITIALIZE ENDP
C_SEG ENDS
END INITIALIZE

Cach n gian e nhan biet cac phm ieu khien la kiem tra cac
c phm (ham 2- ngat 16h). Chung ta se s dung to hp phm Ctrl va
Shift phai e kch hoat va ket thuc s hien th ong ho. Khi kch hoat,
thi gian hien tai se c hien th tai goc phai tren cua man hnh . au
tien phai chep lai d lieu man hnh e sau khi ket thuc hien th ong ho
ta co the phuc hoi lai man hnh cu.

Thu tuc SET_CURSOR thiet lap v tr con tro tai hang 0 va cot
trong DL
Thu tuc SAVE_SCREEN chep d lieu man hnh vao bo em
Thu tuc RESTOR_SCREEN tra la i d lieu cho bo em man hnh

Tap tin PGM3_2.ASM
EXTRN SS_BUF:BYTE
PUBLIC SAVE_SCREEN,RESTOR_SCREEN, SET_CURSOR
C_SEG SEGMENT PUBLIC
ASSUME CS:C_SEG

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa vv

;
SAVE_SCREEN PROC
;lu gi 8 ky t goc phai tren cua man hnh
LEA DI,SS_BUF ; Bo em man hnh
MOV CX,8
MOV DL,72 ;cot 72
CLD ; xoa DF cho thao tac chuoi
SS_LOOP:
CALL SET_CURSOR;thiet lap con tro
MOV AH,08H ; oc ky t tren man hnh
INT 10H ;AH=thuoc tnh/AL=ky t
STOSW ; cat ky t va thuoc tnh
INC DL ; cot tiep theo
LOOP SS_LOOP
RET
SAVE_SCREEN ENDP
;
RESTOR_SCREEN PROC
;phuc hoi man hnh a cat
LEA SI,SS_BUF ;SI tro en bo em
MOV DI,8 ;lap 8 lan
MOV DL,72 ;cot 72
MOV CX,1 ; moi lan 1 ky t
RS_LOOP:
CALL SET_CURSOR ;di chuyen con tro
LODSW ;AL=ky t/AH=thuoc tnh
MOV BL,AH
MOV AH,9 ; viet ky t va thuoc tnh
MOV BH,0 ;trang 0
INT 10h
INC DL ;v tr ky t tiep theo
DEC DI
JG RS_LOOP
RET

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 100

RESTOR_SCREEN END
;
SET_CURSOR PROC
;chuyen con tro en hang 0, cot DL
; vao DL=so hieu cot
MOV AH,2
MOV BH,0
MOV DH,0
INT 10H
SET_CURSOR END
;
C_SEG ENDS
END

Bay gi chung ta a san sang e viet thng trnh ngat. e xac
nh xem s hien th thi gian co c kch hoat hay khong ta s dung c
bien oi c ON_FLAG. C nay c thiet lap bang 1 khi thi gian
c hien th. MAIN la thu tuc ngat

Tap tin PGM3_3.ASM

;chng trnh thng tru bo nh ch thi gian
; kch hoat bang to hp phm Ctrl+Shift phai
EXTRN INITIALIZE:NEAR,SAVE_SCREEN:NEAR
EXTRN RESTOR_SCREEN:NEAR,SET_CURSOR:NEAR
EXTRN GET_TIME:NEAR
PUBLIC MAIN
PUBLIC NEW_VEC,OLD_VEC,CURSOR_POS
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:C_SEG, SS:C_SEG
ORG 100H
START:
JMP MAIN
;

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 101

SS_BUF DB 16DUP(?) ;bo em lu gi man hnh
TIME_BUF DB '00:00:00$'
CURSOR_POS DW ? ;v tr con tro
ON_FLAG DB 0 ;1=chay thu tuc ngat
NEW_VEC DW ?,? ; cha vect mi
OLD_VEC DW ?,? ;cha vect cu
;
MAIN PROC
;thu tuc ngat
; cat cac thanh ghi
PUSH DS
PUSH ES
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
PUSH DI
MOV AX,CS ;thiet lap DS
MOV DS,AX
MOV ES,AX
;Goi thu tuc ngat ban phm cu
PUSHF
CALL INITIALIZE
;lay cac c ban phm
MOV AX,CS ;thiet lap lai DS
MOV DS,AX
MOV ES,AX ;ES cho phan oan hien hanh
MOV AH,02 ; xem c ban phm
INT 16H ;AL cha cac c ban phm
TEST AL,1 ; Shift phai?
JE I_DONE ; khong, thoat
TEST AL,100B ;Ctrl?
JE I_DONE ;khong, thoat

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 10z

;ung, x ly
CMP ON_FLAG,1;thu tuc ang c kch hoat?
JE RESTOR ;ung, kh kch hoat
MOV ON_FLAG,1 ;khong, kch hoat
;cat v tr con tro va thong tin man hnh
MOV AH,03 ;lay v tr con tro
MOV BH,0 ;trang 0
INT 10H ;DH=hang,DL=cot
MOV CURSOR_POS,DX ;lu v tr con tro
CALL SAVE_SCREEN ;lu man hnh
; hien th thi gian
; nh v con tro en goc tren ben phai
MOV DL,72 ; cot 72
CALL SET_CURSOR
LEA BX,TIME_BUF
CALL GET_TIME ; lay thi gian hien tai
;hien th thi gian
LEA SI,TIME_BUF
MOV CX,8 ; 8ky t
MOV BH,0 ; trang 0
MOV AH,0EH ; viet ky t
M1:
LODSB ;ky t trong AL
INT 10H ;con tro c chuyen
; en hang tiep theo
LOOP M1 ; lap lai 8 lan
JMP RES_CURSOR
RESTOR:
;phuc hoi man hnh
MOV ON_FLAG,0 ;xoa c
CALL RESTOR_SCREEN
;phuc hoi v tr con tro lu tr
RES_CURSOR:
MOV AH,02 ;thiet lap con tro

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 10+

MOV BH,0
MOV DX,CURSOR_POS
INT 10H
;phuc hoi cac thanh ghi
I_DONE: POP DI
POP SI
POP DX
POP CX
POP BX
POP AX
POP ES
POP DS
IRET ;tr ve t ngat
MAIN ENDP
C_SEG ENDS
END START
Tap tin PGM3_4.ASM

PUBLIC GET_TIME,SETUP_INT
C_SEG SEGMENT PUBLIC
ASSUME CS:C_SEG
;
GET_TIME PROC
MOV AH,2CH
INT 21H
MOV AL,CH
CALL CONVERT
MOV [BX],AX
MOV AL,CL
CALL CONVERT
MOV [BX+3],AX
MOV AL,DH
CALL CONVERT
MOV [BX+6],AX

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 10o

RET
GET_TIME ENDP
;
CONVERT PROC
MOV AH,0
MOV DL,10
DIV DL
OR AX,0303H
RET
CONVERT ENDP
;
SETUP_INT PROC
MOV AH,35H
INT 21H
MOV [DI],BX
MOV [DI+2],ES
MOV DX,[SI]
PUSH DS
MOV DS,[SI+2]
MOV AH,25H
INT 21H
POP DS
RET
SETUP_INT ENDP
;
C_SEG ENDS
END

LINK PGM3_3+PGM3_2+PGM3_4+PGM3_1.
Chu y PGM3_1 c lien ket cuoi cung e thu tuc INITIALIZE
c a vao cuoi chng trnh. Viet cac chng trnh TSR rat phc tap,
neu nh a co mot chng trnh TSR khac trong he thong, chng trnh
hien co co the khong con thc hien ung na

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 10s

PHU LUC A: MOT SO CHNG TRNH PHUC VU CUA DOS

Chc nang Ngat
(Hex)
TG vao
Ket thuc chng trnh 20
Ket thuc nhng nam
thng trc (max 64KB)
27 DS:DX=ach byte sau vung c gi
lai thng tru


CAC CHNG TRNH PHUC VU CUA DOS (NGAT 21h)

Chc nang So (Hex) TG vao TG ra
a vao t ban phm 01
AH=01
AL=ky t vao 8 bit

a ra man hnh mot ky
t
02
AH=02
DL=ky t ra 8 bit

a ra may in 05
AH=05
DL=ky t

a vao trc tiep t ban
phm va khong hien
07
AH=07
AL=ky t vao 8 bit

a ra man hnh mot
chuoi
09
AH=09
DS:DX = tro en
chuoi ket thuc bang
'$'

a vao em ban phm 0A
AH=0A
DS:DX=tro en em
ban phm

Thiet lap vect ngat 25
AH=25
AL= so hieu ngat
DS:DX= tro ti
chng trnh x ly
ngat

Lay ngay thang nam 2A
AH=2A AL=th trong tuan
CX=nam (1980-2099
DH=thang
DL=ngay

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 10o


qaqeo 7aa 1ce 7hco 7ot 1q
Chc nang So (Hex) TG vao TG ra
at ngay thang nam 2B
AH=2B
CX=nam (1980-2099
DH=thang
DL=ngay

AL=ma tra ve
Lay thi gian 2C
AH=2C CL=phut
CH=gi
DL=phan tram giay
DH=giay
at thi gian 2D
AH=2D
CL=phut
CH=gi
DL=phan tram giay
DH=giay

Ket thuc chng trnh va
thng tru
31
AH=31
AL=ma tra ve
DX=a ch oan
chng trnh tren bo
nh e giai phong

AX=ma tra ve
Lay vect ngat 35
AH=35
AL=so hieu ngat

ES:BX=vect ngat
AX=ma tra ve
Tao th muc 39
AH=39
DS:DX = tro en
chuoi ASCII ten
ng
Neu sai CF=01,
AX= ma loi
Neu tot: CF=00
Xoa th muc 3A
AH=3A
DS:DX = tro en
chuoi ASCII ten
ng
Neu sai CF=01,
AX= ma loi

oi th muc hien thi 3B
AH=3B
DS:DX = tro en
chuoi ASCII ten
ng
Neu sai CF=01,
AX= ma loi

Tao file 3C
AH=3C
CX=ac tnh file
DS:DX= tro en
AX=the file hoac ma
tra ve
CF=00 (tot), 01 (sai)

7cp qa 10?


qaqeo 7aa 1ce 7hco 7ot 1q
Chc nang So (Hex) TG vao TG ra
chuoi ASCII ten file
M the 3D
AH=3D
CX=ma truy nhap
DS:DX = tro en
chuoi ASCII ten file
AX=the file hoac ma
tra ve

ong the 3E
AH=3E
BX=the file
AX=the file hoac ma
tra ve
oc file hoac t thiet b 3F
AH=3F
BX=the file
CX=so byte can oc
DS:DX= tro en
vung em d lieu
AX=so byte oc c
hoac ma tra ve
Ghi file hoac ra thiet b 40
AH=40
BX=the file
CX=so byte can ghi
DS:DX= tro en
vung em d lieu
AX=so byte ghi c
hoac ma tra ve
Xoa file 41
AH=41
DS:DX = tro en
chuoi ASCII ten file
Neu sai CF=01, AX=
ma tra ve
Ket thuc qua trnh 4C
AH=4C
AL= ma tra ve

oi ten file 56
AH=56
DS:DX = tro en
chuoi ASCII ten cu
ES:DI = tro en
chuoi ASCII ten mi
Neu sai CF=01, AX=
ma tra ve









7cp qa 10S

PHU LUC B: MOT SO CHNG TRNH PHUC VU CUA ROM-
BIOS



Chc nang Ngat
(Hex)
TG vao TG ra
Di chuyen con tro 10
AH=2
BH=so hieu trang
DH=so hieu dong
DL=so hieu cot

Lay v tr con tro 10
AH=3
BH=so hieu trang
DH=so hieu cot
DL=so hieu dong
CH=dong quet au
cua con tro
CL=dong quet cuoi
cua con tro
oc mot ky t va thuoc
tnh
10
AH=8
BH=trang man hnh
AH=thuoc tnh
AL=ky t
Ghi 1 ky t 10
AH=0E
AL=ky t
BL=mau trong kieu
o th

oc mot cung 13
AH=02
AL= so cung
CH=ranh
CL=cung
DL=o a
ES:BX=con tro en
em
CF=bao ket qua
tot/hong
AH=ma trang thai
AL=so cung c
oc
Ghi mot cung 13
AH=03
AL= so cung
CH=ranh
CL=cung
DL=o a
ES:BX=con tro en
em
CF=bao ket qua
tot/hong
AH=ma trang thai
AL=so cung c ghi

qaqeo 7aa 1ce 7hco 7ot 1q

7cp qa 10v


qaqeo 7aa 1ce 7hco 7ot 1q
Chc nang Ngat
(Hex)
TG vao TG ra
Lay cac tham so hien
thi cua o a
13
AH=08
DL=so hieu a


DL=so o a
DH=so lng mat
max
CL[0,5]=so hieu
cung max
CL[6,7]=2 bit cao
cua so lng t tru
max
CH=ca'c bit thap cua
so lng t tru max
CF=c bao ket qua
tot/hong
AH=ma trang thai
oc ky t tiep t ban
phm
16
AH=00 AH=ma quet ban
phm
AL=ma ASCII
Lay c ban phm 16
AH=02 AL=c cac phm
bit 0=1: Shift phai
bit 1=1: Shift trai
bit 2=1: Ctrl
bit 3=1: Alt
bit 4=1: Scroll lock
bit 5=1: Num Lock
bit 6=1:Caps lock
bit 7=1: Che o chen
Gi mot byte ra may in 17
AH=00
AL=ky t
DX=so may in
Gia tr cac bit:
0: qua thi gian
1: khong s dung
2: khong s dung
3=1:loi vao/ra
4=1: a c chon
5=1:het giay
6=1:xac nhan
7=1:khong ban


7cp qa 110

TAI LIEU THAM KHAO



1. Ngon ng lap trnh Assembly va may vi tnh IBM-PC - Ytha Yu va
Charles Marut (Quach Tuan Ngoc-o Tien Dung- Nguyen Quang
Khai bien dch) - Nha xuat ban Giao duc

2. Lap trnh Hp ng Turbo Assembler 2.0 - Phan Trng Dan - Nha xuat
ban Giao duc

3. Cam nang lap trnh - Peter Norton (Nguyen Minh San dch) - Nha xuat
ban Giao Duc

4. Cam nang cho ngi lap trnh tren may vi tnh IBM- Peter Norton -
Nha xuat ban Thong ke - Vien ien t - Tin hoc

5. Assembleur facile - PhMercier- Marabout

6. Hardware Reference Manual 8088 - Intel

7. Macro Assembler Programmer's Guide 5.0 - Microsoft










qaqeo 7aa 1ce 7hco 7ot 1q