在这个程序中,我很难将字符串转换为数字。我已经花了大约10个小时使用这个代码(是的,我是新手)而且我有一种强烈的感觉,我会非常接近它将要工作的那一点......请证明我是正确的或证明我错了! =)我试图在最复杂的(对我来说)部分代码中添加尽可能多的注释,这样你就可以节省时间,以防你有心情帮助我。如果有必要,我会很高兴解释代码。抱歉我的英文,让我们看看代码。
model tiny
.code
org 0100h
start:
mov ax,cs
mov ds,ax
mov dx,offset text
mov ah,09h
int 21h
mov dx,offset x
mov ah,3fh
int 21h
mov dh,byte ptr[x+di] ; Loading first digit into elem.
sub dh,48
mov ah,2
CH2INT:mov elem,dh
_TEST: push di
OFFST1:mov cl,ah
dec cl
add di,di
loop OFFST1
mov dh,byte ptr[x+di] ; Loading next (x+di*2) digit into dh.
pop di ; Inc ah for the next iteration.
inc ah ; Here we see if next char is a digit or not,
cmp dh,"0" ; if it is (meaning that current digit isn't in the lowest position) -
jb INVAL ; multiplying current digit by 10 (our base),
cmp dh,"9" ; if not - jump to INVAL where we go on to the next digit (if there is any).
ja INVAL ; Push-pop ax just for convinience, I'm almost out of free-to-use registers.
push ax
mov al,elem
mov ten,10
imul ten
mov elem,al
pop ax
jmp _TEST
INVAL: mov al,elem ; Adding number we got to the sum which in the end (after jump to NEXT)
add sum,al ; will containe inputt size of array as a number, not a char.
push di
OFFST2:mov cl,ah
dec cl
add di,di
loop OFFST2
mov dh,byte ptr[x+di] ; Move next digit into dh (x+ah(wich was incremented earlier)+di).
pop di ; See if there is a next digit in x or previous one was the last one
cmp dh,"0" ; (I just hope that next value in memory after the inputt will not be a digit...
jb NEXT ; I understand that it's not nice and a luck game, but I do not yet come up with smthn better).
cmp dh,"9"
ja NEXT
push di ; Next few lines write next char into dh (but with index less by 1 than in previous
; few lines, where we checked is there any more digits in the inputt value left.
OFFST3:mov cl,ah
sub cl,2
add di,di
loop OFFST3
mov dh,byte ptr[x+di]
pop di
sub dh,48
jmp CH2INT ; Jump to next iteration to work with next digit in the inputt value
NEXT: mov dx,offset array
mov ah,3fh
int 21h
mov cl,sum
L: mov dh,byte ptr[array+di]
inc di
mov dl,dh
mov ah,02h
int 21h
loop L
mov ah,4Ch ; Service 4Ch - Terminate with Error Code
mov al,0 ; Error code
int 21h ; Interrupt 21h - DOS General Interrupts
ret
array db 256 dup (?) ; Array
x db ? ; Size of the array
ten db ?
text db "Enter number of elements in array: $"
elem db ? ; A single digit from the inputt value
sum db 0 ; Inputt value transformed into number
end start
在TASM中用.com可执行文件编写的代码。它在这里和那里有一些不合理的东西,但它只是为了清晰,因为它只是一个草案。非常感谢您的帮助!谢谢!
P.S。我会随着时间的推移修改代码,以防你给我一些建议。