You are on page 1of 32

Mục lục

1. Nhập 2 số, xuất số lớn nhất, bé nhất


2. Nhập 1 ký tự, in 3 ký tự tiếp theo
3. Nhập 1 ký tự, in ký tự trước và sau
4. Nhập mảng, nhập vị trí chèn 1 pt, nhập giá trị pt cần chèn
5. Nhập, xuất mảng 1 chiều
6. Nhập, xuất mảng 1 chiều, sắp xếp tăng dần
7. Nhập mảng, tính tổng các phần tử lẻ trong mảng
8. Xét số nguyên tố
9. Đếm số lần xuất hiện ký tự nào đó trong chuỗi
10. Nhập 4 số sắp xếp theo thứ tự tăng dần

;1/ So lon nhat, be nhat giua 2 so


; su dung macro
.model small

max2so macro d1,d2


; al=max (d1, d2)

mov al, d1
cmp al, d2
ja thoi1
mov al,d2
thoi1:

endm
min2somacro d1,d2
; ax=min (d1, d2)
mov al, d1
cmp al, d2
jb thoi2
mov al,d2
thoi2:
endm
.stack 100h
.data
x db ?
y db ?
z db ?
tb1 db 10,'Nhap x=$'
tb2 db 10,'Nhap y=$'
tb3 db 10,'Nhap z=$'
tb4 db 10,'max:$'
tb5 db 10,'min:$'
.code
; lay dia chi vung du lieu
mov ax, @data
mov ds, ax
; xuat thong bao 1
lea dx, tb1
call xuattb
; nhap x
call nhap1so
mov x, al
; xuat thong bao 2
lea dx, tb2
call xuattb
; nhap y
call nhap1so
mov y, al
; xuat thong bao 3
lea dx, tb3
call xuattb
; nhap z
call nhap1so
mov z, al
; xuat thong bao 4
lea dx, tb4
call xuattb
; tim max
max2so x,y
max2so x,z

mov dl, al
call xuat1kytu
; xuat thong bao 5
lea dx, tb5
call xuattb

;tim min
min2sox,y
min2sox,z
mov dl, al
call xuat1kytu
;tro ve dos
mov ah, 4ch
int 21h
;================================================
;----------nhap 1 so---------------------
nhap1so proc
mov ah, 1
int 21h
ret
nhap1so endp
;----------xuat thong bao--------------------
xuattb proc
mov ah, 9h
int 21h
ret
xuattb endp
xuat1kytu proc
mov ah, 2
int 21h
ret
xuat1kytu endp
end
;2/ Nhap vao 1 ky tu roi in ra 3 ky tu tiep sau
.MODEL SMALL
.STACK 100h
.DATA
Mes1 DB 10,13,'Nhap vao 1 ky tu : $'
Mes2 DB 10,13,'Ket qua : $'
Mes3 DB 10,13,'Nhap vao ky tu co ma ASCII khong thich hop$'
UserChar DB ?

.CODE
start :
MOV AX,@DATA
MOV DS,AX
; Thong bao nhap vao 1 ky tu
MOV AH,9
LEA DX,Mes1
INT 21h

; Nhap 1 ky tu
MOV AH,1
INT 21h

CMP AL,252
JA NhapSai
MOV UserChar,AL

; Xuat ra thong bao ve ket qua


MOV AH,9
LEA DX,Mes2
INT 21h

; Xuat 3 ky tu
MOV CX,3
MOV DL,UserChar
Lap :
INC DL
MOV AH,2
INT 21h
LOOP Lap
JMP KetThuc

NhapSai :
MOV AH,9
LEA DX,Mes3
INT 21h

KetThuc :
; Ket thuc
MOV AH,4Ch
INT 21h
END start
;3/ Nhap 1 ky tu, in ra ky tu truoc va sau
.MODEL SMALL
.STACK 100h

.DATA
;CHUOI PHAI CO DAU $ DE KET THUC
MESS DB 13, 10, "CHAO BAN!", 13, 10, "$"
MESS1 DB 13,10,"NHAP MOT KY TU DI BAN !$"
MESS2 DB 13,10,"KY TU TRUOC KY TU BAN DA NHAP KY TU SAU $"
XDONG DB 13,10," $"
SPACE DB " $"
KYTU DB ? ;CHUA BIET KY TU NHAP

