键盘输入两个数,并计算和再输出!请各位帮忙看看
主要有immediate mode illegal
symbol not defined
错误
本人是菜鸟实在不知道怎么修改了!
我的分也少的可怜只能把我最后的一点给大家了!
datasegment
buf1db3,?,3 dup(?)
buf2db3,?,3 dup(?)
num1=0
num2=0
dataends
codesegment
assumecs:code,ds:data
start:
movax,data
movds,ax
movdx,offset buf1
movah,0ah
int21h
movax,bx
movnum1,ax
callconvert
calldisplay
movdx,offsetbuf2
movah,0ah
int21h
movax,bx
mov num2,ax
callconvert
calldisplay
subbl,bl
addbx,num1
addbx,num2;bx is the temporarily result
movax,bx;bx is sended to ax
callshift
movah,4ch
int21h
codeends
endstart
convertproc;convert the data of buffer to the number
movcx,0
movax,0
movbx,0
movcl,3
return:
deccl
moval,[dx]
subal,30h
jcxzadding
mul10;the sum send into ax
adding:
addbx,ax;result send into bx
jcxzend
incdx
jmpreturn
end:
ret
convertendp
display proc
movdl,13
movah,2
int21h
movdl,10
movah,2
int21h
ret
displayendp
shiftproc;convert the result and output
movbx,10
pushbx
dis:
cmpax,0
jzdis0
divbx;al is quotient,ah is residue
addah,30h
pushah
cbw
jmpdis
dis0:
popah
cmpah,10
jestop
movah,dl
movah,2
int21h
jmpdis0
stop:
ret
shiftendp
[解决办法]
; 马马虎虎地在原来的程序修改了下, 自己看看改动的地方, 就是 ;* 这样注释的地方
Data segment
buf1 db 4,?,4 dup(?) ;* 回车也占用一个空间的, 所以最多输入 3 位数值时, 需要定义 4个字节的空间
buf2 db 4,?,4 dup(?) ;*
num1 dw 0 ;* =0 ; num1/num2 需要进行存取操作, 所以应该定义为内存变量, 而不是常量
num2 dw 0 ;* =0
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov dx,offset buf1
mov ah,0ah
int 21h
call convert
mov ax,bx ;* ; convert 子程返回的值要先保存
mov num1,ax ;* ; 这两个指令可以合并为 mov num1, bx
call display
mov dx,offset buf2
mov ah,0ah
int 21h
call convert
mov ax,bx
mov num2,ax
call display
sub bl,bl ; ;bl=0
mov bx,num1 ;*
add bx,num2 ;bx is the temporarily result
mov ax,bx ; bx is sended to ax
call shift
mov ah,4ch
int 21h
convert proc ;convert the data of buffer to the number
mov cx,0
mov ax,0
mov bx,0
mov si, dx ;*+ 进入子程时, dx 指向整个缓冲区的开始
add si, 2 ;*+ si 指向实际输入的字符的开始
mov cl,byte ptr [si][-1] ;* 这个是实际输入的字符数, 直接使用 3 容错性差
mov dl, 10 ;*+ 对 mul 指令不能使用立即数的
return:
dec cl
mov al,[si] ;*[dx]
sub al,30h
xchg ax, bx ;* bx 保存的是前几位的内容, 所以要被 10 乘的是它, 而不是当前的数字位
; jcxz adding
mul dl ;the sum send into ax
adding:
add bx,ax ;result send into bx
jcxz l_end ; end is a key word
inc si ;* dx
jmp return
l_end:
ret
convert endp
display proc
mov dl,13
mov ah,2
int 21h
mov dl,10
mov ah,2
int 21h
ret
display endp
; 两个三位数相加, 结果可能会是四位数, 除以 10 之后会是三位数, 可能会大于 255
; 因此, 除以 10 应采用 dword/word. 如果是 word/byte 的话, 可能会溢出
shift proc ;convert the result and output
mov bx,10
push bx ;set a stack
dis:
cmp ax,0
jz dis0
mov dx, 0 ;*+ 为了兼容, 可以显示更大的数值, 还是采用 dword/word
div bx ;al is quotient,ah is residue
add dl,30h ;*
push dx ;* ah
; cbw
jmp dis
dis0:
pop ax ;* ;pop the data
cmp ax,10 ;*
je stop
mov dl, al ;* ah,dl
mov ah,2
int 21h
jmp dis0
stop:
ret
shift endp
code ends
end start