ASM Tutorial

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

1.

Tnh tng 2 s a v b
inxau macro inxau macro tbao
tbao lea dx,tbao
mov ah,9
lea int 21h
dx,tbao endm
mov ah,9 _stack segment stack
int 21h dw 100 dup(0)
endm _stack ends
_stack data segment
segment stack m1 db 10,13,'a=$'
dw 100 m2 db 10,13,'b=$'
dup(0) m3 db 10,13,'ket qua=$'
_stack ends m4 db '.5$'
data segment so dw 0
m1 db a dw 0
10,13,'a=$' b dw 0
m2 db du dw 0
10,13,'b=$' data ends
m3 db code segment
10,13,'ket assume cs:code, ds:data, ss:_stack
qua=$' begin:
m4 db mov ax,data
'.5$' mov ds,ax
so dw 0 ; nhap a
a dw 0 inxau m1
b dw 0 call nhap
du dw 0 mov bx,so
data ends mov a,bx
code segment ;nhap b
assume inxau m2
cs:code, call nhap
ds:data, mov bx,so
ss:_stack mov b,bx
begin: ;cx=ax*bx
mov inxau m3
ax,data call tong
mov ds,ax call in10
; nhap a mov ah,1
inxau m1 int 21h
call nhap mov ah,4ch
mov bx,so int 21h
mov a,bx nhap proc
;nhap b push ax
inxau m2 push bx
call nhap push cx
mov bx,so push dx
mov b,bx mov bx,10
;cx=ax*bx xor cx,cx
inxau m3 mov si,0
call tong l1:
call in10 mov ah,1
mov ah,1 int 21h
int 21h cmp al,13
mov je het
ah,4ch cmp al,8
int 21h jnz so_am
nhap proc and cx,cx
push ax jz nodiv
push bx xor dx,dx
push cx mov ax,cx
push dx jmp ctro
mov bx,10 nodiv:
xor cx,cx xor si,si
mov si,0 ctro:
l1: push cx
mov ah,1 mov cx,1
int 21h mov ah,10
cmp al,13 mov dl,20h
je het int 10h
cmp al,8 pop cx
jnz so_am jmp l1
and cx,cx so_am:
jz nodiv cmp al,'-'
xor dx,dx jne nap
mov ax,cx mov si,1
jmp ctro jmp l1
nodiv: nap:
xor si,si cmp al,'0'
ctro: jl l1
push cx cmp al,'9'
mov cx,1 jg l1
mov ah,10 and al,0fh
mov xor ah,ah
dl,20h xchg ax,cx
int 10h mul bx
pop cx add cx,ax
jmp l1 jmp l1
so_am: het:
cmp al,'- and si,si
' jz soduong
jne nap neg cx
mov si,1 soduong:
jmp l1 mov so,cx
nap: pop dx
cmp pop cx
al,'0' pop bx
jl l1 pop ax
cmp ret
al,'9' nhap endp
jg l1 tong proc
and push ax
al,0fh push bx
xor ah,ah push cx
xchg push dx
ax,cx mov ax,a
mul bx mov bx,b
add cx,ax add ax,bx
jmp l1 mov so,ax
het: pop dx
and si,si pop cx
jz pop bx
soduong pop ax
neg cx ret
soduong: tong endp
mov so,cx in10 proc
pop dx push ax
pop cx push bx
pop bx push cx
pop ax push dx
ret mov ax,so
nhap endp xor cx,cx
tong proc mov bx,10
push ax cmp ax,0
push bx jge duong
push cx push ax
push dx mov al,'-'
mov ax,a mov ah,14
mov bx,b int 10h
add ax,bx pop ax
mov so,ax neg ax
pop dx duong:
pop cx xor dx,dx
pop bx div bx
pop ax push dx
ret inc cx
tong endp cmp ax,0
in10 proc jnz duong
push ax ;in ra
push bx mov ah,2
push cx inra:
push dx pop dx
mov ax,so or dl,30h
xor cx,cx int 21h
mov bx,10 loop inra
cmp ax,0 pop dx
jge duong pop cx
push ax pop bx
mov al,'- pop ax
' ret
mov ah,14 in10 endp
int 10h code ends
pop ax end begin
neg ax
duong:
xor dx,dx
div bx
push dx
inc cx
cmp ax,0
jnz duong
;in ra
mov ah,2
inra:
pop dx
or dl,30h
int 21h
loop inra
pop dx
pop cx
pop bx
pop ax
ret
in10 endp
code ends
end begin