.CODE
;DUA DIA CHI SEGMENT DATA VAO DS, PHAI QUA TRUNG GIAN AX
MOV CX, @DATA
MOV DS, CX

;HIEN THI MESS


MOV AH, 9
LEA DX, MESS ;MOV DX, OFFSET MESS
INT 21h

;HIEN THI MESS1


MOV AH, 9
LEA DX, MESS1
INT 21h

;CHO NHAP 1 KY TU ROI DUA DU LIEU VAO AL


MOV AH, 1
INT 21h

;DUA KY TU TRONG AL DA NHAP VAO KYTU


MOV KYTU, AL

;HIEN THI MESS2


MOV AH, 9
LEA DX, MESS2
INT 21h

;XUONG DONG
LEA DX, XDONG
INT 21H

;KYTU = KYTU - 1
DEC KYTU

;XUAT KY TU TRUOC
MOV AH, 2
MOV DL, KYTU
INT 21h

;XUAT KHOANG TRANG


MOV AH, 9
LEA DX, SPACE
INT 21h

;KYTU = KYTU + 1
ADD KYTU, 1

;XUAT KY TU VUA NHAP TRONG DL


MOV AH, 2
MOV DL, KYTU
INT 21h

;XUAT KHOANG TRANG


MOV AH, 9
LEA DX, SPACE
INT 21h

ADD KYTU,1

;XUAT KY TU VUA NHAP TRONG DL


MOV AH, 2
MOV DL, KYTU
INT 21h

;XUONG DONG
MOV AH, 9
LEA DX, XDong
INT 21h

;KET THUC CHUONG TRINH VA TRA DIEU KHIEN VE CHO HDH


MOV AH, 4Ch
INT 21h

END
;Nhap chuoi, doi chuoi sang so
title Chuong trinh doi tu chuoi sang so
.model small
.stack 100h
.data
string DB 10 DUP(?),'$'

kq DB 10 DUP(?),'$'
msg1 DB 'Nhap vao mot chuoi gom cac ky so:','$'
msg2 DB 0dh,0ah,'So la:','$'
endl DB 0dh,0ah,'$'
.code
main PROC
mov dx,@data
mov ds,dx
lea dx,msg1 ;thong bao nhap chuoi
mov ah,9
int 21h

lea di,string ;chuoi nhap vao la string do di tro toi


call get_string

lea dx,msg2 ; thong bao se viet so ra man hinh


mov ah,9
int 21h

lea di,string ;doi chuoi string do di tro toi


; ra so trong chua trong ax
call str2num ;goi ham doi chuoi thanh so...

lea di,kq ;doi so trong ax ra chuoi ASCII


;tro toi boi di
call num2str

lea dx,kq ;viet ket qua vua doi ra man hinh


mov ah,9
int 21H
;thoat chuong trinh
mov ax,4c00h
int 21h
main ENDP

;************************************************************************
;Chuong trinh con doi tu so chua trong ax sang chuoi tro den boi DI
; in : di =offset chuoi
; ax =so can doi
; out: khong co(chuoi van do di tro toi)
;************************************************************************
num2str PROC
push ax
push bx
push cx
push dx
push di

mov bx,10 ;So bi chia


mov cx,0 ;dung lam bien dem de push va pop tu stack
lap:
mov dx,0 ;Luon luon phai gan lai dx sau moi lenh chia
div bx ;AX=AX/10
;Thuong : AX,Du: DX
inc cx
push dx
cmp ax,0
jne lap

;bay gio cx chua so chu so cua chuoi ASCII bieu dien so


;di dang tro den dau chuoi
thoat:
pop dx
and dx,0FFh ; lay 4 bits thap
or dl,30h ; doi chu so ra ASCII
mov [di],dl ; luu chu so vao vi tri tuong ung
inc di
loop thoat

inc di
mov byte ptr[di],'$'
pop di
pop dx
pop cx
pop bx
pop ax
ret
num2str ENDP
;************************************************************************
; lay chuoi tu ban phim bang cach nhap tung ky tu
; in :di =offset chuoi
;************************************************************************
get_string PROC
push ax
mov ah,1
get_char:
int 21h
cmp al,0dh
je end_of_line
mov [di],al
inc di
jmp get_char

end_of_line:
mov byte ptr[di],'$'
pop ax
ret
get_string ENDP
;************************************************************************
;Chuong trinh con chuyen tu chuoi thanh so chua trong ax
; in :di =offset chuoi
; out :ax =so da doi
;************************************************************************
str2num PROC
push bx
push cx
push dx

