汇编语言:-999到999之间的两个数的加减乘除运算,结果范围为(-2^15到2^15-1)

用汇编语言实现两个三位数(-999到+999)的加减乘除运算,结果范围为(-2^15到+2^15-1),按ESC键退出。

【使用了自定义的宏库】

include tools1.mac
data segment
	show  db "==============================$"
	show1 db "Please input x:$"
	show2 db "Please input y:$"
	show3 db "Please input method<+-*/>:$"
	show4 db "Please input again:$"
	result db "The result is:$"
	x dw ?
	y dw ?
	k db 20 dup(?)
	error1 db "not input any number!$"
	error2 db "input not a number!$"
	error3 db "input only a '-'!$"
	error4 db "No more than three!$"
	error5 db "input method error!(must be ""+-*/"")$"
	error6 db "Overstep the boundary!$"
	w dw ?,?
	s dw ?,?
	j db ?
	r db ?
data ends
stack segment
stack ends
code segment
	assume ds:data,ss:stack,cs:code
start:
;;;;;加载数据段;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	mov ax,data
	mov ds,ax
;;;;;加载堆栈段;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	mov ax,stack
	mov ss,ax
;;;;;;;;;;;;;;;;;;;;;;;;;;主程序main开始;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
main proc far
;;;;;显示"=======================";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	display show
	ld
;;;;;显示"Please input x:";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	display show1
;;;;;;;传参寄存器bx清0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	mov bx,0
ss1:
;;;;;;;调用子程序inputnumber;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	call inputnumber
	cmp bx,0
	jz ss1
;;;;;;;;对输入的x进行处理;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	call dealwith
	mov x,ax  ;把处理好的数据存入x单元
;;;;;;;先回车换行,否则将覆盖"Please input x:xxx";;;;;;;;;;;;;;;;;;;;;;;;;;
	ld
;;;;;显示"Please input y:";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	display show2
;;;;;;;传参寄存器bx清0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	mov bx,0
ss2:
;;;;;;;调用子程序inputnumber;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	call inputnumber
	cmp bx,0
	jz ss2
;;;;;;;;对输入的y进行处理;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	call dealwith
	mov y,ax  ;把处理好的数据存入y单元
;;;;;;;;调用method子程序对x和y进行运算;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	call method
;;;;;;;;;;对结果处理;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	dresult
main endp
;;;;;;;;;;;;;;;;;;;;;;;;;;主程序main结束;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;子程序inputnumber开始;;;;;;;;;;;;;;;;;;;;;;;;;;;;
inputnumber proc near
;;;;;;从键盘录入一个字符;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	input
	cmp al,1bh   ;判断是否是ESC键,如果是,则退出
	jz outt
	cmp al,0dh   ;判断录入的字符是否是回车
	jz exit
;;;;;;;录入的不是回车,先检查是否输入的是负号;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	cmp al,'-'
	jz minus
;;;;;;;;录入的不是负号,则检查录入的是否是数字;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	cmp al,'0'
	jl errortwo
	cmp al,'9'
	jg errortwo
;;;;;;;;;录入的是数字,则将其存放在预定义的存储区里;;;;;;;;;;;;;;;;;;;;;;;;
	mov k[bx],al
	inc bx
	jmp inputnumber   ;循环录入
minus:
	cmp bx,0  ;是否是第一个字符为负号
	jnz errortwo
	mov k[bx],al ;把负号存在0号单元
	inc bx
	jmp inputnumber
exit:
	cmp bx,0   ;第一个字符是回车(未录入)
	jz errorone
	cmp k[0],'-'
	jz negative
	cmp bx,3     
	jg errorfour   ;输入超过3位
	jmp final
negative:
	cmp bx,1   ;bx为1,说明只有一个字符,而且可以知道是负号
	jz errorthree
	cmp bx,4
	jg errorfour   ;输入超过三位(除了负号)
final:
;;;;;;;把最后的回车填到末单元;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	mov k[bx],al
	ret        ;返回主程序
