//二路分支设计法
; You may customize this and other start-up templates;
; The location of this template is c:\emu8086\inc\0_com_template.txt
org 100h
; add your code here
data segment
xx1 db 45
yy1 db ?
data ends
code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov al,xx1
cmp al,0
jz aa1
jns aa2
mov al,0ffh
jmp aa1
aa2: mov al,1
aa1: mov yy1,al ;aa1可以完成两种任务,因为此时yy1==al
mov ah,4ch
int 21h
code ends
end start
//逻辑分解法
; You may customize this and other start-up templates;
; The location of this template is c:\emu8086\inc\0_com_template.txt
org 100h
; add your code here
CODE SEGMENT
ASSUME CS:CODE
START:
MOV AH,1
INT 21H
CMP AL,'1'
JZ WORK1
CMP AL,'2'
JZ WORK2
CMP AL,'3'
JZ WORK3
CMP AL,'4'
JZ WORK4
CMP AL,'5'
JZ WORK5
JMP WORK0
WOKR1:
WORK2:
WORK3:
WORK4:
WORK5:
WORK0:
MOV AH,4CH
INT 21H
CODE ENDS
END START
ret
//地址表法
表地址=表首址+(键号-1)*2
;and al,0fh 保留低四位
; You may customize this and other start-up templates;
; The location of this template is c:\emu8086\inc\0_com_template.txt
org 100h
; add your code here
data segment
table dw work1,work2,work3,work4,work5;index table
data ends
code segment
assume cs:code,ds:data
start:mov ax,data
mov ds,ax
lea bx,table
mov ah,1 ;设置ah=1,转化为输入模式
int 21h ;
and al,0fh ;保留低四位,将字符转化为数字
dec al ;sub 1
add al,al ;sub 2
sub ah,ah ;ah to be zero
add bx,ax ;because bx must match with ax to make table address
jmp word ptr[bx];trans
work1:
work2:
work3:
work4:
work5:
mov ah,4ch
int 21h ;return console
code ends
end start
//段内转移表法
// An highlighted block
var foo = 'bar';
实验
org 100h
; add your code here
dseg segment para public'dseg'
m db x(20)
p db 20 dup(?)
n db 20 dup(?)
dseg ends
code segment
assume cs:code,ds:desg
start:
mov ax,dseg
mov ds,ax
lea si,p
lea di,n
lea bx,m
xor ax,ax ;zero
xor dx,dx ;zero
mov cx,20 ;loop 20
l1: mov al,[bx] ;
test al,80h
jz l2
mov [di],al
inc bx
inc di
inc dh ;negative++
jmp l3
l2: mov [si],al
inc bx
inc si
inc dl ;positive++
l3: loop l1
mov cx,2
l5: mov bl,dl
shr dl,1
shr dl,1
shr dl,1
shr dl,1
and dl,0fh
cmp dl,10
jb l4
;add dl,7
l4:
add dl,30h
mov ah,2
int 21h
mov dl,bl
and dl,0fh
cmp dl,10
jb l6
add dl,7
l6: add dl,30h
mov ah,2
int 21h
mov dl,0ah
mov ah,2
int 21h
mov dl,0dh
mov ah,2
int 21h
mov dl,dh
loop l5
mov ah,4ch
int 21h
code ends
end start
第三次实验第二题source
DATA SEGMENT
DIGIT DB "DIGIT",0AH,0DH
LETTER DB "LETTER",0AH,0DH
OTHER DB "OTHER",0AH,0DH
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AH,07 ;接受输入不回显
INT 21H
CMP AL,30H ;判断是否是数字
JB AA1
CMP AL,39H
JA AA1
MOV CX,7
LEA SI,DIGIT ;显示 digit
AA0:MOV DL,[SI]
MOV AH,2
INT 21H
INC SI
LOOP AA0
JMP BB
AA1: CMP AL,41H ;判断是否为字母
JB AA3
CMP AL,5AH
JA AA2
LEA SI,LETTER
MOV CX,8
AA4:MOV DL,[SI]
MOV AH,2
INT 21H
INC SI
LOOP AA4
JMP BB
AA2:CMP AL,61H
JB AA3
CMP AL,7AH
JA AA3
LEA SI,LETTER
MOV CX,8
AA5:MOV DL,[SI]
MOV AH,2
INT 21H
INC SI
LOOP AA5
JMP BB
AA3:LEA SI,OTHER ;其他
MOV CX,7
AA6:MOV DL,[SI]
MOV AH,2
INT 21H
INC SI
LOOP AA6
BB: MOV AH,4CH
INT 21H
CODE ENDS
END START
fix加注释之后
DATA SEGMENT
DIGIT DB "DIGIT",0AH,0DH
LETTER DB "LETTER",0AH,0DH
OTHER DB "OTHER",0AH,0DH
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX ;将数据段设置为data
MOV AH,07 ;接受输入不回显
INT 21H
CMP AL,30H ;判断是否是数字是不是大于‘0’
JB AA1 ;小于等于则跳转去判断是不是其他
CMP AL,39H ;比较是否大于‘9’
JA AA1 ;大于则跳转去判断是不是其他
MOV CX,5 ;是数字则直接输出
LEA SI,DIGIT ;接下来要显示 digit
AA0:MOV DL,[SI] ;将基址设置为SI
MOV AH,2 ;设置为输出格式
INT 21H ;输出
INC SI ;si自加
LOOP AA0 ;循环
JMP BB ;7次循环后退出
AA1:CMP AL,41H ;判断是否为字母
JB AA3
CMP AL,5AH
JA AA2
LEA SI,LETTER
MOV CX,8
AA4:MOV DL,[SI]
MOV AH,2
INT 21H
INC SI
LOOP AA4
JMP BB ;八次循环之后退出
AA2:CMP AL,61H ;判断是不是小写字母
JB AA3
CMP AL,7AH
JA AA3
LEA SI,LETTER
MOV CX,8
AA5:MOV DL,[SI]
MOV AH,2
INT 21H
INC SI
LOOP AA5
JMP BB ;八次循环之后退出
AA3:LEA SI,OTHER ;其他
MOV CX,7
AA6:MOV DL,[SI]
MOV AH,2
INT 21H
INC SI
LOOP AA6
BB: MOV AH,4CH
INT 21H ;返回控制权
CODE ENDS
END START
编译出现问题:
zero prefix must be added to a HEX value, for example: 0Ch
解决方法:
在数据前面没有0的加个0,如EH变成0EH;就可以正常编译通过了;
若第一个start缺省,则end start中的 start 也必须去掉。
问题:为什么要先code end之后再进行end start
回答:
code end是结束代码段,end start是结束汇编。
第四次实验:
1、先执行后判断的循环,类似于do while循环
data segment
org 100h ;偏移合适,不要覆盖代码部分的内存
number1 db 1,2,3,4,5,6
sum1 dw ?
data ends
code segment
assume cs:code,ds:data
start:mov ax,data
mov ds,ax
lea bx,number1
mov ax,0
mov dh,0
mov cl,6
aa1: mov dl,[bx]
add ax,dx
inc bx
sub cl,1
jnz aa1
mov sum1,ax
mov ah,4ch
int 21h
code ends
end start
或者可以直接
;相当于直接从org 100h开始汇编
org 100h ;偏移合适,不要覆盖代码部分的内存
number1 db 1,2,3,4,5,6
sum1 dw ?
start:
lea bx,number1
mov ax,0
mov dh,0
mov cl,6
aa1: mov dl,[bx]
add ax,dx
inc bx
sub cl,1
jnz aa1
mov sum1,ax
mov ah,4ch
int 21h
end start
2、先判断后执行的循环,类似于while()
org 100h ;偏移合适,不要覆盖代码部分的内存
number1 db 1,2,3,4,5,6
sum1 dw ?
start:
lea bx,number1-1
mov ax,0
mov dh,0
mov cl,7
aa1: inc bx
sub cl,1
jz aa2
mov dl,[bx]
add ax,dx
jmp aa1
aa2:
mov sum1,ax
mov ah,4ch
int 21h
end start
问题代码:
org 100h ;偏移合适,不要覆盖代码部分的内存
number1 db 1,2,3,4,5,6
sum1 dw ?
start:
lea bx,number1
mov ax,0
mov dh,0
aa1:
mov dl,[bx]
add ax,dx
inc bx
cmp dl,6
jnz aa1
mov sum1,ax
mov ah,4ch
int 21h
end start
data segment
course1 db 70h,88h,92h,90h,99h
db 67h,77h,88h,76h,69h
db 74h,87h,77h,74h,70h
db 99h,97h,94h,98h,96h
num1 dw 5 dup(0)
data ends
code segment
assume cs:code,ds:data
start:mov ax,data
mov ds,ax
lea si,course1
lea di,num1
sub si,5
mov cl,5
aa1: mov bx,si
sub ax,ax
mov ch,4
aa2: add bx,5
add al,[bx]
daa
adc ah,0
dec ch
jnz aa2
mov [di],ax
inc si
add di,2
dec cl
jnz aa1
mov ah,4ch
int 21h
code ends
end start
data segment
course1 db 88h,88h,92h,90h,99h
db 77h,77h,88h,76h,69h
db 87h,87h,77h,74h,70h
db 97h,97h,94h,98h,96h
num1 dw 5 dup(0)
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,course1
lea di,num1
sub si,5
mov cl,5
aa1:mov bx,si
sub ax,ax
mov ch,4
aa2:add bx,5
add al,[bx]
cmp al,0f9h
jns aa3
daa
adc ah,0
dec ch
jmp aa2
aa3:add ah,1
daa
adc ah,0
dec ch
jmp aa2
mov [di],ax
inc si
add di,2
dec cl
jnz aa1
mov ah,4ch
int 21h
code ends
end start
即将正确代码
data segment
course1 db 70h,88h,92h,90h,99h
db 67h,77h,88h,76h,69h
db 74h,87h,77h,74h,70h
db 99h,97h,94h,98h,96h
num1 dw 5 dup(0)
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,course1
lea di,num1
sub si,5
mov cl,5
aa1:mov bx,si
sub ax,ax
mov ch,4
aa2:add bx,5
add al,[bx]
mov dl,al
daa
adc ah,0
cmp dl,0fah
js fii
add ah,01h
fii:dec ch
jnz aa2
mov [di],ax
inc si
add di,2
dec cl
jnz aa1
mov ah,4ch
int 21h
code ends
end start
//正确代码!!!
data segment
course1 db 70h,88h,92h,90h,99h
db 67h,77h,88h,76h,69h
db 74h,87h,77h,74h,70h
db 99h,97h,94h,98h,96h
num1 dw 5 dup(0)
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,course1
lea di,num1
sub si,5
mov cl,5
aa1:mov bx,si
sub ax,ax
mov ch,4
aa2:add bx,5
add al,[bx]
mov dl,al
daa
adc ah,0
cmp dx,00fah
js fii
add ah,01h
fii:dec ch
jnz aa2
mov [di],ax
inc si
add di,2
dec cl
jnz aa1
mov ah,4ch
int 21h
code ends
end start
一组数据找最大最小
data segment
array db 13,24,92,42,25,46,75,81,53,10
ma db 'MAX=','$'
mi db 0dh,0ah,'MIN=$'
data ends
code segment
assume cs:code,ds:data
main proc far
mov ax,data
mov ds,ax
lea dx,ma
mov ah,9
int 21h
lea bx,array
call max
cmp al,10
jc aa6
mov dh,30h
aa5:inc dh
sub dl,10
cmp dl,10
jnc aa5
push dx
mov dl,dh
mov ah,2
int 21h
pop dx
aa6:add dl,30h
mov ah,2
int 21h
lea dx,mi
mov ah,9
int 21h
lea bx,array
call min
cmp dl,10
jc aa8
mov dh,30h
aa7:inc dh
sub dl,10
cmp dl,10
jnc aa7
push dx
mov dl,dh
mov ah,2
int 21h
pop dx
aa8:add dl,30h
mov ah,2
int 21h
mov ah,4ch
int 21h
max proc near
mov cx,10
mov dl,[bx]
aa1:mov dh,[bx]
cmp dl,dh
jc aa2
inc bx
dec cx
cmp cx,0
jz out1
jmp aa1
aa2:mov dl,dh
inc bx
dec cx
cmp cx,0
jz out1
jmp aa1
out1:ret
max endp
min proc near
mov cx,10
mov dl,[bx]
aa3:mov dh,[bx]
cmp dl,dh
jnc aa4
inc bx
dec cx
cmp cx,0
jz out2
jmp aa3
aa4:mov dl,dh
inc bx
dec cx
cmp cx,0
jz out1
jmp aa3
out2:ret
min endp
code ends
end main
找地址
data segment
array1 dw 5 dup(5 dup(212fh,5a5dh,1234h,7865h))
eod dw 0
data ends
code segment
assume cs:coe ,ds:data
main proc far
mov ax,data
mov ds,ax
lea si,array1
mov cx,(eod-array1)/2
call sub3
mov di,cx
mov bx,data
call sub4
mov dl,':'
mov ah,2
int 21h
mov bx,si
call sub4
mov dl,20h
mov ah,2
int 21h
mov bx,di
call sub4
mov ah,4ch
int 21h
sub3 proc near
push ax
push di
mov di,si
mov ax,[si]
aa1:cmp ax,[si]
jnc aa2
mov ax,[si]
mov di,si
aa2:
add si,2
loop aa1
mov si,di
mov cx,ax
pop di
pop ax
ret
sub3 endp
sub4 proc near
mov dl,bh
mov cl,4
shr dl,cl
call sub5
mov dl,bh
and dl,0fh
call sub5
mov dl,bl
shr dl,cl
call sub5
mov dl,bl
and dl,0fh
call sub5
ret
sub4 endp
sub5 proc near
or dl,30h
cmp dl,3ah
jc aa3
add dl,7
aa3:
mov ah,2
int 21h
ret
sub5 endp
code ends
end main
计算阶乘
data segment
result dw 0
data ends
code segment
assume ds:data,cs:code
main proc far
mov ax,data
mov ds,ax
mov cx,5
call fact
cmp cx,10000
jc bb1
mov dl,30h
aa1:
inc dl
sub cx,10000
cmp cx,10000
jnc aa1
mov ah,2
int 21h
bb1:
cmp cx,1000
jc bb2
mov dl,30h
aa2:
inc dl
sub cx,1000
cmp cx,1000
jnc aa2
mov ah,2
int 21h
bb2:
cmp cx,100
jc bb3
mov dl,30h
aa3:inc dl
sub cx,100
cmp cx,100
jnc aa3
mov ah,2
int 21h
bb3:cmp cx,10
jc bb4
mov dl,30h
aa4:inc dl
sub cx,10
cmp cx,10
jnc aa4
mov ah,2
int 21h
bb4:
mov dl,cl
add dl,30h
mov ah,2
int 21h
mov ah,4ch
int 21h
fact proc near
and cx,0ffh
mov ax,0
lop2:
call fang
add ax,dx
dec cx
cmp cx,0
jnz lop2
mov cx,ax
ret
fact endp
fang proc near
push cx
push ax
and cx,0ffh
mov ax,1
lop1:
mul cx
dec cx
cmp cx,0
jnz lop1
mov dx,ax
pop ax
pop cx
ret
fang endp
code ends
end main
计算小写字母的个数
data segment
string db 100 dup(0)
data ends
code segment
assume cs:code,ds:data
main proc far
mov ax,data
mov ds,ax
lea bx,string
mov cl,0
aa1:
mov ah,1
int 21h
;中断输入
cmp al,0dh;若是回车
jz aa3;结束程序并且进行输出
cmp al,41h;比较大小"A"
jc aa4 ;小于A的数据去结束
cmp al,5bh;大于Z的数据去结束
jnc aa2
mov [bx],al;数据送到string
inc bx ;地址加一
jmp aa1 ;接着输入
aa2:
cmp al,61h
jc aa4
cmp al,7bh
jnc aa4
mov [bx],al
inc bx
inc cl
jmp aa1
aa3:
push cx
mov ah,3 ;为了设置dh,来确定光标位置的高八位
int 10h
mov ah,2 ;修改字符的命令开始
lea di,string;设置为起点
sub bx,di ;通过终点减去起点计算字符的数目
mov dl,bl ;认为数量很少,bl来代替字符的数目
inc dl ;保持中间有一个空格
int 10h ;修改输出的光标的列位位置
pop cx ;返回cx
mov dl,cl ;小写字母的个数
add dl,30h;转化为十进制
mov ah,2 ;
int 21h ;显示
aa4:
mov ah,4ch
int 21h
code ends
end main
匹配字符串
data segment
string1 db 'ABCDEFG'
string2 db 'ABCDEFG'
yes db 'YES$'
no db 'NO$'
data ends
code segment
assume cs:code,ds:data
main proc far
mov ax,data
mov ds,ax
mov cx,string2-string1
mov bx,yes-string2
cmp cx,bx
jnz aa2
lea bx,string1
lea si,string2
aa1:
mov al,[bx]
mov ah,[si]
cmp al,ah
jnz aa2
inc bx
inc si
loop aa1
mov ah,9 ;输出string,则ah=9
lea dx,yes
int 21h
mov ah,4ch
int 21h
aa2:mov ah,9
lea dx,no
int 21h
mov ah,4ch
int 21h
main endp
code ends
end main
是否有匹配的字符串
data segment
string1 db 'ABCDEFG' ;模板串
string2 db 'DEF' ;目标串
yes db 'Match$'
no db 'No Match$'
data ends
code segment
assume cs:code,ds:data
main proc far
mov ax,data
mov ds,ax
mov cx,string2-string1 ;模板串长度
mov bx,yes-string2 ;目标串长度
cmp cx,bx ;若cx<bx则返回错误
js aa2
lea bx,string1
lea si,string2
aa1:
mov al,[bx]
mov ah,[si]
cmp al,ah
jnz aa3
inc bx
inc si
inc dl
cmp dl,yes-string2
jz aa4
loop aa1
jmp aa2
aa3:mov dl,0
inc bx
lea si,string2
loop aa1
jmp aa2
aa4:
mov ah,9 ;输出string,则ah=9
lea dx,yes
int 21h
mov ah,4ch
int 21h
aa2:mov ah,9
lea dx,no
int 21h
mov ah,4ch
int 21h
main endp
code ends
end main
data segment
data ends
code segment
assume cs:code,ds:data
start:
mov al,'A'
mov dx,10h
mov bl,0f0h
A1:
push dx
mov ah,9
mov bh,0
mov cx,1
int 10h
mov ah,3
mov bh,0
int 10h
inc dl
mov ah,2
int 10h
add bl,1
pop dx
dec dx
jnz a1
mov ah,4ch
int 21h
code ends
end start
矩阵乘法
data segment
matrix1 db 3h,4h,15h,8h
db 4h,5h,6h,17h
db 8h,9h,3h,2h
db 1h,1h,4h,6h
matrix2 db 0f3h,0f9h,8h,0e6h
unit dw 4 dup(0)
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,matrix1
lea di,matrix2
lea bx,unit
mov cl,4h
aa0:
sub ax,ax
mov ch,4h
aa1:
mov dx,ax
mov al,[di]
mul byte ptr[si]
add ax,dx
inc si
inc di
dec ch
jnz aa1
lea di,matrix2
mov [bx],ax
inc bx
inc bx
dec cl
jnz aa0
mov ah,4ch
int 21h
code ends
end start
输出很多个斜线的A
DATA SEGMENT
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE DS:DATA
START:MOV AL,'A'
MOV DX,10H
MOV BL,0F0H
A1: PUSH DX
MOV AH,9
MOV BH,0
MOV DH,0
MOV CX,1
INT 10H
MOV AH,3 ;读取获得光标位置
MOV BH,0
INT 10H
INC DL ;设置光标的列号和行号
INC DH
MOV AH,2
INT 10H
ADD BL,1 ;改变颜色
POP DX
DEC DX ;字符个数统计
JNZ A1
MOV AH,4CH
INT 21H
CODE ENDS
END START
小车运动
WR MACRO CHA,ATR,NUM ;宏定义,显示处理
MOV AH,2 ;设置光标位置
INT 10H
MOV AL,CHA ;要显示的字符送AL
MOV CX,NUM ;要显示字符的个数送CX
MOV BL,ATR ;要显示字符的颜色属性
MOV AH,9
INT 10H
ENDM
CODE SEGMENT
ASSUME CS:CODE
MAIN PROC FAR
CAR: MOV AH,0
MOV AL,3 ;设置屏幕显示模式为80*25的彩色字符方式
INT 10H
LOP1:MOV SI,0A0AH ;定义显示位置(10,10)
LOP2:MOV DX,SI
WR '+',0CH,5 ;显示第一行
INC DH ;调整行坐标
SUB DL,2 ;调整列坐标
WR '+',4,9 ;显示第二行
INC DH ;调整行坐标
INC DL ;调整列坐标
WR 'O',8EH,1 ;显示前轮
ADD DL,6 ;调整列坐标
WR 'O',8EH,1 ;显示后轮
CALL DELAY ;调用延时
MOV DX,SI
WR 0,0,5 ;清除第一行
INC DH
SUB DL,2
WR 0,0,9 ;清除第二行
INC DH
WR 0,0,8 ;清除第三行
MOV AH,1 ;判断是否有按键
INT 16H
JZ CONU
MOV AH,4CH ;返回DOS
INT 21H
JLOP2:JMP LOP2
CONU: INC SI
CMP SI,0A50H ;判断车是否跑到头
JB JLOP2
JMP LOP1
MAIN ENDP
DELAY PROC NEAR ;延时子程序
MOV CX,00001H
LOOP $
RET
DELAY ENDP
CODE ENDS
END MAIN