30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
Laptrinhhethong
1. HƯ NG D N V NG T TRONG C (S
D NG THƯ VI N DOS.H ) VÀ CON TR
M NG
L i nói u :
+ Chào các b n ,L p trình h th ng là h c ph n b t bu c i v i các sinh viên Khoa CNTT, kì trư c ã
h c l p trình h th ng r i nhưng còn r t nhi u b n chưa h c (trong ó có tôi) .Và m t th c t là trên
trư ng ch d y nh ng ki n th c cơ b n nh t nên nhi u b n dù ã ư c h c r i nhưng v n chưa n m
v ng ư c v n này .
+ L p trình h th ng theo tôi là m t môn h c quan tr ng ,là n n t ng các b n có th h c t t các môn
chuyên ngành sau này như Vi x lí ,vi i u khi n…Và n u b n nào có ý nh i theo ngành nhúng thì
môn này c bi t ph i n m v ng .
+ Th t ra l p trình h th ng là m t lĩnh v c khá l n nhưng theo L th y trên trư ng ch d y ch y u hai
ph n ng t dos và con tr m ng.
+ N u các b n n m v ng hai v n này thì các môn h c sau s h c r t nh nhàng.
Chính vì lí do ó nên L vi t ra ebook này nh m giúp các b n chưa ư c h c ho c chưa n m v ng v
ph n này có m t tài li u tham kh o.
Ebook ư c vi t trong th i gian ng n nên L không th nói h t ư c t t c v n nhưng hi v ng t
nh ng ki n th c cơ b n này b n có m t n n t ng có th t suy nghĩ nh ng v n khác. N u b n có
th c m c gì hay có v n gì không hi u b n có th liên h tr c ti p v i L ư c gi i áp.
YM :thelinh_wsg
Email :thelinhbk@gmail.com
B n có toàn quy n s d ng tài li u này nhưng vui lòng thông tin tác gi …….
---------------------------------------------------------------------------------------------------------------------------------------
A .NG T DOS TRONG C :
+ Công c vi t chương trình
Vì ng t dos s d ng thư vi n dos.h vì v y l p trình b n ph i s d ng Tuborl C ho c Borland C
Visula C++ không h tr thư vi n này.
N u chưa có b n có th t i trên m ng ho c tìm mau các c a hàng bán ĩa ,ho c liên h tr c ti p v i L
l y (kho ng vài MB v i TC còn BC thì n ng hơn tí)
Sau khi cài t xong thì b n ã có trong tay công c l p trình.
Gi chúng ta vào ch chính thôi::D
Vì ng t DOS thì có r t nhi u hàm nhưng trong bài vi t này L ch nh c n nh ng hàm hay dùng (th y ã
d y trư ng ).
Hàm th nh t là hàm 1 c a ng t 21h
+ Các b n chú ý là m i ng t thư ng có u vào và u ra
Ví d ây là hàm th nh t c a ng t 21h dùng nh p m t kí t ( ch m t kí t thôi nha)
Vào AH, ra AL
T t c m i hàm c a ng t 21h u vào AH
V y AH là gì
2. B n nhìn hình sau ây
Hình trên là m t thanh ghi AX 16 bit ( 2byte) g m 2 thanh ghi 8 bit là AH và AL
Trong ó AL là 8 bit th p c a AX còn AH là 8 bít cao
Ngoài AX ta còn có các thanh ghi BX, CX, DX tương t như AX
Ví d thanh ghi BX cũng g m hai thanh ghi 8 bit là BH và BL
Các bít ư c ánh d u như hình trên
Hàm th nh t c a ng t 21h ư c th c hi n như sau:
Gán cho thanh ghi AH giá tr là 1 ( t c là hàm th nh t)
Sau ó g i ng t 21h. Sau khi g i l nh ng t 21h thì con tr trên màn hình s xu t hi n d u nh c
ch b n nh p vô m t kí t .Sau khi b n nh p xong m t kí t thì kí t ó s ư c lưu trong thanh ghi
AL trên
+ L nh c th như sau
Trong C có th s d ng ư c cái hàm ng t b n ph i include thư vi n dos.h vào
include<dos.h>
Sau âu là hàm nh p m t kí t s d ng hàm 1 nh t ng t 21h
#include<stdio.h>
#include<conio.h>
#include<dos.h>
int input()
{
int x;
// u tiên b n ph i khai báo cái này
union REGS regs,regs;
// B n ph i nh p 1 vào thanh ghi AH báo cho nó bi t là b n mu n s d ng hàm 1 ng t 21h
//Câu l nh như sau ,các b n nh áp d ng tương t cho các thanh ghi khác
regs.h.ah =1; // t c là n p 1 vô thanh ghi ah
// Sau ó g i hàm ng t 21h
int86(0x21,®s,®s) ;
//Sau khi g i hàm này trên màn hình s xu t hi n d u nh c b n nh p vô m t kí t
// sau khi b n nh p xong thì nó s lưu kí t b n v a nh p vô thanh ghi AL
//b n l y nó ra
x = regs.h.al ;// t c là gán cho x b ng giá tr thanh ghi AL,chính là kí t b n v a nh p
x = x – 0x30 // 0x ch s th p l c phân
//V y t i sao l i tr 0x30 s gi i thích sau
return x
}
Các b n chú ý ch này nha:
3. Sau ây là b ng mã
ASCII
Trong b ng mã này các b n chú ý là c t char là kí t còn hex và dec chính là mã th p phân và th p l c
phân c a kí t ó.
Ví d các b n nhìn vào kí t s 1 ( kí t 1 ch ko ph i s 1 âu nghe)
Kí t 1 này có mã th p l c phân là 30 mã th p phân là 31
Khi b n s d ng hàm 1 c a ng t 21h nó cho phép b n nh p kí t thui.
Ví d như b n nh p vô 1 t bàn phím t c là kí t 1 ch ko ph i s 1
Kí t 1 này có mã hex là 0x31
Nhưng b n l i mu n nh p vô 1 s ch ko ph i là kí t
Chính vì v y nên b n ph i tr i 0x30
x = regs.h.al
x = x – 0x30
ví d nh p vô 1 thì trong thanh ghi AL nó lưu kí t 1 (0x31)
sau ó
x = regs.h.al
t c là x=0x31
nhưng b n mu n nh p s 1 ch ko ph i kí t 1 nên c n tr 0x30 i
x=x-0x31
cu i cùng x= 0x01 = 1
return x
k t qu là hàm input này tr v cho ta m t con s 1
4. N u b n nh p 2 thì sao
Thì x =0x32 ( nhìn lên b ng phía trên)
x=x-0x30
suy ra :x=0x02 =2
như v y b n ch c n tr x i 0x30 là có th l y ư c s t kí t
Sau ây là m t chương trình y .S d ng hàm th nh t c a ng t 21h nh p m t kí t
-----------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<dos.h>
int nhap();
void main()
{
int x;
printf ("n Moi ban nhap mot so tu 0->9:");
x=nhap();
printf("n So ban vua nhap la: %d",x);
getch();
}
int nhap()
{
union REGS regs;
int x;
regs.h.ah=1;
int86(0x21,®s,®s);
x=regs.h.al;
x -=0x30;
return x;
}
-----------------------------------------------------------------------------------------------------------------------
Hàm th nh t này có m t h n ch là ch cho phép nh p m t kí t thui.
Vì v y b n ch có th nh p t 0->9 mà thui.Nhưng th y l i yêu c u b n nh p vào m t s có nhi u hơn 1
ch s .Ví d như 15 hay 1988 ch ng h n mà yêu c u là v n dùng hàm th 1 c a ng t 21h này.
Thu t toán sau ây s giúp b n
Ví d gi b n mu n nh p vô 1988 i
T c là b n l n lư t nh p vô 1 , 9, 8, 8 t bàn phím .Nhi m v c a chúng ta là làm sao bi n nó thành 1988
Các b n ý như sau
Lúc âu b n nh p vô kí t 1 -> chuy n nó thành s 1 ( tr 0x30) lưu l i x=1
Sau ó b n nh p ti p kí t 9 -> chuy n nó thành s 9 và làm như sau x=x*10+9
V y ta có 19 lúc này x=19
B n nh p ti p kí t 8 vô -> chuy n nó thành s 8 và x=x*10+8 =198
B n nh p ti p vô 8 n a -> chuy n thành s 8 và x=x*10+8=1988
V y cu i cùng x=1988 úng yêu c u c a b n rùi
***Nhưng l i g p môt khó khăn khác:
N u như b n ch y ví d trên s th y.Khi con tr hi n ra d y nháy ch ta nh p m t kí t ,ta v a nh p
xong ngay l p t c nó xu t ra li n âu có ta nh p ti p con s khác
B n ng lo l ng ,vi c này r t ơn gi n.
---------------------------------------------------------------------------------------------------------------
B n l i ti p t c nhìn l i cái b ng mã ASCII lúc n y
B n ý hàng sau:
Hex =0D
5. Dec =13
Cái mã này không ch m t kí t nào mà nó c t code nó là CR
CR ây chính là kí hi u c a phím xu ng dòng và quay l i u dòng ( Enter ch còn gì n a)
Như v y phím Enter có mã hex là 0D và mã dec là 13
HÌ
T i u ó b n nghĩ ra m t cách ,t i sao ko dùng vòng l p cho phép nh p nhi u kí t
do
{
regs.h.ah=1;
int86(0x21,®s,®s);
x=regs.h.al;
if(x==0x0D)
goto EXIT
x-=0x30;
EXIT:
}
while(x!=0x0D);
o n l nh trên như sau:
Làm trong khi x!=13
Ví d nha. B n nh p vào 1
x=regs.h.al
->x=0x31// ( vì mã hex c a kí t 1 là 0x31)
ki m tra xem x có b ng 0x0D=13 ko
:D r ràng là x ây b ng 0x30 ch b ng 0x0D âu
Quay l i ti p t c nh p ti p
Khi b n nh p xong 1988 rùi b n không mu n nh p n a
B n nh n enter
Khi b n nh p enter thì nó s nh p vô thanh ghi AL =0x0D
B n c coi Enter như 1 kí t (CR) có mã hex là 0x0D và mã dec là 13
x=regs.h.al;
v y x=0x0D =13;
Ki m tra i u ki n xem : Uh ùng rùi x úng b ng 0x0D rùi
K t thúc l p và chuy n sang câu l nh ti p theo
Như v y là ta có th nh p vào nhi u kí t ư c rùi
Nhưng v n chưa lưu ư c kí t x=1988 mà k t thúc o n l nh v a rùi x=8 là kí t cu i cùng
Ta s a l i code như sau
------------------------------------------------------------------------------------------------------------------------------
int nhap()
{
union REGS regs;
int x=0;
int temp;
do
{
regs.h.ah=1;
int86(0x21,®s,®s);
temp=regs.h.al;
if(temp=0X0D)
goto EXIT;
temp -=0x30;
x=x*10+temp;
EXIT:
6. }while(temp!=0X0D);
return x;
}
------------------------------------------------------------------------------------------------------------------------------
Khi b n nh n Enter thì (coi như b n nh p kí t CR) thì x=regs.h.al=0x0D
Nó ki m tra if(x==0xoD) úng
Goto EXIT t c là nó nh y n nh y EXIT phái dư i mà ko làm các l nh phía sau
T i EXIT nó g p hàm ki m tra i u ki n l p
while(temp!=0X0D); úng rùi và k t thúc l p
lúc này x=1988 rùi
x m i chính là bi n lưu ,temp ch là bi n l m lưu giá tr al hi n th i.
Phù phù m t quá ….
Như v y là b n ã bi t cách s d ng hàm 1 c a ng t 21h nh p m t s rùi
Gi L hư ng d n các b n dùng hàm 2 c a ng t 21h xu t m t s t m t thanh ghi
s d ng hàm 2 ng t 21h
Tương t như hàm 1 b n ph i nh p 2 vô thanh ghi AH cho nó bi t là b n mu n s d ng hàm 2
Còn giá tr mu n xu t thì ph i n m trong thanh ghi DL
(nh c l i thanh ghi DX 16 bít g m hai thanh ghi DH và DL 8 bít)
Như v y b n mu n xu t cái chi ra ph i n p nó vô thanh ghi DL
Chương trình trên b n ch nh p vô b ng ng t thui còn xu t thì dùng C
printf("n So ban vua nhap la: %d",x);
getch();
gi ta th vi t m t hàm output xu t giá tr x tra màn hình dùng hàm 2 ng t 21h
----------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<dos.h>
int nhap();
void xuat(int x);
void main()
{
int x;
printf ("n Moi ban nhap mot so tu 0->9:");
x=nhap();
xuat(x);
getch();
}
int nhap()
{
union REGS regs;
int x=0;
int temp;
do
{
regs.h.ah=1;
int86(0x21,®s,®s);
temp=regs.h.al;
7. if(temp==0x0D)
goto EXIT;
temp -=0x30;
x=x*10+temp;
EXIT:
}while(temp!=13);
return x;
}
void xuat(int x)
{
union REGS regs;
regs.h.ah=2;
x+=0x30;
regs.h.dl=x;
int86(0x21,®s,®s);
}
--------------------------------------------------------------------------------------------------------------------------
Các b n ý t i hàm xu t
Hàm 2 c a ng t 21h cũng ch cho phép xu t ra m t kí t thui nên b n ph i x+=0x30 chuy n s thành
kí t
N p nó vô DL và g i hàm 21h xu t ra màn hình
Chương trình trên ch xu t ra ư c 1->9 thui ko th xu t ra 2 ch s vì hàm 2 cũng gi i h n xu t ra m t
kí t .
Mu n xu t ra ư c s 1988 b n ã nh p lúc u thì ph i dùng thu t toán tách t ng ch s ra
1988 /10 =198 dư 8
198/10=19 dư 8
19/10 =1 dư 9
1/10 =0 dư 1
V y là ta có th tách ra xu t ư c rùi.
Còn chương trình thì b n t suy nghĩ. Coi như bài t p cho các b n.
Chú ý là các s dư ư c xu t ra theo chi u ngư c l i lúc nh p nên b n ph i o l i
Như v y là các b n ã có th h u ư c cách dùng hàm 1 và 2 c a ng t 21h nh p xu t
Ti p theo L xin gi i thi u v i các b n m t bài toán c th
Dùng ng t 21h chuy n m t s t th p phân sang nh phân
u tiên b n dùng hàm input trên nh p vào m t s th p phân x
Sau ó chuy n nó thành d ng nh phân và xu t ra
Các b n chú ý ch này :
Ví d ta dùng hàm input nh p vô s 12
Sau ó gán cho AX giá tr 12
Trong AX s lưu 12 dư i d ng nh phân ch ko ph i th p phân
12 =1100
Chú ý là ánh t bít th p t i bít cao. T c là t ph i sang trái
N u b n nh p vô 1988
8. 1988 = 0000011111000100
Thì thanh ghi AX s như sau
L xin d ng l i nói sơ m t chút v d ch bít
Ví d x=12 thì x ư c lưu trong máy tính có d ng như sau
Cái mô ph ng này th t ra ko úng v i th c t l m nhưng L c ý làm như v y các b n m i h c có th
hi u nhanh hơn
M i tên ch t i bít 0
Gi chúng ta dùng l nh sau
x>>1 t c là d ch m i tên i 1 bit
x có d ng như sau
Các b n chú ý m i tên ch ch mô thì ta có th xác nh ư c bít ó b ng cách &1
Vì v y in ra 16 bít c a x ta làm như sau:
void convert_to_bin(int x)
{
for(int i=15;i>=0;i--)
{
printf("%d",(x>>i)&1);
}
}
Ban u ta d ch i 15 bít con tr ch n bít th 15 và &1 in ra bít ó
Sau ó gi m 1 xu ng còn 14
D ch i 14 bít và in bít th 15
Ti p t c cho t i bít th 0 bên ph i ngoài cùng
Như v y là b n có th d dàng chuy n t s th p phân x v d ng nh phân ch b ng m y dòng code trên
Các b n chú ý là ây ta nh nghĩa bi n x ki u int nên ch chi m 2byte
2byte t c là 16 bít
Nên các b n for(int i=15;i>=0;i--)
9. Nhưng n u b n nh nghĩa ki u unsigned x thì nó chi m t i 4 byte =32 bít nên b n ph i in t bit 31 ->bit
0
for(int i=31;i>=0;i--)
.Phù phù m t quá , bài trên b n ã h c cách chuy n sang d ng nh phân rùi gi s h c cách chuy n sang
d ng t phân
T phân là các m t s ư c t o thành t các ch s 0,1,2,3
Ví d s t phân :1230 = 1*4^2 +2*4^2+3*4^1+0*4^0 =298 = 0000001010011000
14 t phân=12= 00001100
Các b n xem hình sau:
C hai ch s nh phân quy nh m t s t phân
Theo trên thì 12 = 0000000000001100 = 00000030
T c là 12 d ng th p phân = 30 t phân
T ó ta suy nghĩ ra cách chuy n như sau
----------------------------------------------------------------------------------------
Ban u b n nh p 12 vô AX thì AX có d ng như trên
Như ã nói AX là thanh ghi 16 bít g m hai thanh ghi 8 bít là AH và AL
Theo hình trên thì
AH = 00000000
AL = 00001100
Gi ta mu n i s nh phân ư c lưu trong thanh ghi AX ra d ng t phân thì ta l n lư t in ra các c p
bit
u tiên b n ph i in ra giá tr c a hai bít ngoài cùng bít 15 và 14
in ra hai bít ó thì chúng ta c n làm như sau
+Cách 1 d ch AH sang ph i 6 bít
Ban u AH như sau
Sau khi d ch qua ph i 6 bít gi ng như b n êm cái AH kéo qua ph i 6 bít
S có d ng như sau ,sau khi d ch ph i 6 bít
10. Sau ó b n ch vi c in AH ra màn hình b ng hàm 2 c a ng t 21h
Cu i cùng b n d ch AX qua trái 2 bít ti p t c in hai bit ti p theo
Và ti p t c l p l i thao tác ó 8 l n (Vì thanh ghi AX 16 bít nên 16/2 =8)
o n l nh c th như sau:
void convert_to_tu(int x)
{
union REGS regs;
int i=1,ah,temp;
regs.h.ax=x;
while(i<=8)
{
ah=regs.h.ah;
ah=ah>>6;
ah+=48;
temp=regs.x.ax<<2;
regs.h.dl=ah;
regs.h.ah=2;
int86(0x21,®s,®s);
regs.x.ax= temp;
i++;
}
}
Xin gi i thích sơ m t chút v o n l nh này;
Vì kí t 1 có mã hex là 0x31 nên mu n chuy n các con s 0,1,2,3,4,…9 sang d ng kí t in ra
Thì ph i c ng thêm 0x30 (th p phân là 48)
Sơ hoàn ch nh
11. Như v y là b n ã bi t cách chuy n sang d ng t phân, gi ti p sang d ng th p l c phân
M i ch s th p l c phân là tôt h p c a 4 bít nh phân
Vì v y cách làm tương t như t phân
Code hoàn ch nh như sau:
void convert_to_hex(int x)
{
union REGS regs;
int i=1,ah,temp;
regs.h.ax=x;
while(i<=4)
{
ah=regs.h.ah;
ah=ah>>4;
if(ah<=9) goto Num;
ah+=7;
Num:
ah+=48;
temp=regs.x.ax<<4;
regs.h.dl=ah;
regs.h.ah=2;
int86(0x21,®s,®s);
regs.x.ax= temp;
12. i++;
}
}
B n chú ý t i nh ng ch L to màu xanh.Code th p l c phân hoàn toàn gi ng như t phân ch khác
nhưng ch tô xanh
+ Ch th nh t i<=4 là vì m i ch s th p l c là t h p 4 bít nh phân ,
Mà ây có 16 bít nên c n l p l i 4 l n in ra 4 s th p l c phân
+ Ch th hai là vì
T phân ch có các ch s 0,1,2,3
Nhưng th p l c phân thì 0,1,2,3,…,9,A,B,C,D,E
i v i các s 0…9 thì ch c n c ng v i 0x30(48) là chuy n thành d ng kí t in nhưng các ch s như
A,B,C,D,E,F thì khác
10=A
11=B
…
15=F
B n xem l i b ng mã ASCII
Kí t A có mã hex là 0x41 và mã dec là 65
if(ah<=9) goto Num;
có nghĩa là n u ah<=9 t c là các s 0…9 ch hok ph i kí t thì nh y n nh n Num
Num:
ah+=48;
sau ó ah+=48 chuy n s trên thành d ng kí t in ra
nhưng n u nó l n hơn 9 t c là các kí t A,B,C,D,E,F
thì c n c ng thêm 7 trư c khi c ng 48
v y có nghĩa là c ng thêm 7+48 =55
vì A=10 nên ah=10+55=65 úng y mã dec c a kí t A
khi ah=11 thì ah=11+55 =66 úng ý mã dec c a kí t B
tương t như v y i v i C,D,E,F
+ Ch khác th ba
temp=regs.x.ax<<4;
là do m i ch sô th p l c phân = 4 bít nh phân nên c n d ch l i 4 bít in ra s hex ti p theo
13. Qua các ví d v a r i ch c các b n cũng hi u ph n nào v cách mà máy tính lưu tr d li u và cách
chuy n qua l i gi a các h m
Ng t trong dos còn có r t nhi u hàm khác nhưng b n ch c n n m quy t c sau
M i lo i ng t u có cung c p u vào u ra .
T c là b n c n nh p data vào thanh ghi nào và k t qu xu t ra thanh ghi nào
Ví d Hàm 1 c a ng t 21h thì b n ko c n cho ah=1(regs.h.ah=1) sau ó g i l nh ng t 21h
Int86(0x21,®s,®s); g i ng t
u ra c a ng t là thanh ghi AL t c là kí t b n nh p vô ch trong thanh ghi AL
Hàm 2 c a ng t 21h thì u vào là DL,AH
B n c n AH=2 và DL=kí t c n xu t
u ra là mà hình
:D
Như v y b t c ng t nào b n ch c n bi t các thông s u vào và u ra là có th s d ng ư c.
Mà các thông s y thì bao gi cũng ư c ghi s n trong tài li u r i.
Sau ây là các hàm ng t thông d ng b n tham kh o
m i hàm ng t u có input output n u có
Ví d ng t sau ây
14. INT 10h / AH = 0 - set video mode.
input:
AL = desired video mode.
these video modes are supported:
00h - text mode. 40x25. 16 colors. 8 pages.
03h - text mode. 80x25. 16 colors. 8 pages.
13h - graphical mode. 40x25. 256 colors. 320x200 pixels. 1 page.
example:
mov al, 13h
mov ah, 0
int 10h
Gi i thích : INT 10h / AH = 0 t c là ng t 10h AH=0 t c là b n c n set AH=0
Input là các giá tr c n đưa vào
Code:
Regs.h.ah=0;
Regs.h.al=00h
Int86(0x10,®s,®s);
Trong ó có nh ng hàm mà b n ít khi dùng t i nhưng L cũng ưa vào các b n tham kh o
INT 10h / AH = 0 - set video mode.
input:
AL = desired video mode.
these video modes are supported:
00h - text mode. 40x25. 16 colors. 8 pages.
03h - text mode. 80x25. 16 colors. 8 pages.
13h - graphical mode. 40x25. 256 colors. 320x200 pixels. 1 page.
example:
mov al, 13h
mov ah, 0
int 10h
INT 10h / AH = 01h - set text-mode cursor shape.
input:
CH = cursor start line (bits 0-4) and options (bits 5-7).
CL = bottom cursor line (bits 0-4).
when bit 5 of CH is set to 0, the cursor is visible. when bit 5 is 1, the cursor is not visible.
; hide blinking text cursor:
mov ch, 32
mov ah, 1
int 10h
; show standard blinking text cursor:
mov ch, 6
mov cl, 7
mov ah, 1
int 10h
; show box-shaped blinking text cursor:
mov ch, 0
mov cl, 7
mov ah, 1
int 10h
; note: some bioses required CL to be >=7,
; otherwise wrong cursor shapes are displayed.
INT 10h / AH = 2 - set cursor position.
15. input:
DH = row.
DL = column.
BH = page number (0..7).
example:
mov dh, 10
mov dl, 20
mov bh, 0
mov ah, 2
int 10h
INT 10h / AH = 03h - get cursor position and size.
input:
BH = page number.
return:
DH = row.
DL = column.
CH = cursor start line.
CL = cursor bottom line.
INT 10h / AH = 05h - select active video page.
input:
AL = new page number (0..7).
the activated page is displayed.
INT 10h / AH = 06h - scroll up window.
INT 10h / AH = 07h - scroll down window.
input:
AL = number of lines by which to scroll (00h = clear entire window).
BH = attribute used to write blank lines at bottom of window.
CH, CL = row, column of window's upper left corner.
DH, DL = row, column of window's lower right corner.
INT 10h / AH = 08h - read character and attribute at cursor position.
input:
BH = page number.
return:
AH = attribute.
AL = character.
INT 10h / AH = 09h - write character and attribute at cursor position.
input:
AL = character to display.
BH = page number.
BL = attribute.
CX = number of times to write character.
INT 10h / AH = 0Ah - write character only at cursor position.
input:
AL = character to display.
16. BH = page number.
CX = number of times to write character.
INT 10h / AH = 0Ch - change color for a single pixel.
input:
AL = pixel color
CX = column.
DX = row.
example:
mov al, 13h
mov ah, 0
int 10h ; set graphics video mode.
mov al, 1100b
mov cx, 10
mov dx, 20
mov ah, 0ch
int 10h ; set pixel.
INT 10h / AH = 0Dh - get color of a single pixel.
input:
CX = column.
DX = row.
output:
AL = pixel color
INT 10h / AH = 0Eh - teletype output.
input:
AL = character to write.
this functions displays a character on the screen, advancing the cursor and scrolling the screen
as necessary. the printing is always done to current active page.
example:
mov al, 'a'
mov ah, 0eh
int 10h
; note: on specific systems this
; function may not be supported in graphics mode.
INT 10h / AH = 13h - write string.
input:
AL = write mode:
bit 0: update cursor after writing;
bit 1: string contains attributes.
BH = page number.
BL = attribute if string contains only characters (bit 1 of AL is zero).
CX = number of characters in string (attributes are not counted).
DL,DH = column, row at which to start writing.
ES:BP points to string to be printed.
example:
17. mov al, 1
mov bh, 0
mov bl, 0011_1011b
mov cx, msg1end - offset msg1 ; calculate message size.
mov dl, 10
mov dh, 7
push cs
pop es
mov bp, offset msg1
mov ah, 13h
int 10h
jmp msg1end
msg1 db " hello, world! "
msg1end:
INT 10h / AX = 1003h - toggle intensity/blinking.
input:
BL = write mode:
0: enable intensive colors.
1: enable blinking (not supported by the emulator and windows command prompt).
BH = 0 (to avoid problems on some adapters).
example:
mov ax, 1003h
mov bx, 0
int 10h
bit color table:
character attribute is 8 bit value, low 4 bits set fore color, high 4 bits set background color.
note: the emulator and windows command line prompt do not support background blinking,
however to make colors look the same in dos and in full screen mode it is required to turn off
the background blinking.
HEX BIN COLOR
0 0000 black
1 0001 blue
2 0010 green
3 0011 cyan
4 0100 red
5 0101 magenta
6 0110 brown
7 0111 light gray
8 1000 dark gray
9 1001 light blue
A 1010 light green
B 1011 light cyan
C 1100 light red
D 1101 light magenta
E 1110 yellow
F 1111 white
note:
; use this code for compatibility with dos/cmd prompt full screen mode:
mov ax, 1003h
mov bx, 0 ; disable blinking.
int 10h
18. INT 11h - get BIOS equipment list.
return:
AX = BIOS equipment list word, actually this call returns the contents of the word at
0040h:0010h.
Currently this function can be used to determine the number of installed number of floppy disk
drives.
Bit fields for BIOS-detected installed hardware:
bit(s) Description
15-14 Number of parallel devices.
13 Reserved.
12 Game port installed.
11-9 Number of serial devices.
8 Reserved.
7-6 Number of floppy disk drives (minus 1):
00 single floppy disk;
01 two floppy disks;
10 three floppy disks;
11 four floppy disks.
5-4 Initial video mode:
00 EGA,VGA,PGA, or other with on-board video BIOS;
01 40x25 CGA color.
10 80x25 CGA color (emulator default).
11 80x25 mono text.
3 Reserved.
2 PS/2 mouse is installed.
1 Math coprocessor installed.
0 Set when booted from floppy.
INT 12h - get memory size.
return:
AX = kilobytes of contiguous memory starting at absolute address 00000h, this call returns the
contents of the word at 0040h:0013h.
Floppy drives are emulated using FLOPPY_0(..3) files.
INT 13h / AH = 00h - reset disk system.
INT 13h / AH = 02h - read disk sectors into memory.
INT 13h / AH = 03h - write disk sectors.
input:
AL = number of sectors to read/write (must be nonzero)
CH = cylinder number (0..79).
CL = sector number (1..18).
DH = head number (0..1).
DL = drive number (0..3 , for the emulator it depends on quantity of FLOPPY_ files).
ES:BX points to data buffer.
return:
19. CF set on error.
CF clear if successful.
AH = status (0 - if successful).
AL = number of sectors transferred.
Note: each sector has 512 bytes.
INT 15h / AH = 86h - BIOS wait function.
input:
CX:DX = interval in microseconds
return:
CF clear if successful (wait interval elapsed),
CF set on error or when wait function is already in progress.
Note:
the resolution of the wait period is 977 microseconds on many systems (1 million microseconds
- 1 second).
Windows XP does not support this interrupt (always sets CF=1).
INT 16h / AH = 00h - get keystroke from keyboard (no echo).
return:
AH = BIOS scan code.
AL = ASCII character.
(if a keystroke is present, it is removed from the keyboard buffer).
INT 16h / AH = 01h - check for keystroke in the keyboard buffer.
return:
ZF = 1 if keystroke is not available.
ZF = 0 if keystroke available.
AH = BIOS scan code.
AL = ASCII character.
(if a keystroke is present, it is not removed from the keyboard buffer).
INT 19h - system reboot.
Usually, the BIOS will try to read sector 1, head 0, track 0 from drive A: to 0000h:7C00h. The
emulator just stops the execution, to boot from floppy drive select from the menu: 'virtual
drive' -> 'boot from floppy'
INT 1Ah / AH = 00h - get system time.
return:
CX:DX = number of clock ticks since midnight.
AL = midnight counter, advanced each time midnight passes.
notes:
there are approximately 18.20648 clock ticks per second,
and 1800B0h per 24 hours.
AL is not set by the emulator.
20. INT 20h - exit to operating system.
INT 21h / AH=2 - write character to standard output.
entry: DL = character to write, after execution AL = DL.
example:
mov ah, 2
mov dl, 'a'
int 21h
INT 21h / AH=5 - output character to printer.
entry: DL = character to print, after execution AL = DL.
example:
mov ah, 5
mov dl, 'a'
int 21h
INT 21h / AH=6 - direct console input or output.
parameters for output: DL = 0..254 (ascii code)
parameters for input: DL = 255
for output returns: AL = DL
for input returns: ZF set if no character available and AL = 00h, ZF clear if character available.
AL = character read; buffer is cleared.
example:
mov ah, 6
mov dl, 'a'
int 21h ; output character.
mov ah, 6
mov dl, 255
int 21h ; get character from keyboard buffer (if any) or set ZF=1.
INT 21h / AH=7 - character input without echo to AL.
if there is no character in the keyboard buffer, the function waits until any key is pressed.
example:
mov ah, 7
int 21h
INT 21h / AH=9 - output of a string at DS:DX. String must be terminated by '$'.
example:
org 100h
mov dx, offset msg
mov ah, 9
int 21h
21. ret
msg db "hello world $"
INT 21h / AH=0Ah - input of a string to DS:DX, fist byte is buffer size, second byte is number of
chars actually read. this function does not add '$' in the end of string. to print using INT 21h /
AH=9 you must set dollar character at the end of it and start printing from address DS:DX + 2.
example:
org 100h
mov dx, offset buffer
mov ah, 0ah
int 21h
jmp print
buffer db 10,?, 10 dup(' ')
print:
xor bx, bx
mov bl, buffer[1]
mov buffer[bx+2], '$'
mov dx, offset buffer + 2
mov ah, 9
int 21h
ret
the function does not allow to enter more characters than the specified buffer size.
see also int21.asm in c:emu8086examples
INT 21h / AH=0Bh - get input status;
returns: AL = 00h if no character available, AL = 0FFh if character is available.
INT 21h / AH=0Ch - flush keyboard buffer and read standard input.
entry: AL = number of input function to execute after flushing buffer (can be 01h,06h,07h,08h,
or 0Ah - for other values the buffer is flushed but no input is attempted); other registers as
appropriate for the selected input function.
INT 21h / AH= 0Eh - select default drive.
Entry: DL = new default drive (0=A:, 1=B:, etc)
Return: AL = number of potentially valid drive letters
Notes: the return value is the highest drive present.
INT 21h / AH= 19h - get current default drive.
Return: AL = drive (0=A:, 1=B:, etc)
22. INT 21h / AH=25h - set interrupt vector;
input: AL = interrupt number. DS:DX -> new interrupt handler.
INT 21h / AH=2Ah - get system date;
return: CX = year (1980-2099). DH = month. DL = day. AL = day of week (00h=Sunday)
INT 21h / AH=2Ch - get system time;
return: CH = hour. CL = minute. DH = second. DL = 1/100 seconds.
INT 21h / AH=35h - get interrupt vector;
entry: AL = interrupt number;
return: ES:BX -> current interrupt handler.
INT 21h / AH= 39h - make directory.
entry: DS:DX -> ASCIZ pathname; zero terminated string, for example:
org 100h
mov dx, offset filepath
mov ah, 39h
int 21h
ret
filepath DB "C:mydir", 0 ; path to be created.
end
the above code creates c:emu8086vdriveCmydir directory if run by the emulator.
Return: CF clear if successful AX destroyed. CF set on error AX = error code.
Note: all directories in the given path must exist except the last one.
INT 21h / AH= 3Ah - remove directory.
Entry: DS:DX -> ASCIZ pathname of directory to be removed.
Return:
CF is clear if successful, AX destroyed CF is set on error AX = error code.
Notes: directory must be empty (there should be no files inside of it).
23. INT 21h / AH= 3Bh - set current directory.
Entry: DS:DX -> ASCIZ pathname to become current directory (max 64 bytes).
Return:
Carry Flag is clear if successful, AX destroyed.
Carry Flag is set on error AX = error code.
Notes: even if new directory name includes a drive letter, the default drive is not changed,
only the current directory on that drive.
INT 21h / AH= 3Ch - create or truncate file.
entry:
CX = file attributes:
mov cx, 0 ; normal - no attributes.
mov cx, 1 ; read-only.
mov cx, 2 ; hidden.
mov cx, 4 ; system
mov cx, 7 ; hidden, system and read-only!
mov cx, 16 ; archive
DS:DX -> ASCIZ filename.
returns:
CF clear if successful, AX = file handle.
CF set on error AX = error code.
note: if specified file exists it is deleted without a warning.
example:
org 100h
mov ah, 3ch
mov cx, 0
mov dx, offset filename
mov ah, 3ch
int 21h
jc err
mov handle, ax
jmp k
filename db "myfile.txt", 0
handle dw ?
err:
; ....
k:
ret
INT 21h / AH= 3Dh - open existing file.
24. Entry:
AL = access and sharing modes:
mov al, 0 ; read
mov al, 1 ; write
mov al, 2 ; read/write
DS:DX -> ASCIZ filename.
Return:
CF clear if successful, AX = file handle.
CF set on error AX = error code.
note 1: file pointer is set to start of file.
note 2: file must exist.
example:
org 100h
mov al, 2
mov dx, offset filename
mov ah, 3dh
int 21h
jc err
mov handle, ax
jmp k
filename db "myfile.txt", 0
handle dw ?
err:
; ....
k:
ret
INT 21h / AH= 3Eh - close file.
Entry: BX = file handle
Return:
CF clear if successful, AX destroyed.
CF set on error, AX = error code (06h).
INT 21h / AH= 3Fh - read from file.
Entry:
BX = file handle.
CX = number of bytes to read.
DS:DX -> buffer for data.
25. Return:
CF is clear if successful - AX = number of bytes actually read; 0 if at EOF (end of file) before
call.
CF is set on error AX = error code.
Note: data is read beginning at current file position, and the file position is updated after a
successful read the returned AX may be smaller than the request in CX if a partial read
occurred.
INT 21h / AH= 40h - write to file.
entry:
BX = file handle.
CX = number of bytes to write.
DS:DX -> data to write.
return:
CF clear if successful; AX = number of bytes actually written.
CF set on error; AX = error code.
note: if CX is zero, no data is written, and the file is truncated or extended to the current
position data is written beginning at the current file position, and the file position is updated
after a successful write the usual cause for AX < CX on return is a full disk.
INT 21h / AH= 41h - delete file (unlink).
Entry:
DS:DX -> ASCIZ filename (no wildcards, but see notes).
return:
CF clear if successful, AX destroyed. AL is the drive of deleted file (undocumented).
CF set on error AX = error code.
Note: DOS does not erase the file's data; it merely becomes inaccessible because the FAT chain
for the file is cleared deleting a file which is currently open may lead to filesystem corruption.
INT 21h / AH= 42h - SEEK - set current file position.
Entry:
AL = origin of move: 0 - start of file. 1 - current file position. 2 - end of file.
BX = file handle.
26. CX:DX = offset from origin of new file position.
Return:
CF clear if successful, DX:AX = new file position in bytes from start of file.
CF set on error, AX = error code.
Notes:
for origins 1 and 2, the pointer may be positioned before the start of the file; no error is
returned in that case, but subsequent attempts to read or write the file will produce errors. If
the new position is beyond the current end of file, the file will be extended by the next write
(see AH=40h).
example:
org 100h
INT 21h / AH= 47h - get current directory.
Entry:
DL = drive number (00h = default, 01h = A:, etc)
DS:SI -> 64-byte buffer for ASCIZ pathname.
Return:
Carry is clear if successful
Carry is set on error, AX = error code (0Fh)
Notes:
the returned path does not include a drive and the initial backslash.
INT 21h / AH=4Ch - return control to the operating system (stop program).
INT 21h / AH= 56h - rename file / move file.
Entry:
DS:DX -> ASCIZ filename of existing file.
ES:DI -> ASCIZ new filename.
Return:
CF clear if successful.
CF set on error, AX = error code.
Note: allows move between directories on same logical drive only; open files should not be
renamed!
mouse driver interrupts -- INT 33h
INT 33h / AX=0000 - mouse ininialization. any previous mouse pointer is hidden.
returns:
if successful: AX=0FFFFh and BX=number of mouse buttons.
if failed: AX=0
example:
27. mov ax, 0
int 33h
see also: mouse.asm in examples.
INT 33h / AX=0001 - show mouse pointer.
example:
mov ax, 1
int 33h
INT 33h / AX=0002 - hide visible mouse pointer.
example:
mov ax, 2
int 33h
INT 33h / AX=0003 - get mouse position and status of its buttons.
returns:
if left button is down: BX=1
if right button is down: BX=2
if both buttons are down: BX=3
CX = x
DX = y
example:
mov ax, 3
int 33h
; note: in graphical 320x200 mode the value of CX is doubled.
; see mouse2.asm in examples.
==============================================================================================
28. M ng và con tr
Ti p theo L xin gi i thi u sơ v con tr m ng
Ki n th c v con tr thì ã h c r i nên hok nh c l i.
L ch nói v m ng :
*M ng 1 chi u :M ng 1 chi u ch a n ph n t có th bi u di n như sau
a[i] v i i= 0->n-1
n u dùng con tr p bi u di n m ng trên thì
p ch n ph n t u tiên a[o];
t c là *p=a[o];
n u ta c p cho con tr p m t vùng nh liên ti p nhau thì ô nh u tiên ch a[0];
*p=a[o];
*(p+1)=a[1];
*(p+2)=a[2];
...
*(p+n)=a[n];
như v y là ta có th dùng con tr bi u di n m ng m t chi u...
vi c c p phát b nh ng trong c thì có hàm malloc nhưng hàm này gi ít ai xài nên tôi xin dùng c++
c p phát m t vùng nh cho con tr p bi u di n m ng m t chi u n ph n t thì ta c p phát như sau
p=new int(n);
m ng này ki u int
còn xóa thì delete p;
ví d 1: nh p xu t m ng m t chi u dùng con tr
Code:
/*********************************************
Gioi thieu ve con tro mang trong C++
Mang hai chieu A(n,m) co the dung con tro nhu sau
*(a+i*m+j);
Chuong trinh duoc viet tren:
+ ubuntu 8.04
+ CodeBlock version 8.02
+ Bo bien dich gcc
Writed by Nguyen The Linh
**********************************************/
#include<stdio.h>
#include<stdlib.h>
// Khai bao cac ham duoc su dung trong chuong trinh
void input_array(int *p,int n,char name);
void output_array(int *p,int n,char name);
// Chuong trinh chinh
int main()
{
int n,*p;
printf("n-------------------------------------------");
printf("n Moi ban nhap so phan tu cua mang :n =");
scanf("%d",&n);
printf("n-------------------------------------------");
p=new int (n);
input_array(p,n,'P');
29. printf("n-------------------------------------------");
output_array(p,n,'P');
printf("n-------------------------------------------");
delete p;
return EXIT_SUCCESS;
}
// Dinh nghia ham nhap mang mot chieu
void input_array(int *p,int n,char name)
{
printf("n Moi ban nhap mang %c :n",name);
for(int i=0;i<n;i++)
{
printf(" %c[%2d]=",name,i);
scanf("%d",&*(p+i));
}
}
// Dinh nghia ham xuat mang mot chieu
void output_array(int *p,int n,char name)
{
printf("n Xuat mang %c ra man hinh :",name);
for(int i=0;i<n;i++)
{
printf("n %c[%2d]=%3d",name,i,*(p+i));
}
}
Ti p theo là m ng hai chi u
Tương t như m ng m t chi u thôi
ví d m ng hai chi u a[n][m]
t c là m ng trên có n hàng mà m c t
v y có t ng c ng là n*m ph n t
t a[0][0] , a[0][1].....a[n-1][m-1] ;
khi dùng con tr ta s dùng ô nh u tiên ch n ph n t a[0][0];
Gi s ta có m ng th này
1 2 3 4
5 6 7 8
9 10 11 12
M ng này có ba hàng và b n c t
n=3 và m=4
*p =a[0][0] =1;
*(p+1) =a[0][1] =2;
*(p+2) =a[0][2] =3;
*(p+3) =a[0][3] =4;
...
*(p+11) =a[2][3]=12;
nhìn vào b n có th oán ra công th c t ng quát c a a[i][j]
v i i=0->n-1 và j=0->m-1
*(p+i*m+j); v i m là s c t
t c là a[i][j] = *(p+i*m+j);
Ví d 2 v nh p xu t m ng hai chi u dùng con tr .
+ C ng hai m ng
30. + S p x p m ng
+ Chéo hóa m ng
Code:
/*********************************************
Gioi thieu ve con tro mang trong C++
Mang hai chieu A(n,m) co the dung con tro nhu sau
*(a+i*m+j);
Chuong trinh duoc viet tren:
+ ubuntu 8.04
+ CodeBlock version 8.02
+ Bo bien dich gcc
Writed by Nguyen The Linh
**********************************************/
#include<stdio.h>
#include<stdlib.h>
// Khai bao cac ham dung trong chuong trinh
void input(int *p,int n,int m,char name);
void output(int *p,int n,int m,char name);
void output_to_file(int *p,int n,int m);
void plus_array(int *p1,int *p2,int *p,int n,int m);
void sort_array(int *p,int n,int m);
void cheohoa(int *p,int n,int m);
// Chuong trinh chinh
int main()
{
int *p1,*p2,*p,n,m;
printf("--------------------------------------------------n");
printf("n Please input rows :");
scanf("%d",&n);
printf("n Please input Colums :");
scanf("%d",&m);
printf("--------------------------------------------------n");
p1=new int[n*m]; //Cap phat bo nho dong
input(p1,n,m,'A');
output(p1,n,m,'A');
printf("--------------------------------------------------n");
p2=new int[n*m];
input(p2,n,m,'B');
output(p2,n,m,'B');
printf("--------------------------------------------------n");
printf(" Cong hai ma tran A +B = Cn");
p=new int[n*m];
plus_array(p1,p2,p,n,m);
output(p,n,m,'C');
printf("--------------------------------------------------n");
printf(" Sap xep ma tran:n");
sort_array(p,n,m);
output(p,n,m,'C');
printf("--------------------------------------------------n");
printf(" Cheo hoa ma tran:n");
cheohoa(p,n,m);
output(p,n,m,'C');
31. printf("--------------------------------------------------n");
printf(" Output result to file OUTPUT.DAT:n");
output_to_file(p,n,m);
printf(" ......>Xuat thanh congn");
delete p1,p2,p;
printf("--------------------------------------------------n");
return EXIT_SUCCESS;
}
//Khai bao ham nhap mang hai chieu
void input(int *p,int n,int m,char name)
{
printf("n Please input array %c:n",name);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
printf(" %c[%d][%d]=",name,i,j);
scanf("%d",&*(p+i*m+j));
}
}
// Khai bao ham xuat mang hai chieu
void output(int *p,int n,int m,char name)
{
printf("n Ouput array %c to screen:n",name);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
printf("%5d",*(p+i*m+j));
printf("n");
}
}
// Khai bao ham xuat ma tran ra file OUTPUT.DAT
void output_to_file(int *p,int n,int m)
{
FILE *fp;
fp=fopen("OUTPUT.dat","wt");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
fprintf(fp,"%5d",*(p+i*m+j));
fprintf(fp,"n");
}
fclose(fp);
}
// Khai bao ham cong hai mang p1 va p2 thanh mang p
void plus_array(int *p1,int *p2,int *p,int n,int m)
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
*(p+i*m+j)=*(p1+i*m+j)+*(p2+i*m+j);
}
// Khai bao ham sap xep mang tang dan
void sort_array(int *p,int n,int m)
32. {
int temp;
for(int i=0;i<=n*m-2;i++)
{
for(int j=i;j<=n*m-1;j++)
{
if(*(p+i) > *(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}
}
}
// Cheo hoa ma tran
void cheohoa(int *p,int n,int m)
{
float heso;
int temp,temp1,temp2;
for(int i=0;i<n;i++)
{
//********************************************
//Loai bo cac phan tu a[i][i]=0
if(*(p+i*m+i)==0)
for(int j=i+1;j<n;j++)
{
if(*(p+j*m+i)!=0)
{
for(int k=0;k<m;k++)
{
temp=*(p+i*m+k);
*(p+i*m+k)=*(p+j*m+k);
*(p+j*m+k)=temp;
}
}
}
//cheo hoa ma tran
//********************************************
for(int j=i+1;j<n;j++)
{
temp1=*(p+i*m+i);
temp2=*(p+j*m+i);
heso= (float)-temp2/temp1;
for(int k=0;k<m;k++)
{
*(p+j*m+k) += (*(p+i*m+k))*heso;
}
}
}
}
33. /*************************************************************** */
------------------------------------------------------------------------------------------------------------------
Cu i cùng là m t bài C chuy n i qua l i gi a các h m không dùng m ng các b n
tham kh o:
/*********************************************
Chuyen doi qua lai giua cac he dem
Chuong trinh duoc viet tren:
+ ubuntu 8.04
+ CodeBlock version 8.02
+ Bo bien dich gcc
Writed by Nguyen The Linh
**********************************************/
#include<stdio.h>
#include<stdlib.h>
// Khai bao cac ham dung trong chuong trinh
unsigned menu();
void error(unsigned i);
void thapphan_sang_nhiphan(unsigned n);
void thapphan_sang_tuphan(unsigned n);
void thapphan_sang_batphan(unsigned n);
void thapphan_sang_thaplucphan(unsigned n);
unsigned getbit(unsigned n,unsigned i);
unsigned getnum(unsigned n,unsigned j,unsigned i);
void input(unsigned &n);
// Chuong trinh chinh
int main()
{
unsigned router,n;
printf("n----------------------------------------------");
printf("n Day la chuong trinh chuyen doi qua lai giua cac he dem dung
dich bit.");
do
{
printf("n----------------------------------------------");
router=menu();
printf("n----------------------------------------------");
switch(router)
{
printf("n----------------------------------------------");
case 1:
printf("n Ham nay se chuyen mot so thap phan sang nhi
phan.");
printf("n");
input(n);
thapphan_sang_nhiphan(n);
break;
printf("n----------------------------------------------");
case 2:
printf("n Ham nay se chuyen mot so thap phan sang tu phan.");
printf("n");
34. input(n);
thapphan_sang_tuphan(n);
break;
printf("n----------------------------------------------");
case 3:
printf("n Ham nay se chuyen mot so thap phan sang bat
phan.");
printf("n");
input(n);
thapphan_sang_batphan(n);
break;
printf("n----------------------------------------------");
case 4:
printf("n Ham nay se chuyen mot so thap phan sang thap luc
phan.");
printf("n");
input(n);
thapphan_sang_thaplucphan(n);
break;
printf("n----------------------------------------------");
case 0:
printf("n Bibi See you again");
break;
printf("n----------------------------------------------");
default:
error(1);
}
}while(router!=0);
}
// Dinh nghia ham chuyen tu thap phan sang nhi phan
void thapphan_sang_nhiphan(unsigned n)
{
printf("n");
unsigned sobit=sizeof(n)*8;
printf(" %u = ",n);
for(int i=sobit-1;i>=0;i--)
printf("%u",getbit(n,i));
}
// Dinh nghia ham chuyen tu thap phan sang tu phan
void thapphan_sang_tuphan(unsigned n)
{
printf(" %u = ",n);
for(int i=15;i>=0;i--)
printf("%u",getnum(n,i*2+1,i*2));
}
// Dinh nghia ham chuyen tu thap phan sang bat phan
void thapphan_sang_batphan(unsigned n)
{
printf(" %u = ",n);
for(int i=10;i>=0;i--)
printf("%u",getnum(n,i*3+2,i*3));
}
35. // Dinh nghia ham chuyen tu thap phan sang thap luc phan
void thapphan_sang_thaplucphan(unsigned n)
{
printf(" %u = ",n);
for(int i=7;i>=0;i--)
{
char temp=getnum(n,i*4+3,i*4);
if(temp>9)
temp+=55;
else
temp+=48;
printf("%c",temp);
}
}
// Dinh nghia menu cua chuong trinh chinh
unsigned menu()
{
unsigned router;
printf("n Moi ban chon menu:");
printf("n 1.Chuyen tu thap phan sang nhi phan.");
printf("n 2.Chuyen tu thap phan sang tu phan.");
printf("n 3.Chuyen tu thap phan sang bat phan.");
printf("n 4.Chuyen tu thap phan sang thap luc phan.");
printf("n 0.Thoat");
printf("n--->Router=");
scanf("%u",&router);
return router;
}
// Dinh nghia ham thong bao loi
void error(unsigned i)
{
switch(i)
{
case 1:
printf("n Khong co chuc nang nay.Moi ban chon lai.");
break;
default:
printf("n Khong biet loi chi ca ");
}
}
// Dinh nghia ham lay bit thu i
unsigned getbit(unsigned n,unsigned i)
{
return (n>>i)&1;
}
// Dinh nghia ham lay gia tri tu bit thu j toi bit thu i(tu trai sang
phai);
unsigned getnum(unsigned n,unsigned j,unsigned i)
{
unsigned sobit;
sobit=sizeof(n)*8-1-j;
n<<=sobit;
36. n>>=sobit;
n>>=i;
return n;
}
// Dinh nghia ham nhap
void input(unsigned &n)
{
printf("n Moi ban nhap mot so thap phan :");
scanf("%u",&n);
}
==========================================================================