MCS_8086习题4答案:
1. 从屏幕上输入大写字母,转换为小写字母并输出
要求:程序具有可读性、容错性 data segment para
str1 str2 str3
db 0dh, 0ah, ‘Please input(a-z): $’ db 0dh, 0ah, ‘The input is error!$’ db 0dh, 0ah, ‘The result is: $’
data ends
ss_seg segment stack
dw
100 dup(0)
ss_seg ends code segment para
assume cs:code, ss:ss_seg, ds:data main proc far
mov mov
ax, data ds, ax dx, str1 ah, 9 21h ah, 1 21h al, ‘A’ error al, ‘Z’ error next dx, str2 ah, 9 21h
1
main1: lea
mov int mov int cmp jb cmp ja jmp
;显示提示信息
;输入小写字母
;判断输入字符是否为’a’~’z’
error: lea
mov int
;显示错误提示信息
jmp main1 al, 20h
;跳转,重新输入
next: sub
push ax lea mov int pop mov mov int mov int
dx, str3 ah, 9 21h ax dl, al ah, 2 21h ax, 4c00h 21h
;返回操作系统
;显示转换后的大写字母
;显示输出提示信息
main endp
code ends
end main
2. 编写一子程序asc2bin,将ASCII转换为二进制数
要求: 输入参数:AL中存放需要转换的ASCII
输出参数:AL中存放转换后的二进制数并返回
;功能:将ASCII转换为二进制数 ;输入参数:AL中存放需要转换的ASCII ;输出参数:AL中存放转换后得到的二进制数 asc2bin
sub cmp jbe sub
proc al, 30h al, 9 asc2bin_1 al, 7
asc2bin_1: ret asc2bin
2
endp
3. 内存中存放8个16位有符号数,求8个数值之和,并将结果存放在内存变
量SUM中
注:程序中应用到字扩展为双字的指令CWD data segment para
buf sum
dw dd
-1, 30000, 35000, 36000, 37000, 20000, 10000, -2 0
data ends
ss_seg segment stack
dw
100 dup(0)
ss_seg ends code segment para
assume cs:code, ds:data, ss:ss_seg main proc far
mov mov lea mov
ax, data ds, ax bx, buf cx, 8 ax, [bx]
;有符号数字扩展为双字 ;32位数相加
;bx指向buf首地址
main1: mov
cwd add adc inc inc loop mov int
word ptr sum, ax word ptr sum + 2, dx bx bx main1 ax, 4c00h 21h
;循环8次
main endp
code ends
end main
3
4. 内存中存放8个8位有符号数,请按从大到小顺序排列
data segment para
buf
db
-1, 30, 35, 36, 37, 20, 100, -2
data ends
ss_seg segment stack
dw
100 dup(0)
ss_seg ends code segment para
assume cs:code, ds:data, ss:ss_seg main proc far
mov mov mov
ax, data ds, ax cx, 8 bx, 0 di, 0
;外循环
main1: mov
mov
push cx
al, buf[bx] al, buf[bx+1] next
;不符合规则, 则交换数据
;内循环
main2: mov
cmp jge
xchg al, buf[bx+1] mov mov
buf[bx], al di, 1 bx main2 cx di, 0 exit main1 ax, 4c00h
next: inc
loop pop cmp jz loop mov
;判断内循环是否发生数据交换
exit:
4
int 21h
main endp
code ends
end main
5. 内存中有8个16位数,请编写程序将8个数倒序排放
例:定义内存中8个数
buf dw 100, 3, 1, 20, 40, -2, 7, 10
10, 7, -2, 40, 20, 1, 3, 100
程序运行后,buf开始应为:buf dw
data segment para
buf
dw
-1, 30000, 35000, 36000, 37000, 20000, 10000, -2
data ends
ss_seg segment stack
dw
100 dup(0)
ss_seg ends code segment para
assume cs:code, ds:data, ss:ss_seg main proc far
mov mov mov mov
ax, data ds, ax bx, 0 cx, 8
main1: push buf[bx]
inc inc loop lea mov
bx bx main1 bx, buf cx, 8 [bx] bx bx
5
;8个数依次入堆栈
main2: pop
inc inc