mov ax,0
mov bx,10 ;So nhan
nhan:
mov dx,0
push ax ;luu ket qua
mov ah,0 ;ax = ky tu hien tai cua chuoi
mov al,[di] ;
cmp al,'$' ;toi cuoi chuoi chua ?
je laytri ;da het chuoi ,thoat ,lay tri trong ax ra
sub al,30h ;doi ky tu ra so
mov cx,ax ;Cat AX chua so tam thoi trong CX
pop ax ;lay lai ket qua da luu
mul bx ; ax:=ax * 10;
add ax,cx ; ax:=ax +chu so hien tai
inc di ;DI tro den vi tri tiep theo
jmp nhan
laytri:
pop ax

pop dx
pop cx
pop bx
ret
str2num ENDP
;************************************************************************
END main
;4/ Nhap mang, nhap vi tri chen 1 pt, nhap gia tri pt cần chèn
.Model Small
.Stack 100H
.Data
msg_00 DB 'So phan tu trong mang : $'
msg_01 DB 'Mang vua nhap vao :',10,13,'$'
msg_02 DB 10,13,'Vi tri muon chen vao trong mang : $'
msg_03 DB 10,13,'Gia tri muon chen : $'
msg_04 DB 10,13,'Mang sau khi chen vao : ',10,13,'$'
space DB ' $'
sign1 DB 'A [ $'
sign2 DB ' ] = $'
CR_LF DB 10,13,'$'
array DW 20 DUP(0)
index DW 1
n DW 0
p DW 0
.Code
MAIN Proc
mov AX, @Data
mov DS, AX
lea DX, msg_00
call IN_CHUOI
call NHAP_SO
mov CX, AX
lea BX, array
push CX
@@NHAP_MANG :
lea DX, CR_LF
call IN_CHUOI
lea DX, sign1
call IN_CHUOI
mov AX, index
call IN_SO
lea DX, sign2
call IN_CHUOI
call NHAP_SO
mov [BX], AX
add BX, 2
inc index
loop @@NHAP_MANG

lea DX, CR_LF


call IN_CHUOI
pop CX
lea DX, msg_01
call IN_CHUOI
lea BX, array
push CX
@@IN_MANG :
mov AX, [BX]
call IN_SO
lea DX, space
call IN_CHUOI
add BX, 2
loop @@IN_MANG

lea DX,msg_02
call IN_CHUOI
call NHAP_SO
mov p, AX
lea DX, msg_03
call IN_CHUOI
call NHAP_SO
mov n, AX

lea SI, array


pop CX
push CX
;==================================
sub CX, p
@@DICH_CHUYEN :
mov AX, [SI]
mov [SI+2], AX
sub SI, 2
loop @@DICH_CHUYEN

pop CX
inc CX
push CX
lea BX, array
@@CHEN :
cmp BX, p
add BX, 2
jne @@CHEN
mov AX, n
mov [BX], AX

lea DX, msg_04


call IN_CHUOI
lea BX, array
pop CX
@@IN_LAI:
mov AX, [BX]
call IN_SO
lea DX, space
call IN_CHUOI
add BX, 2
loop @@IN_LAI

xor AH, AH
int 16H
mov AX, 4C00H
int 21H
MAIN Endp

NHAP_SO Proc
push BX
push CX
push DX
xor CX, CX
mov BX, 10
@@NHAP :
mov AH, 01H
int 21H
cmp AL, 0DH
je @@THOI_NHAP
sub AL, 30H
xor AH, AH ; che AH di
xchg AX, CX
mul BX
add CX, AX
jmp @@NHAP
@@THOI_NHAP :
mov AX, CX
pop DX
pop CX
pop BX
ret
NHAP_SO Endp

IN_SO Proc
push AX
push BX
push CX
push DX
xor CX, CX ; so lan dua vao ngan xep
mov BX, 10
@@DOI_SO :
xor DX, DX
div BX
add DL, 30H
push DX
inc CX
and AX, AX
jnz @@DOI_SO
@@IN_RA :
pop AX
mov AH, 0EH
int 10H
loop @@IN_RA
pop DX
pop CX
pop BX
pop AX
ret
IN_SO Endp

