汇编语言大作业
(1)从键盘输入两个四位十六进制数。
(2)将这两个数以二进制形式输出,要求输出的0和1颜色交替变化。
(3)找出这两个数中的偶数,若有则以十进制输出,若无,输出“NO”。
(4)计算这两个数的平方和。
(5)数据的输入和结果的输出都要有必要的提示,且提示独占一行。
(6)要使用到子程序。
data segment
str1 db 0ah,0dh,'Input number1 :',0ah,0dh,'$'
str2 db 0ah,0dh,'Input number2 :',0ah,0dh,'$'
str3 db 0ah,0dh,'Output number1 bin :',0ah,0dh,'$'
str4 db 0ah,0dh,'Output number2 bin :',0ah,0dh,'$'
str5 db 0ah,0dh,'Judge number1 oushu :',0ah,0dh,'$'
str6 db 0ah,0dh,'Judge number2 oushu :',0ah,0dh,'$'
str7 db 0ah,0dh,'Output powsum :',0ah,0dh,'$'
str8 db 0ah,0dh,'Program End !!!',0ah,0dh,'$'
num dw 2 dup(?)
str db 5,?,5 dup(?)
a dw ?
d dw ?
f dw ?
data ends
code segment
assume cs:code,ds:data
main proc far ;主函数
start:
mov ax,data
mov ds,ax
lea dx,str1
mov ah,09h
int 21h
call input
mov ax,num
mov num+2,ax ;将输入的第一个16进制数存到num+2中
lea dx,str2
mov ah,09h
int 21h
call input ;第二个输入的16进制数存到num中
lea dx,str3
mov ah,09h
int 21h
mov bx,num+2
call bin ;将第一个数以2进制输出
lea dx,str4
mov ah,09h
int 21h
mov bx,num
call bin ;将第二个数以2进制输出
lea dx,str5
mov ah,09h
int 21h
mov bx,num+2
call oushu ;判断第一个数是否为偶数
lea dx,str6
mov ah,09h
int 21h
mov bx,num
call oushu ;判断第二个数是否为偶数
lea dx,str7
mov ah,09h
int 21h
call powsum ;将两数平方相加并以16进制输出
lea dx,str8
mov ah,09h
int 21h
mov ax,4c00h
int 21h
main endp
input proc near ;子程序1:输入16进制数
lea dx,str
mov ah,0ah
int 21h
lea si,str+2
mov cx,4
mov dx,0
A1:
push cx ;保护cx防止被后面左移改变
mov al,[si]
cmp al,'0' ;判断与0的大小,小于则退出
jb A4
cmp al,'F' ;判断与F的大小,大于则退出
ja A4
cmp al,'9' ;判断与9的大小
jbe A2
sub al,07h
A2:
sub al,30h
cbw ;扩大al为ax,用于存4位16进制数
add dx,ax ;加到dx,dx用来存每次变化后的结果
cmp cx,1 ;防止在将第4位数加入后仍左移
jz A3
mov cl,4
shl dx,cl ;dx左移4位,便于下一位16进制数加入dx
A3:
inc si
pop cx
loop A1
A4:
mov num ,dx
ret
input endp
bin proc near ;子程序2:输出2进制,颜色交替
mov cx,8 ;循环8次,每次输出两位颜色交替2进制数
mov dx,0
B1:
mov ax,0
shl bx,1
adc al,0 ;循环左移1位,将cf表示位最高位2进制数
push cx ;保护cx避免后面重复输出1次时发生改变
push bx ;保护bx避免改变
add al,30h
mov cx,1
mov bl,04h ;设置为红色
mov bh,0
mov ah,09h
int 10h
mov ah,03h ;03号功能读取当前光标位置,确定dh的有坐标和dl的x坐标
int 10h
inc dl ;光标右移1位
mov ah,02h ;02号功能设置光标位置
int 10h
pop bx
mov ax,0
shl bx,1
adc al,0
push bx
add al,30h
mov cx,1
mov bl,0ah ;设置为浅绿色
mov bh,0
mov ah,09h
int 10h
mov ah,03h
int 10h
inc dl
mov ah,02h
int 10h
pop bx
pop cx
loop B1
ret
bin endp
oushu proc near ;子程序3:判断偶数并输出
push bx ;保护bx
and bx,0001h ;取2进制的末位
mov cx,8
C1:
cmp bl,0h ;判断2进制末位为0则偶数
jz C2
mov ah,02h ;非偶数输出’NO’
mov dl,'N'
int 21h
mov ah,02h
mov dl,'O'
int 21h
pop bx
jmp C3
C2:
pop bx
mov cx,5
mov ax,bx
mov bx,10
C4:
mov dx,0
div bx ;除10,余数压栈
add dl,30h
push dx
loop C4
mov cx,5
C5:
pop dx
mov ah,02h ;10进制输出
int 21h
loop C5
C3:
ret
oushu endp
powsum proc near ;子程序4:计算平方和
mov ax,num
mul num
mov a,ax
mov d,dx
mov ax,num+2
mul num+2
add a,ax
adc d,dx ;将ax相加的进位与dx相加
adc f,0 ;dx相加可能产生进位,cf为最高位16进制数
mov dx,f
add dl,30h
mov ah,02h
int 21h
mov cx,4
mov dx,d
D1:
push cx
mov cl,4
rol dx,cl ;循环左移,
push dx
and dl,0fh ;dx最末位为所要输出的16进制数
cmp dl,9
jbe D2
add dl,7h
D2:
add dl,30h
mov ah,02h
int 21h
pop dx
pop cx
loop D1
mov cx,4
mov dx,a
D3:
push cx
mov cl,4
rol dx,cl ;循环左移
push dx
and dl,0fh ;ax最末位为所要输出的16进制数
cmp dl,9
jbe D4
add dl,7h
D4:
add dl,30h
mov ah,02h
int 21h
pop dx
pop cx
loop D3
ret
powsum endp
code ends
end main
文件地址:
https://download.csdn.net/download/weixin_45330449/12464854
运行截图