X、Y、Z、V均为字变量,在X、Y、Z、V字单元中存放是16位带符号数。试编写汇编语言程序完成以下功能:
计算表达式值(V–(X*Y+Z-720))/X,将运算结果整数放在SUM1单元,余数放在SUM2单元。
(1) 在DOSBOX编译链接成可执行文件后,使用Debug装入内存。
(2) 使用U命令反汇编代码,并与源文件比对,需要注意数据段名、变量转入内存后的形式。
(3) 分别在G命令执行前后,使用D命令查看各个变量的值。
assume cs:code,ds:data,ss:stack
stack segment stack
db 1024 dup(?)
stack ends
data segment
X dw 540
Y dw 1
Z dw -1
V dw 1080
SUM1 dw ?
SUM2 dw ?
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov ax,word ptr X
imul word ptr Y
mov bx,ax
mov cx,dx
mov ax,word ptr Z
cwd
add bx,ax
adc cx,dx
sub bx,720
sbb cx,0
mov ax,word ptr V
cwd
sub ax,bx
sbb dx,cx
idiv x
mov word ptr SUM1,ax
mov word ptr SUM2,dx
mov ax,4c00h
int 21h
code ends
end start
2、BL中的只有一位为0,编写程序测试0所在的位数(从左编号,最左边为第0位),并输出提示信息“The X Bit is 0”(X为0、1、2、3…7),要求使用地址表方法实现。
首先,没有用地址表的方法。
assume cs:code,ds:data,ss:stack
stack segment stack
db 1024 dup(?)
stack ends
data segment
str1 db "The $"
str2 db " Bit is 0$"
data ends
code segment
printstr macro str ;宏--用于输出字符串
push ax
push dx
lea dx,str
mov ah,09h
int 21h
pop dx
pop ax
endm
printnum macro num ;宏--用于输出一个十进制的数
push ax
push dx
xor dx,dx
mov dx,30h
add dx,num
mov ah,02h
int 21h
pop dx
pop ax
endm
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov bl,11011111b
xor ax,ax ;记录位数
xor dx,dx ;判断是否为0
mov cx,8
again: sar bl,1
adc dx,0
cmp dx,0
jz done
inc ax
xor dx,dx
loop again
done: printstr str1
printnum ax
printstr str2
mov ax,4c00h
int 21h
code ends
end start
assume cs:code,ds:data,ss:stack
stack segment stack
db 1024 dup(?)
stack ends
data segment
table dw l0,l1,l2,l3,l4,l5,l6,l7
str0 db 'The 0 Bit is 0$'
str1 db 'The 1 Bit is 0$'
str2 db 'The 2 Bit is 0$'
str3 db 'The 3 Bit is 0$'
str4 db 'The 4 Bit is 0$'
str5 db 'The 5 Bit is 0$'
str6 db 'The 6 Bit is 0$'
str7 db 'The 7 Bit is 0$'
str db 'bl = $'
data ends
code segment
printstr macro str ;宏--用于输出字符串
push ax
push dx
lea dx,str
mov ah,09h
int 21h
pop dx
pop ax
endm
printnum macro num ;宏--用于输出二进制字符
push ax
push cx
push dx
mov cx,8
mov al,num
xor dx,dx
again1:sar al,1
adc dx,30h
push dx
xor dx,dx
loop again1
mov cx,8
continue:pop dx
mov ah,02h
int 21h
loop continue
mov dl,62h
int 21h
mov dl,0dh
int 21h
mov dl,0ah
int 21h
pop dx
pop cx
pop ax
endm
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov bl,11011111b
printstr str
printnum bl
xor si,si
again: sar bl,1
inc si
jc again
dec si
shl si,1
jmp table[si]
l0: mov dx,offset str0
mov ah,09h
int 21h
jmp done
l1: mov dx,offset str1
mov ah,09h
int 21h
jmp done
l2: mov dx,offset str2
mov ah,09h
int 21h
jmp done
l3: mov dx,offset str3
mov ah,09h
int 21h
jmp done
l4: mov dx,offset str4
mov ah,09h
int 21h
jmp done
l5: mov dx,offset str5
mov ah,09h
int 21h
jmp done
l6: mov dx,offset str6
mov ah,09h
int 21h
jmp done
l7: mov dx,offset str7
mov ah,09h
int 21h
done: mov ax,4c00h
int 21h
code ends
end start
3、在内存Score缓冲区中存放有100个学生的成绩数据,为无符号字节数。设 计程序完成如下功能:根据用户输入的一个2位十进制数,作为查找对象,在该数组中查找,若找到则显示“Y”,若没找到则显示“N” 。
assume cs:code,ds:data,ss:stack
stack segment stack
db 1024 dup(?)
stack ends
data segment
Score db 23,34,56,86,95,34,76,79,45,65
data ends
code segment
printChar macro Char ;宏--用于输出字符 'Y' 或‘N’
push ax
push dx
mov dl,Char
mov ah,02h
int 21h
pop dx
pop ax
endm
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
sub sp,2
call input
pop ax
mov cx,10
mov si,offset Score
continue: cmp byte ptr [si],al
je l
inc si
loop continue
printChar 'N'
jmp done
l: printChar 'Y'
done: mov ax,4c00h
int 21h
input proc ;从键盘输入一个数,0~65535
push bp
mov bp,sp
push ax
push bx
push cx
mov bx,10
mov word ptr[bp+4],0
again:mov ah,01h
int 21h
cmp al,0dh
je next
xor cx,cx
add cl,al
sub cl,30h
mov ax,word ptr [bp+4]
mul bx
add ax,cx
mov word ptr[bp+4],ax
jmp again
next:pop cx
pop bx
pop ax
pop bp
ret
input endp
code ends
end start
4. 编写一个子程序计算z=f(x,y)=x*y+x-y(x,y,z有符号数字操作数,假设计算结果为16位,可以放在z中)。要求:(1)通过堆栈传送参数x和y;(2)将计算结果按照10进制输出到屏幕上。
assume cs:code,ds:data,ss:stack
stack segment stack
db 1024 dup(?)
stack ends
data segment
str1 db 'please input x: $'
str2 db 'please input y: $'
str3 db 'z = $'
x dw ?
y dw ?
z dw ?
data ends
code segment
newline macro ;宏--用于输出回车换行
push ax
push dx
mov dl,0dh
mov ah,02h
int 21h
mov dl,0ah
mov ah,02h
int 21h
pop dx
pop ax
endm
printstr macro str ;宏--用于输出字符串
push ax
push dx
lea dx,str
mov ah,09h
int 21h
pop dx
pop ax
endm
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
;用来输入x,y
printstr str1
sub sp,2
call input
pop word ptr x
printstr str2
sub sp,2
call input
pop word ptr y
;执行函数fun
sub sp,2
push word ptr x
push word ptr y
call fun
pop word ptr z
;输出z
printstr str3
push word ptr z
call output
mov ax,4c00h
int 21h
fun proc ;实现x*y+x-y
push bp
mov bp,sp
push ax
mov ax,word ptr[bp+4]
imul word ptr[bp+6]
add ax,word ptr[bp+6]
sub ax,word ptr[bp+4]
mov word ptr[bp+8],ax
pop ax
pop bp
ret 4
fun endp
input proc ;从键盘输入一个数,(-32768~32767)
push bp
mov bp,sp
push ax
push bx
push cx
push si
mov bx,10
mov word ptr[bp+4],0
mov si,1
again:mov ah,01h
int 21h
cmp al,45
jnz f1
mov si,-1
jmp again
f1:cmp al,0dh
je next
xor cx,cx
add cl,al
sub cl,30h
mov ax,word ptr [bp+4]
mul bx
add ax,cx
mov word ptr[bp+4],ax
jmp again
next:mov ax,word ptr[bp+4]
imul si
mov word ptr[bp+4],ax
pop si
pop cx
pop bx
pop ax
pop bp
ret
input endp
output proc ;将数输出到屏幕(-32768~32767)
push bp
mov bp,sp
push ax
push bx
push dx
mov bx,-1
push bx
mov bx,10
mov ax,word ptr [bp+4]
add ax,0
jns l1
mov dl,45
mov ah,02h
int 21h
mov ax,word ptr [bp+4]
neg ax
l1: cmp ax,0
jz l2
xor dx,dx
div bx
add dl,30h
push dx
jmp l1
l2:pop dx
cmp dx,-1
jz done
mov ah,02h
int 21h
jmp l2
done:pop dx
pop bx
pop ax
pop bp
ret 2
output endp
code ends
end start