IN_CHUOI Proc
push AX
mov AH, 09H
int 21H
pop AX
ret
IN_CHUOI Endp
END MAIN
; 5/ nhap - xuat mang 1 chieu
.Model Small
.Stack 100H
.Data
msg_ask DB 'So phan tu cua mang : $'
msg_ans DB 'Mang vua nhap vao : ',10,13,'$'
msg_ind1 DB 'A[ $'
msg_ind2 DB ' ] = $'
CRLF DB 10,13,'$'
array DW 20 DUP(?)
index DW 0
n DW 0
.Code
MAIN Proc
mov AX, @Data
mov DS, AX ; nap data vao DS
lea DX, msg_ask
call PRNT_STR
call SCAN_INT
lea DX, CRLF
call PRNT_STR
mov CX, AX
mov n, AX
mov BX, offset array
mov index, 0

LOOP_WHILE@1 : ; get data


lea DX, msg_ind1
call PRNT_STR
mov AX, index
call PRNT_INT
lea DX, msg_ind2
call PRNT_STR
call SCAN_INT
mov [BX], AX
inc index
add BX, 2
lea DX, CRLF
call PRNT_STR
loop LOOP_WHILE@1

lea DX, msg_ans


call PRNT_STR
mov BX, offset array
mov CX, n
LOOP_WHILE@2 :
mov AX, [BX]
call PRNT_INT
mov AH, 02H
mov DL, 20H
int 21H
add BX, 2
loop LOOP_WHILE@2

mov AH, 00H


int 16H
mov AH, 4CH
int 21H

MAIN Endp

SCAN_INT Proc ; get an integer number and put to AX


push BX
push CX
push DX
push SI
mov BX, 10
xor CX, CX
mov SI, CX

@@L1 : mov AH, 01H


int 21H
cmp AL, 0DH
je @@L3
cmp AL, 2DH
jne @@L2
inc SI
jmp @@L1

@@L2 : xor AH, AH


sub AL, 30H
xchg AX, CX
mul BX
add CX, AX
jmp @@L1

@@L3 : and SI, SI


jz @@L4
neg CX

@@L4 : mov AX, CX


pop SI
pop DX
pop CX
pop BX
ret
SCAN_INT Endp

PRNT_INT Proc ; print an integer number in AX


push AX
push BX
push CX
push DX
mov BX, 10
xor CX, CX
and AX, AX
jns @@P1
push AX
mov AL, 2DH
mov AH, 0EH
int 10H
pop AX
neg AX

@@P1 : xor DX, DX


div BX
add DX, 30H
push DX
inc CX
and AX, AX
jnz @@P1

@@P2 : pop AX
mov AH, 0EH
int 10H
loop @@P2
pop DX
pop CX
pop BX
pop AX
ret
PRNT_INT Endp

PRNT_STR Proc
push AX
mov AH, 09H
int 21H
pop AX
ret
PRNT_STR Endp
END MAIN
;6/ nhap - xuat mang 1 chieu
; sap xep mang tang dan
.Model Small
.Stack 100H
.Data
msg_ask DB 'So phan tu cua mang : $'
msg_ans DB 'Mang vua nhap vao : ',10,13,'$'
msg_srt DB 10,13,'Mang sau khi da sap xep : ',10,13,'$'
msg_ind1 DB 'A[ $'
msg_ind2 DB ' ] = $'
CRLF DB 10,13,'$'
array DW 20 DUP(?)
index DW 0
n DW 0
.Code
MAIN Proc
mov AX, @Data
mov DS, AX
lea DX, msg_ask
call PRNT_STR
call SCAN_INT
lea DX, CRLF
call PRNT_STR
mov CX, AX
mov n, AX
mov BX, offset array
mov index, 0

LOOP_WHILE@1 : ; get data


lea DX, msg_ind1
call PRNT_STR
mov AX, index
call PRNT_INT
lea DX, msg_ind2
call PRNT_STR
call SCAN_INT
mov [BX], AX
inc index
add BX, 2
lea DX, CRLF
call PRNT_STR
loop LOOP_WHILE@1

lea DX, msg_ans


call PRNT_STR
mov BX, offset array
mov CX, n

LOOP_WHILE@2 :
mov AX, [BX]
call PRNT_INT
mov AH, 02H
mov DL, 20H
int 21H
add BX, 2
loop LOOP_WHILE@2

lea BX, array


