Chương trình sử dụng trình dịch emu8086.
Mã nguồn ASM:
;=================================================================
;Bai 2: TINH TONG 2 SO THAP PHAN CO 4 CHU SO
;VAN DE O CHO: PHAI NHAP VAO 2 SO O DANG MA ASCII TREN BAN PHIM,
;DE TINH TOAN DUOC TA PHAI CHUYEN SANG MA NHI PHAN.
;BANG CACH: DUNG 1CTC NHAPSO
;SAU KHI CO 2 SO DANG NHI PHAN THI OK. TUONG TU BAI 1.
;=================================================================
.model small
.stack 100
.data
tong db 5 dup(0),'$' ;Chu y: tong o dau tien
tb1 db 'nhap so thu nhat: $'
tb2 db 13,10,'nhap so thu hai: $'
tb3 db 13,10,'tong hai so la: $'
so1 dw 0
so2 dw 0
.code
main proc
mov ax,@data
mov ds,ax
mov ah,9
lea dx,tb1
int 21h ;goi loi thong bao nhap
call nhapso
mov so1,bx
mov ah,9
lea dx,tb2
int 21h
call nhapso
mov so2,bx
mov ax,so1
add ax,so2 ;dung ax chua tong 2 so
lea si,tong ;si tro vao dia chi lech cua 'tong'
add si,4
mov cx,5 ;lap lai 5 lan chia,chuyen so sang chuoi: 'tong': tong 2 so 4 chu so co the la 5 chu so.
tach:
mov dx,0 ;o lenh DIV BX: goc BX la 16bit=>DXAX/BX<=>AX/BX
mov bx,10
div bx ;DXAX/BX: AX<-thuong , DX<-so du
add dx,30h ;doi ra ma ASCII
mov [si],dl ;dl -> thanh ghi tong
dec si ;si<- si-1
loop tach
mov ah,9 ;in ra thong bao va 'tong'
lea dx,tb3
int 21h
lea dx,tong
int 21h
mov ah,4ch
int 21h
main endp
;HAM NHAP**********************************
nhapso proc
mov bx,0 ;chua so nhap vao
mov cx,4 ;lap lai 4 lan luu 4 chu so
TIEP:
;nhap AX
mov ah,1
int 21h ;doc 1 ky tu vao AL: (0->9)
cmp al,13
je thoat ;neu = enter thi thoat.
;Cat AX->SP
sub al,30h ;chuyen qua he 2
push ax ;cat ax -> {SP} ngan xep
;Nhan 10*BX
mov ax,bx ;chuyen BX->AX de nhan 10 len
mov si,10 ;nap 10 vao thanh ghi SI 16Bit
mul si ;AX*SI: DXAX<-tich. (KQ <=90 =>tich->AX)
;BX=AX+10*BX
mov bx,ax ;AX=BX*10->BX
pop ax ;lay tu ngan xep {SP} ->ax
mov ah,0 ;xoa ah=1 o tren
add bx,ax ;BX*10+AX->BX
loop TIEP
thoat: ret ;tro ve CTC tu ctc
nhapso endp
;*******************************************
end main
0 nhận xét:
Đăng nhận xét