2. Tnh hiu 2 s a v b
code segment
assume cs:code
org 100h
start : jmp over
msg1 db 'nhap a=$'
msg2 db 10,13,'nhap b=$'
msg3 db 10,13 ,'Hieu hai so la :$'
over:

mov ah,9
lea dx,msg1
int 21h

mov ah,1
int 21h
mov bl,al

mov ah,9
lea dx,msg2
int 21h

mov ah,1
int 21h
mov cl,al

mov ah,9
lea dx,msg3
int 21h

cmp cl,bl
jae truonghop2
sub bl,cl
jmp hieu
truonghop2: ;in dau tru
mov ah,2
mov dl,'-'
int 21h
sub bl,cl
neg bl
hieu:
;in hieu
add bl,30h
mov ah,2
mov dl,bl
int 21h
int 20h
code ends
end start

3. Tnh trung bnh cng 2 s c 5 ch s a v b


inxau macro tbao
lea dx,tbao
mov ah,9
int 21h
endm
_stack segment stack
dw 100 dup(0)
_stack ends
data segment
m1 db 10,13,'a=$'
m2 db 10,13,'b=$'
m3 db 10,13,'ket qua=$'
m4 db '.5$'
so dw 0
a dw 0
b dw 0
du dw 0
data ends
code segment
assume cs:code, ds:data, ss:_stack
begin:
mov ax,data
mov ds,ax
; nhap a
inxau m1
call nhap
mov bx,so
mov a,bx
;nhap b
inxau m2
call nhap
mov bx,so
mov b,bx
;cx=ax*bx
inxau m3
call tbcong

mov ah,1
int 21h
mov ah,4ch
int 21h
nhap proc
push ax
push bx
push cx
push dx
mov bx,10
xor cx,cx
mov si,0
l1:
mov ah,1
int 21h
cmp al,13
je het
cmp al,8
jnz so_am
and cx,cx
jz nodiv
xor dx,dx
mov ax,cx
jmp ctro
nodiv:
xor si,si
ctro:
push cx
mov cx,1
mov ah,10
mov dl,20h
int 10h
pop cx
jmp l1
so_am:
cmp al,'-'
jne nap
mov si,1
jmp l1
nap:
cmp al,'0'
jl l1
cmp al,'9'
jg l1
and al,0fh
xor ah,ah
xchg ax,cx
mul bx
add cx,ax
jmp l1
het:
and si,si
jz soduong
neg cx
soduong:
mov so,cx
pop dx
pop cx
pop bx
pop ax
ret
nhap endp
tbcong proc
push ax
push bx
push cx
push dx
xor dx,dx
mov ax,a
mov bx,b
add ax,bx
mov cx,2
div cx
mov so,ax
call in10
cmp dx,0
je he
inxau m4
he:
pop dx
pop cx
pop bx
pop ax
ret
tbcong endp
in10 proc
push ax
push bx
push cx
push dx
mov ax,so
xor cx,cx
mov bx,10
cmp ax,0
jge duong
push ax
mov al,'-'
mov ah,14
int 10h
pop ax
neg ax
duong:
xor dx,dx
div bx
push dx
inc cx
cmp ax,0
jnz duong
;in ra
mov ah,2
inra:
pop dx
or dl,30h
int 21h
loop inra
pop dx
pop cx
pop bx
pop ax
ret
in10 endp
code ends
end begin

1. Nhp tn v in chui XIN CHAO + tn ra mn hnh