mov AX, n
mov index, AX
dec index
@@SORT1 :
mov CX, n
lea BX, array
dec CX
@@SORT2 :
mov AX, [BX]
mov DX,[BX+2]
cmp AX, DX
jl @@SORT3
mov [BX], DX
mov [BX+2], AX
@@SORT3 :
add BX, 2
loop @@SORT2
dec index
jnz @@SORT1

lea DX, msg_srt


call PRNT_STR
mov CX, n
lea BX, array
@@PRNT1: mov AX, [BX]
call PRNT_INT
mov AH, 02H
mov DL, 20H
int 21H
add BX, 2
loop @@PRNT1

mov AH, 00H


int 16H
mov AH, 4CH
int 21H

MAIN Endp

SCAN_INT Proc ; get an integer number and put to AX


push BX
push CX
push DX
push SI
mov BX, 10
xor CX, CX
mov SI, CX

@@L1 : mov AH, 01H


int 21H
cmp AL, 0DH
je @@L3
cmp AL, 2DH
jne @@L2
inc SI
jmp @@L1

@@L2 : xor AH, AH


sub AL, 30H
xchg AX, CX
mul BX
add CX, AX
jmp @@L1

@@L3 : and SI, SI


jz @@L4
neg CX

@@L4 : mov AX, CX


pop SI
pop DX
pop CX
pop BX
ret
SCAN_INT Endp

PRNT_INT Proc ; print an integer number in AX


push AX
push BX
push CX
push DX
mov BX, 10
xor CX, CX
and AX, AX
jns @@P1
push AX
mov AL, 2DH
mov AH, 0EH
int 10H
pop AX
neg AX

@@P1 : xor DX, DX


div BX
add DX, 30H
push DX
inc CX
and AX, AX
jnz @@P1

@@P2 : pop AX
mov AH, 0EH
int 10H
loop @@P2
pop DX
pop CX
pop BX
pop AX
ret
PRNT_INT Endp
PRNT_STR Proc
push AX
mov AH, 09H
int 21H
pop AX
ret
PRNT_STR Endp

END MAIN
;7/ Nhap mang va tinh tong cac pt le
.Model Small
.Stack 100H
.Data
msg_00 DB 'So phan tu cua mang : $'
msg_01 DB 10,13,'Tong cac phan tu le : $'
msg_02 DB 'A [ $'
msg_03 DB ' ] = $'
msg_04 DB 'Mang vua nhap vao : ',10,13,'$'
CR_LF DB 10,13,'$'
SPACE DB ' ','$'
a DW 10 DUP(0) ; mang toi da co 10 phan tu
i DW 1
n DW 0
.Code
MAIN Proc
mov AX, @Data
mov DS, AX
mov DX, offset msg_00
call IN_CHUOI
call LAY_SO
mov n, AX ; n chua so phan tu cua mang
mov CX, AX ;
lea BX, a ; SI tro vao mang a
@@NHAP_MANG :
lea DX, CR_LF
call IN_CHUOI
lea DX, msg_02
call IN_CHUOI
mov AX, i
call IN_SO
lea DX, msg_03
call IN_CHUOI
call LAY_SO
mov [BX], AX
add BX, 2
inc i
loop @@NHAP_MANG
lea DX, CR_LF
call IN_CHUOI
lea BX, a
mov CX, n
@@IN_MANG :
mov AX, [BX]
call IN_SO
lea DX, SPACE
call IN_CHUOI
add BX, 2
loop @@IN_MANG
lea DX, msg_01
call IN_CHUOI
lea BX, a
mov CX, n
xor AX, AX
@@TINH_TONG :
mov DX, [BX]
shr DX, 1 ; kiem tra xem co phai so le khong
jnc @@TIEP ; khong dung - bo qua
add AX, [BX] ; dung - cong vao AX
@@TIEP :
add BX, 2
loop @@TINH_TONG
call IN_SO ; AX chua tong
mov AH, 00H
int 16H
mov AH, 4CH
int 21H
MAIN Endp

IN_CHUOI Proc
; vao DX chua dia chi lech cua chuoi
push AX
mov AH, 09
int 21H
pop AX
ret
IN_CHUOI Endp
IN_SO Proc
; in so trong AX
push AX
push BX
push CX
push DX
xor CX, CX ; CX chua so lan dua vao ngan xep
mov BX, 10 ; BX chua so hang bi chia
@@DOI_SO :
xor DX, DX
div BX
add DX, 30H
push DX
inc CX
and AX, AX ; xep phan du = 0 chua
jnz @@DOI_SO ; neu chua thi lam tiep
@@IN_RA :
pop AX
mov AH, 0EH
int 10H
loop @@IN_RA
pop DX
pop CX
pop BX
pop AX
ret
IN_SO Endp

