Một số Ví dụ Lập trình Hợp ngứ (Assembly)
; Ví dụ 1: In ra màn hình : 'CHAO CAC BAN!'
.MODEL TINY
.CODE
ORG 100H
START:
JMP LoiChao
CRLF DB 13,10,'$'
CHAO DB 'CHAO CAC BAN! $'
LoiChao:
MAIN PROC
;Xuong dong moi
MOV AH,9
LEA DX,CRLF
INT 21H
;Hien thi loi chao
MOV AH,9
LEA DX,CHAO
INT 21H
;Xuong dong moi
MOV AH,9
LEA DX,CRLF
INT 21H
;Tro ve DOS
INT 4cH
MAIN ENDP
END START
;------------------------------------------------------------------------------------------------------------
; Ví dụ 2: Tính tổng 2 số nhập vào từ bàn phím
.MODEL SMALL
.DATA
TB1 DB 'NHAP SO THU NHAT: $'
TB2 DB 'NHAP SO THU HAI: $'
TB3 DB 'KET QUA: $'
A DB ?
B DB ?
.CODE
MAIN PROC
;IN RA XAU1
MOV AX,@DATA
MOV DS,AX
LEA DX,TB1
MOV AH,9
INT 21H
;NHAP SO THU NHAT
MOV AH,1
INT 21H
MOV BL,AL
SUB BL,30H
MOV A,BL
;XUONG DONG VE DAU DONG
MOV AH,2
MOV DL,0DH
INT 21H
MOV DL,0AH
INT 21H
;IN RA XAU2
MOV AX,@DATA
MOV DS,AX
LEA DX,TB2
MOV AH,9
INT 21H
;NHAP SO THU HAI
MOV AH,1
INT 21H
MOV BL,AL
SUB BL,30H
MOV B,BL
;TINH TONG
MOV BL,B
ADD BL,A
;XUONG DONG VE DAU DONG
MOV AH,2
MOV DL,0DH
INT 21H
MOV DL,0AH
INT 21H
;IN XAU 3
MOV AX,@DATA
MOV DS,AX
LEA DX,TB3
MOV AH,9
INT 21H
MOV AH,2
;SUB BL,30H
MOV DL,BL
INT 21H
;KET THUC
MOV AH,4CH
INT 21H
END MAIN
;-------------------------------------------------------------------------------------------------
; Ví dụ 3: In ra màn hình Xâu nghịch đảo
org 100h
jmp start
; tao xau nghich dao
string1 db ' toi khong mo hoa $'
start: lea bx, string1
mov si, bx
next_byte: cmp [si], 'a'
je found_the_end
inc si
jmp next_byte
found_the_end: dec si
; now bx points to beginning,
; and si points to the end of string.
; do the swapping:
do_reverse: cmp bx, si
jae done
mov al, [bx]
mov ah, [si]
mov [si], al
mov [bx], ah
inc bx
dec si
jmp do_reverse
; reverse complete, print out:
done: lea dx, string1
mov ah, 09h
int 21h
; wait for any key press....
mov ah, 0
int 16h
ret
;-------------------------------------------------------------------------------------------------------------
; Ví dụ 4: Đổi số ra xâu
org 100h
jmp start
; text data:
msg1 db 0Dh,0Ah, " Nhap vao so nguyen bat ky tu -32768 -> 65535, nhan so O de dung: $"
msg2 db 0Dh,0Ah, " Dang so thap phan : $"
buffer db 7,?, 5 dup (0), 0, 0
;
binary dw ?
start:
; xuat dong thong bao 1:
mov dx, offset msg1
mov ah, 9
int 21h
; nhap chuoi tu ban phim:
mov dx, offset buffer
mov ah, 0ah
int 21h
; chan chan chuoi la so 0 thi dung chuong trinh:
mov bx, 0
mov bl, buffer[1]
mov buffer[bx+2], 0
lea si, buffer + 2 .
call tobin ; goi thu tuc doi chuoi thanh nhi phan.
mov binary, cx
jcxz stop ; dung lai neu cx = 0
; xuat thong bao thu 2:
mov dx, offset msg2
mov ah, 9
int 21h
mov ax,cx
call outdec
jmp start ; lap lai tu dau
stop:
ret ;
; Thu tuc doi string thanh so nhi phan co dau ('-').
; ket qua luu vao thanh ghi cx
tobin proc near
push dx
push ax
push si
jmp process
;==== local variables ====
make_minus db ? ; used as a flag.
ten dw 10 ; used as multiplier.
;=========================
process:
; reset the accumulator:
mov cx, 0
; reset flag:
mov cs:make_minus, 0
next_digit:
; read char to al and
; point to next byte:
mov al, [si]
inc si
; check for end of string:
cmp al, 0 ; end of string?
jne not_end
jmp stop_input
not_end:
; check for minus:
cmp al, '-'
jne ok_digit
mov cs:make_minus, 1 ; set flag!
jmp next_digit
ok_digit:
; multiply cx by 10 (first time the result is zero)
push ax
mov ax, cx
mul cs:ten ; dx:ax = ax*10
mov cx, ax
pop ax
; it is assumed that dx is zero - overflow not checked!
; convert from ascii code:
sub al, 30h
; add al to cx:
mov ah, 0
mov dx, cx ; backup, in case the result will be too big.
add cx, ax
; add - overflow not checked!
jmp next_digit
stop_input:
; check flag, if string number had '-'
; make sure the result is negative:
cmp cs:make_minus, 0
je not_minus
neg cx
not_minus:
pop si
pop ax
pop dx
ret
tobin endp
; Thu tuc nay xuat ra man hinh chuoi so .
; ke ca dau ('-') tu thanh ghi ax .
outdec proc near
mov cx, ax
push ax
push bx
push cx
push dx
or ax,ax ; kiem tra ax<0?
jge @end_if1
push ax
mov dl,'-'
mov ah,2
int 21h
pop ax
neg ax ; ax = -ax
@end_if1:
xor cx,cx
mov bx,10d ; bx chua so chia
@repeat1:
xor dx,dx
div bx
push dx
inc cx
or ax,ax
jne @repeat1
mov ah,2
@print_loop:
pop dx
or dl,30h
int 21h
loop @print_loop ;lap lai cho den khi xong
pop dx ; Phuc hoi lai cac thanh ghi
pop cx
pop bx
pop ax
ret
outdec endp
0 nhận xét:
Đăng nhận xét