题
我正在尝试使用下面的代码转换一个整数 ax
到ASCII代码。但是运行它的输出为515,而不是我预期的513。错误在哪里?
stk segment
dw 32 dup(0)
stk ends
dts Segment
posnum db 6 dup(0) ;0<x<65536;
dts ends
cds Segment
Assume cs:cds , ds:dts,ss:stk
Main Proc Far
mov ax,seg dts
mov ds,ax
mov es,ax
xor ax,ax
mov ax,513
mov di,offset posnum
Call ConvertPositive
mov ah,09h
mov dx ,offset posnum
int 21h
main_finish:
mov ah,08h
int 21h
mov ax,4c00h
int 21h
Main endp
cds Ends
procs segment
assume cs:procs
ConvertPositive proc far
xor dx,dx
xor cx,cx
mov bl,10
mov bh,0
divloop:
mov dx,0
div bx
add dl,30h
mov byte ptr [si],dl
inc cl
inc si
cmp ax,0
jne divloop
enddiv:
dec si
copy:
std
LODSB
cld
STOSB
loop copy
mov byte ptr [di],'$'
ret
ConvertPositive endp
procs ends
end Main
解决方案
SI
不是初始化的。它应该指向分配的反向顺序数字的缓冲区。
其他提示
您的分隔环路看起来不错
#include <stdio.h> #include <stdlib.h> #include <string.h> int main ( void ) { unsigned int ax; unsigned int dx; ax=513; while(ax) { dx=ax%10; ax=ax/10; printf("%c",dx+0x30); } printf("\n"); }
我从上述循环中获得315。
我怀疑当您尝试将字符串翻转为5时,我会踩在3上。
不隶属于 StackOverflow