DSEG SEGMENT
MSG1 DB 'NHAP VAO TEN CUA BAN : $'
MSG2 DB 10,13,'XIN CHAO $ !'
MAX DB 30
LEN DB ?
BUFF DB 30 DUP('$')
DSEG ENDS
CSEG SEGMENT
ASSUME CS:CSEG , DS: DSEG
BEGIN:
MOV AX ,DSEG
MOV DS,AX
MOV AH , 09H
LEA DX, MSG1
INT 21H
MOV AH, 0AH
LEA DX, MAX
INT 21H
MOV AH,02H
MOV DX,1500H
INT 10H
MOV AH,09H
LEA DX,MSG2
INT 21H
MOV AH,09H
LEA DX,BUFF
INT 21H
MOV AH,08H
INT 21H
MOV AH,4CH
INT 21H
CSEG ENDS
END BEGIN

2. Nhp vo mt chui, xut chui o ngc ra mn hnh


dseg segment
m1 db 'xuat chuoi tu A den Z: $'
m2 db 10,13,'xuat chuoi dao nguoc: $'
dseg ends
sseg segment
db 100 dup(?)
sseg ends
cseg segment
assume cs: cseg, ds:dseg, ss:sseg
start:
mov ax, dseg
mov ds,ax

xor ax,ax
xor cx,cx

mov ah ,09h
lea dx,m1
int 21h

mov dl, 'A'


mov cl, 26
xuat:
mov ah, 02h
int 21h
push ax
inc dl
loop xuat

mov ah, 09h


lea dx,m2
int 21h

xor cx, cx
mov cl, 26

xuatdaonguoc:
mov ah, 02h
pop dx
int 21h
loop xuatdaonguoc

exit:
mov ah, 08h
int 21h
mov ah, 4ch
int 21h
cseg ends
end start

3. Nhp vo chui k t thng, xut ra chui in hoa


DSEG SEGMENT

msg1 DB 'Hay nhap chuoi ky tu thuong: $'


msg2 DB 10, 13, 'Chuoi in hoa la: $'
DEM DW ?

DSEG ENDS

SSEG SEGMENT STACK 'STACK'

DW 256 DUP(?)

SSEG ENDS

CSEG SEGMENT

ASSUME CS: CSEG, DS: DSEG, SS: SSEG


start: MOV AX, DSEG
MOV DS, AX

MOV AH, 09h


LEA DX, msg1
INT 21h

XOR CX, CX
nhap: MOV AH, 01
INT 21h
CMP AL, 0Dh ; (hoac dung CMP AX,010Dh)
JZ inra
SUB AX,20H
PUSH AX
INC CX
JMP nhap

inra: MOV AH, 09h


LEA DX, msg2
INT 21h

MOV BX,1
MOV DEM,CX

intiep:

XOR DX,DX
MOV DX,0112H
ADD DX,DEM

MOV AH,02H
SUB DX,BX
INC BX
INT 10H

POP AX
MOV AH, 02H
MOV DL,AL
INT 21h
LOOP intiep

MOV AH, 08h


INT 21h

MOV AH, 4Ch


INT 21h

CSEG ENDS
END start

4. Nhp vo mt k t, in ra k t k sau n
DSEG SEGMENT
MSG1 DB 'NHAP VAO MOT KY TU : $'
MSG2 DB 10,13,'KY TU KE TIEP KY TU VUA NHAP LA : $'
SYM DB ?
DSEG ENDS

CSEG SEGMENT
ASSUME CS:CSEG , DS:DSEG
BEGIN:
MOV AX,DSEG
MOV DS,AX
MOV AH,09H
LEA DX,MSG1
INT 21H

MOV AH,01H
INT 21H

MOV SYM,AL

MOV AH,09H
LEA DX,MSG2
INT 21H

MOV DL,SYM
ADD DL,1
MOV AH,02H
INT 21H

MOV AH,08H
INT 21H

MOV AH,4CH
INT 21H
CSEG ENDS
END BEGIN

5. Xut mt chui t A->Z v chui o ngc t Z->A


DSEG SEGMENT
MSG1 DB 'XUAT CHUOI TU A --> Z : $'
MSG2 DB 10,13,'XUAT CHUOI DAO NGUOC LA : $'
DSEG ENDS
SSEG SEGMENT STACK
DB 100 DUP(?)
SSEG ENDS
CSEG SEGMENT
ASSUME CS:CSEG,DS:DSEG

START: MOV AX,DSEG


MOV DS,AX

MOV AH,09H
LEA DX,MSG1
INT 21H

XOR DX,DX
MOV CX,26
MOV DL,'A'

