概述
要求
从键盘输入2个数X1和X2,然后再输入计算符号(+或*),按输入的符号进行计算,结果放在X3,并输出到屏幕显示。输入“+”号,程序计算X3=X1+X2, 输入“*”号计算X3=X1 * X2
思路
定义数据段变量x1,x2,x3分别存储数据,再定义input、output保存输入的ASCII码值、输出的十进制数;
程序从键盘读入的数据(十进制)是以十进制存入的(即存入的是图形),要进行运算就需要将数据转换为二进制储存到寄存器中;
同样,数据输出到屏幕时,需先将数据转换成ASCII码十进制显示;
因此,定义了两个函数ASCB、BASC,方便调用
实现
data segment
input db 10,0,10 dup(30h) ;输入的数据ascii码
x1 dw 0
x2 dw 0
x3 dw 0
output db 5 dup(30h),'$'
mes1 db 'input first number',0ah,0dh,'$'
mes2 db 'input second number',0ah,0dh,'$'
mes3 db 'input computing symbol(+ or *) ',0ah,0dh,'$'
mes4 db 0ah,0dh,' computing result =','$'
data ends
code segment
assume cs:code,ds:data,ss:stacks
main proc
mov ax, data
mov ds, ax
;第一个数字
mov dx,offset mes1
mov ah,09h
int 21h
mov dx,offset input
mov ah,0ah
int 21h
mov ch,00h
mov cl,input+1
cmp cl,05h
jc l1
mov cl,05h
l1:
mov si,offset input+2
call ascb
mov x1,ax
mov dl,0ah ;回车
mov ah,2
int 21h
mov dl,0dh ;换行
mov ah,2
int 21h
;第二个数字
mov dx,offset mes2
mov ah,09h
int 21h
mov dx,offset input
mov ah,0ah
int 21h
mov ch,00h
mov cl,input+1
cmp cl,05h
jc l2
mov cl,05h
l2:
mov si,offset input+2
call ascb
mov x2,ax
mov dl,0ah ;回车
mov ah,2
int 21h
mov dl,0dh ;换行
mov ah,2
int 21h
mov dx,offset mes3
mov ah,09h
int 21h
;输入运算符
mov ah,1
int 21h
cmp al,'*'
je mulnum
;计算
addnum:
mov ax,x1
add ax,x2
mov x3,ax
mov dx,ax
mov si,offset output
call basc
jmp done
mulnum:
mov ax,x1
mov bx,x2
mul bx
mov x3,ax
mov dx,ax
mov si,offset output
call basc
done:
mov dx,offset mes4
mov ah,09h
int 21h
mov dx,offset output
mov ah,09h
int 21h
mov ah,7
int 21h
mov ax, 4c00h ;程序返回
int 21h
main endp
ascb proc
dec cl
mov bx,000ah
mov ah,00h
mov al,[si]
sub al,30h
a1:
imul bx
mov dx,[si+01]
and dx,00ffh
add ax,dx
sub ax,30h
inc si
loop a1
ret
endp
basc proc
add si,5
a2:
dec si
mov ax,dx
mov dx,0000h
mov cx,000ah
div cx
xchg ax,dx
add al,30h
mov [si],al
cmp dx,0000h
jnz a2
ret
endp
code ends
end main
最后
以上就是鳗鱼钢笔为你收集整理的微机原理汇编实验一:数值计算程序设计的全部内容,希望文章能够帮你解决微机原理汇编实验一:数值计算程序设计所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复