LAY_SO Proc
; nhan mot so nguyen duong tu ban phim
; va luu trong AX
push BX
push CX
push DX
mov BX, 10
xor CX, CX
@@LAP :
mov AH, 01
int 21H
cmp AL, 0DH ; xem co phai Enter khong ?
je @@DUNG ; dung Enter - dung lai
xor AH, AH
sub AL, 30H ; khong dung - doi AL tu dang ASCII sang so
xchg AX, CX ; dua so vua nhap vao CL,
mul BX ; nhan AL cho 10 roi cong
add CX, AX ; voi gia tri truoc do - dat vao CX
jmp @@LAP
@@DUNG :
mov AX, CX
pop DX
pop CX
pop BX
ret
LAY_SO Endp
END MAIN
;8/ Xet so nguyen to
.MODEL SMALL
.STACK 1024
.DATA
n dw ?
msg0 db 13,10,"$"
msg1 db "Nhap mot so :","$"
msg2 db " La nguyen to","$"
msg3 db " Khong la nguyen to","$"
.CODE
proc main
call LayDC
;nhap so xong xuong dong
mov dx,offset msg1
call XuatChuoi
call NhapSo
mov n,ax
mov dx,offset msg0
call XuatChuoi

;ktra co la nguyento
mov ax,n
call LaNguyenTo

;ax=0 -> khong la nguyen to


cmp ax,0
je Khong_Nt
;ax=1 -> la nguyen to
mov ax,n
call XuatSo
mov dx,offset msg2
call XuatChuoi
jmp Thoat

Khong_Nt:
mov ax,n
call XuatSo
mov dx,offset msg3
call XuatChuoi

Thoat:

call ThoatDos
endp main

;==============CHUONG TRINH CON================

proc LayDC
mov ax,@data
mov ds,ax
ret
endp LayDC

proc ThoatDos
mov ah,4Ch
int 21h
ret
endp ThoatDos

proc XuatChuoi
mov ah,9
int 21h
ret
endp XuatChuoi

proc NhapKtu
mov ah,1
int 21h
ret
endp NhapKtu

proc XuatKtu
mov ah,2
int 21h
ret
endp XuatKtu

proc NhapSo
.data
tam dw ?
.code
mov tam,0 ;neu khong co lenh nay lan nhap thu 2 tam lay gtri
lan nhap truoc de thuc hien tiep
mov dx,0 ;
mov bx,10 ;so bi nhan

Nhap:
;tam*10 vd: tam=2 -> tam=20
mov ax,tam
mul bx
mov tam,ax

;nhap so ke al=3
call NhapKtu
cmp al,13 ;so sanh ktu nhap co la enter
je ThoatNhap ;neu bang thi nhay xuong
thoatnhap