;;;;;;;;;显示错误信息1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
errorone:
	;mov ax,seg error1
	;mov ds,ax 
	ld
	display error1
	ld
	display show4
	
	jmp inputnumber
;;;;;;;;;显示错误信息2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
errortwo:
	ld
	display error2
	ld
	display show4
	
	mov bx,0
	jmp inputnumber
;;;;;;;;;显示错误信息3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
errorthree:
	ld
	display error3
	ld
	display show4
	
	mov bx,0
	jmp inputnumber
;;;;;;;;;显示错误信息4;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
errorfour:
	ld
	display error4
	ld
	display show4
	
	mov bx,0
	jmp inputnumber
inputnumber endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;子程序inputnumber结束;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;子程序dealwith开始;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dealwith proc near
	cmp k[0],'-'  ;如果是负数
	jz negativetreatment
	cmp bx,1  ;如果是一位数
	jz onedigit
	cmp bx,2  ;如果是两位数
	jz twodigit
	cmp bx,3  ;如果是三位数
	jz threedigit
onedigit:
	mov al,k[0]
	and al,0fh   ;去掉ASCII码
	cbw
	ret    ;返回主程序
twodigit:
	mov al,k[0]
	and al,0fh
	mov cl,10
	imul cl
	mov s,ax
	mov al,k[1]
	and al,0fh
	cbw
	add ax,s 
	ret    ;返回主程序
threedigit:
	mov al,k[0]
	and al,0fh
	mov cl,100
	imul cl
	mov s,ax
	mov al,k[1]
	and al,0fh
	mov cl,10
	imul cl
	add ax,s
	mov s,ax
	mov al,k[2]
	and al,0fh
	cbw
	add ax,s
	ret    ;返回主程序
negativetreatment:
	cmp bx,2  ;如果是一位数
	jz onedigitneg
	cmp bx,3  ;如果是两位数
	jz twodigitneg
	cmp bx,4  ;如果是三位数
	jz threedigitneg

onedigitneg:
	mov al,k[1]
	and al,0fh
	neg al
	cbw
	ret    ;返回主程序
twodigitneg:
	mov al,k[1]
	and al,0fh
	mov cl,10
	imul cl
	mov s,ax
	mov al,k[2]
	and al,0fh
	cbw
	add ax,s
	neg ax
	ret    ;返回主程序
threedigitneg:
	mov al,k[1]
	and al,0fh
	mov cl,100
	imul cl
	mov s,ax
	mov al,k[2]
	and al,0fh
	mov cl,10
	imul cl
	add ax,s
	mov s,ax
	mov al,k[3]
	and al,0fh
	cbw
	add ax,s
	neg ax
	ret    ;返回主程序
dealwith endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;子程序dealwith结束;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;子程序method开始;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
method proc near
	ld
	display show3
	input
	cmp al,'+'
	jz addmethod
	cmp al,'-'
	jz submethod
	cmp al,'*'
	jz imulmethod
	cmp al,'/'
	jz idivmethod
	jmp errorfive
addmethod:
	mov ax,x
	add ax,y
	mov w,ax
	mov r,1
	ret    ;返回主程序
submethod:
	mov ax,x
	sub ax,y
	mov w,ax
	mov r,1
	ret    ;返回主程序
imulmethod:
	mov ax,x
	imul y
	mov w,ax
	mov w+2,dx
	ret    ;返回主程序
idivmethod:
	mov ax,x
	cwd
	idiv y
	mov w,ax  ;只传商,舍弃余数
	ret    ;返回主程序
;;;;;;;;;显示错误信息5;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
errorfive:
	ld
	display error5
	jmp method
method endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;子程序method结束;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
outt:
	retsys
code ends
end start

运行结果如下:

1.两个一位数的加减乘除,结果在0-9


2.两个一位数的加减乘除,结果小于0


3.两个多位数的加减乘除,结果在10-100


4.结果在100-1000


扫描二维码关注公众号,回复: 1904473 查看本文章

5.结果在1000-32767



6.结果小于-10



猜你喜欢

转载自blog.csdn.net/baidu_38760069/article/details/80328932
今日推荐