正在学习汇编,教材就是80x86汇编语言程序设计教程,今天看到一个例题,按书上的写了一下,发现并不能达到预期的效果,就开始检查,终于被我找到了,在dispnum段中的mov cx,maxlen应改为mov cx,bufflen。这样就行了。源代码如下,功能是输入两个数(十位以内),将它们相加。
assume cs:code,ds:data
maxlen=10
bufflen=maxlen+1
data segment
buff1 db bufflen,0,bufflen dup(?)
num1 equ buff1+2
buff2 db bufflen,0,bufflen dup(?)
num2 equ buff2+2
result db bufflen dup(?),24h
digitl db '0123456789'
diglen equ $-digitl
mess db 'invalid number |',0dh,0ah,024h
data ends
code segment
start:mov ax,data
mov ds,ax
mov es,ax
mov dx,offset buff1
call getnum
jc over
mov dx,offset buff2
call getnum
jc over
mov si,offset num1
mov di,offset num2
mov bx,offset result
mov cx,maxlen
call addition
mov dx,offset result
call dispnum
jmp short ok
over:mov dx,offset mess
mov ah,9
int 21h
ok: mov ah,4ch
int 21h
getnum proc
mov ah,10
int 21h
call newline
call isdnum
jc getnum2
mov si,dx
inc si
mov cl,[si]
xor ch,ch
mov ax,maxlen
std
mov di,si
add di,ax
add si,cx
sub ax,cx
rep movsb
mov cx,ax
jcxz getnum1
xor al,al
rep stosb
getnum1:cld
clc
getnum2:ret
getnum endp
addition proc
std
add bx,cx
add si,cx
add di,cx
dec si
dec di
xchg di,bx
inc bx
clc
addp1:dec bx
lodsb
adc al,[bx]
aaa
stosb
loop addp1
mov al,0
adc al,0
stosb
cld
ret
addition endp
dispnum proc
mov di,dx
mov al,0
mov cx,bufflen
repz scasb
dec di
mov dx,di
mov si,di
inc cx
dispnum2:lodsb
add al,30h
stosb
loop dispnum2
mov ah,9
int 21h
ret
dispnum endp
isdnum proc
mov si,dx
lodsb
lodsb
mov cl,al
xor ch,ch
jcxz isdnum2
isdnum1:lodsb
call isdecm
jnz isdnum2
loop isdnum1
ret
isdnum2:stc
ret
isdnum endp
isdecm proc
push cx
mov di,offset digitl
mov cx,diglen
repnz scasb
pop cx
ret
isdecm endp
newline proc
push dx
push ax
mov dl,0dh
mov ah,2
int 21h
mov dl,0ah
int 21h
pop ax
pop dx
ret
newline endp
code ends
end start