XUAT1: MOV AH,02H


INT 21H
PUSH AX
INC DL
LOOP XUAT1

MOV AH,09H
LEA DX,MSG2
INT 21H
XOR CX,CX
MOV CX,26

XUAT2: POP AX
MOV DX,AX
MOV AH,02H
INT 21H
XOR DX,DX
MOV DX,AX
LOOP XUAT2

EXIT:MOV AH,08H
INT 21H
MOV AH,4CH
INT 21H

CSEG ENDS
END START

6. Xut ra ngy, thng, nm


DSEG SEGMENT

MSG1 DB 'HOM NAY LA : $'


thu db 'SunMonTueWedThuFriSat $'
St_date db 20, 0, 20 dup(0)
ngay DB ?
thang DB ?
nam DW ?
DSEG ENDS

SSEG SEGMENT
DB 100 DUP (?)
SSEG ENDS

CSEG SEGMENT

ASSUME CS:CSEG , DS:DSEG


START: MOV AX,DSEG
MOV DS,AX

LEA SI,St_date
MOV AH,09H
LEA DX,MSG1
INT 21H

MOV AH,2AH
INT 21H
;Vo:AH=2Ah
;Ra:AL=thu trong tun(0=C.nhat,6=T.bay)
;DL=ngy(1-31)
;DH=thang(1-12)
;CX=nam(1980-2099)
XOR AH,AH
MOV ngay,DL
MOV thang,DH
MOV nam,CX

MOV BL,3
MUL BL

MOV SI,AX
MOV CX,3

_THU:
MOV AH, 02H
mov DL, thu[SI]
int 21h
inc SI
loop _THU

MOV AH,02H
MOV DL,0
INT 21H

_NGAY:
MOV AL,ngay
XOR AH,AH
MOV BL,10
DIV BL

ADD AH,30H
ADD AL,30H
MOV BH,AH

MOV AH,02H
MOV DL,AL
INT 21H
MOV AH,02H
MOV DL,BH
INT 21H

MOV AH,02H
MOV DL,'-'
INT 21H

_THANG:
MOV AL,thang
XOR AH,AH
MOV BL,10
DIV BL

ADD AH,30H
ADD AL,30H
MOV BH,AH

MOV AH,02H
MOV DL,AL
INT 21H
MOV AH,02H
MOV DL,BH
INT 21H

MOV AH,02H
MOV DL,'-'
INT 21H

MOV AX,nam
MOV BX,10

XOR CX,CX
MOV CX,4

_NAM:

XOR DX,DX
DIV BX
PUSH DX
LOOP _NAM

XOR CX,CX
MOV CX,4

_XUATNAM:
POP DX
ADD DL,30H
MOV AH,02H
INT 21H

LOOP _XUATNAM

EXIT:
MOV AH,08H
INT 21H

CSEG ENDS
END START

7. Xut ra gi h thng
DSEG SEGMENT
Time_Buf DB '00:00:00$'
DSEG ENDS

CSEG SEGMENT
ASSUME: CS:CSEG,DS:DSEG

START:MOV AX, DSEG


MOV DS, AX
MOV AH, 2Ch
INT 21h

;AH = 2Ch
;Ra: CH = gio
;CL = phut
;DH = giay
;DL = % giay

MOV AL, CH
MOV AH, 0
MOV DL, 10
DIV DL

ADD AL, 30h


ADD AH, 30h
MOV Time_Buf, AL
MOV Time_Buf+1, AH

MOV AL, CL
MOV AH, 0
MOV DL, 10
DIV DL

ADD AL, 30h


ADD AH, 30h
MOV Time_Buf+3, AL
MOV Time_Buf+4, AH

MOV AL, DH
MOV AH, 0
MOV DL, 10

DIV DL

OR AX, 3030h
MOV Time_Buf+6, AL
MOV Time_Buf+7, AH

mov ah,02h ;Di chuyen


mov dx,1545h;con tro toi
int 10h ;dong 12 (0c) cot 21 (15)

MOV AH, 9
LEA DX, Time_Buf

INT 21h
mov ah,08h
int 21h
MOV AH, 4Ch
INT 21h
CSEG ENDS
END START

You might also like