atoi
アセンブリで関数を作成し、既に定義されている文字列でテストすると正常に動作int 21h / 3Fh
しますが、ユーザーから文字列を取得するために使用すると (保存してこの関数に渡します)、無意味な値が得られます。
私は学校でこれを行っており、作成中のライブラリに入れていますがitoa
、これには問題のない機能が含まれています。
関数は、アドレスが以前に に格納されている文字列を、 に格納さSI
れている整数に変換しますAX
。のコンテンツを表示するため
に使用します。itoa
AX
誰かが問題を知っていることを願っています。
これは動作しない例です:
mov ah, 3fh ;get string
mov dx, stri
int 21h
mov si, stri ;convert it
call z_atoi
stri db '$$$$$$$$$$$$$$$$$$'
実際の例:
mov si, stri
call z_atoi
stri db '789$'
関数:
z_atoi: ;z_atoi(si=str) return ax=[number]
xor dh, dh ;needs to be zero for dx==dl
xor ax, ax ;the number
xor cx, cx ;the character
z_atoi1:
test cx, cx ;is this the first character
je z_atoi2 ;if so skip this
imul ax, 10 ;multiply by 10
z_atoi2:
mov bx, si
add bx, cx
mov dl, [bx] ;get next character
sub dl, 30h ;ascii to integer
add ax, dx ;add to the number
inc cx ;next character
inc bx
cmp byte[bx], '$' ;check if there are any more characters
je z_atoi3
cmp byte[bx], 0
je z_atoi3
cmp byte[bx], 0ah ;<--THIS WAS WHAT I WAS MISSING, THANKS
je z_atoi3
jmp z_atoi1
z_atoi3:
ret