在x86程序集中将整数转换为ASCII代码的问题

时间:2011-01-09 14:40:30

标签: assembly x86 16-bit

我正在尝试使用下面的代码将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

2 个答案:

答案 0 :(得分:1)

SI未初始化。它应该指向为反向数字分配的缓冲区。

答案 1 :(得分:0)

你的分频循环看起来不错

#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.尝试一个像713这样的数字,看看它是否会产生717. 523如果你得到525等等我猜测结果是4334的1234结果。

相关问题