;cong do^`n tam=13


sub al,'0' ;doi ktu thanh so
mov ah,0 ;tam la dw -> dung thanh ax
ma ktu la db -> ep
add tam,ax
jmp Nhap

ThoatNhap:
mov ax,tam
div bx ;ax:bx (ax:10)

ret
endp NhapSo

proc XuatSo
.data
tam2 dw 0
dem db 0
.code
mov tam2,ax
BoVaoStack:
;chia tam2 cho 10 , lay phan du bo vao stack
inc dem ;dem so lan thuc hien
mov ax,tam2
mov bx,10
mov dx,0
div bx ;sau khi chia tam2
cho 10 : ax=phan nguyen , dx=phan du
mov tam2,ax
push dx

;so sanh da het chua , nhay neu tren


cmp tam2,0
ja BoVaoStack

Layra:
pop dx
add dl,'0' ;doi so thanh ktu
call XuatKtu
dec dem

cmp dem,0
ja Layra
ret
endp XuatSo

proc LaNguyenTo
.data
lsnt_i dw ?
lsnt_n dw ?
.code
mov lsnt_n,ax
mov lsnt_i,2

cmp lsnt_n,1
ja LSNT_lap
mov ax,0
ret

LSNT_lap:
mov ax,lsnt_n
shr ax,1 ;ax=n/2
cmp lsnt_i,ax
ja LSNT_thoat_lap ;lon hon -> thoat
mov dx,0
mov ax,lsnt_n
div lsnt_i
cmp dx,0
jne LSNT_skip_if ;<>0 ->nhay

mov ax,0
ret ;==0 -> thoat khoi ctrinh
LSNT_skip_if:
inc lsnt_i
jmp LSNT_lap
LSNT_thoat_lap:
mov ax,1
ret
endp LaNguyenTo

END
;9/ Dem so lan xuat hien cua ky tu nao do trong chuoi
.model small
.stack
.data
tb1 db 10,13,'Nhap vao mot chuoi :$'
tb2 db 10,13,'Nhap ky tu can tim :$'
tb3 db 10,13,'So lan xuat hien cua ky tu la :$'
chuoi db 80
db ?
db 80 dup (?)
dem dw ?

.code
mov ax,@data
mov ds,ax

mov ah,9
mov dx,offset tb1
int 21h

;nhap chuoi
mov dx,offset chuoi
mov ah,0Ah
int 21h

;Them ky tu $ vao cuoi chuoi


xor bx,bx
mov bl,chuoi+1
mov byte ptr chuoi[2][bx],'$'

;xuat thong bao nhap ky tu


mov dx,offset tb2
mov ah,9
int 21h

mov ah,1
int 21h

xor bx,bx
mov dem,0
lap:
cmp chuoi[2][bx],al
je demkytu
cmp chuoi[2][bx],'$'
je ketthuc

inc bx
loop lap

demkytu:
inc dem
inc bx
jmp lap
ketthuc:

;xuat thong bao ket qua


mov dx,offset tb3
mov ah,9
int 21h

mov bx,10
mov ax,dem
xor cx,cx
Chia:
xor dx,dx
div bx
push dx
inc cx
or ax,ax
jz Xuat
jmp Chia

Xuat:
mov ah,2
pop dx
or dl,30h ;'0'
int 21h
dec cx
or cx,cx
jnz Xuat

mov ah,4Ch
int 21h
end
;10/ Nhap 4 so sap xep thu tu tang dan
;------------------------------------
.MODEL SMALL
.STACK
.DATA
Msg1 DB 13,10,'Nhap so $'
Msg2 DB 13,10,'Mang sau khi sap: $'
So1 DW ?
So2 DW ?
So3 DW ?
So4 DW ?

.CODE
;xoa man hinh
MOV AH,3
INT 10h
;xuat thong bao nhap
MOV AX,@DATA
MOV DS,AX
XOR BX,BX

MOV CL,'1'
CALL Nhap
MOV So1,BX

MOV CL,'2'
CALL Nhap
CMP BX,So1
MOV So2,BX

MOV CL,'3'
CALL Nhap
MOV So3,BX

MOV CL,'4'
CALL Nhap
MOV So4,BX

Ve_DOS:
MOV AH,4Ch
INT 21h
;-----------------------------------------
;Nhap so va luu gia tri vao BX
;-----------------------------------------
Nhap PROC
PUSH AX DX
MOV BX,0
MOV DX,OFFSET Msg1
MOV AH,9
INT 21h
MOV AH,2
MOV DL,CL
INT 21h
MOV DL,':'
INT 21h
;nhap so thap phan
lap:
MOV AH,8
INT 21h
CMP AL,13
JE End_lap
;xuat so ra man hinh
MOV AH,2
MOV DL,AL
INT 21h
;doi thanh tri va dua vao SI
AND AL,0Fh
CBW
MOV SI,AX
;BX=BX*10+SI
MOV DX,0
MOV AX,10
MUL BX
MOV BX,SI
ADD BX,AX
JMP Lap
End_lap:
POP AX DX
RET
Nhap ENDP
;-----------------------------------------
;Xuat so he 10
;-----------------------------------------
XuatSo PROC
PUSH BX CX DX

MOV CX,0 ; khoi tao bien dem


MOV BX,10
For_1:
MOV DX,0
DIV BX
PUSH DX
INC CX
CMP AX,0
JNZ For_1
;xuat so he 10
MOV AH,2
MOV DL,' '
INT 21h
MOV AH,2
For_2:
POP DX
OR DL,30h
INT 21H
LOOP For_2

POP BX CX DX
RET
XuatSo ENDP
END

You might also like