Assembly 32位汇编(IA-32)中的十进制到二进制

Assembly 32位汇编(IA-32)中的十进制到二进制,assembly,masm,irvine32,x86,Assembly,Masm,Irvine32,X86,您好,我正在尝试制作一个代码,将十进制数转换为二进制数,到目前为止,我的想法是将原始字符串数转换为数字,然后在BX中将其除以2,因此结果将在AX中,余数在DX中。。。但到目前为止,我还不知道如何进一步,我想也许可以比较一下,在每个除法之后的剩余区域(DX)中是否有0或1,然后用符号将其写入新字符串 TITLE MASM Vstup_výstup (main.asm) INCLUDE Irvine32.inc .data Number DB 9 dup(?),0Dh,0Ah,§ Result D

您好,我正在尝试制作一个代码,将十进制数转换为二进制数,到目前为止,我的想法是将原始字符串数转换为数字,然后在BX中将其除以2,因此结果将在AX中,余数在DX中。。。但到目前为止,我还不知道如何进一步,我想也许可以比较一下,在每个除法之后的剩余区域(DX)中是否有0或1,然后用符号将其写入新字符串

TITLE MASM Vstup_výstup (main.asm)

INCLUDE Irvine32.inc
.data
Number DB 9 dup(?),0Dh,0Ah,§
Result DB 9 dup(?),0Dh,0Ah,§

.code
main PROC

    call Clrscr
    mov ebp, offset Number      ;first number's adress is saved to ebp
    mov esp, offset Result
    mov ecx, 20                 ;
    mov edi, 1                  ;           
    call ReadString
    mov ax, Number              ;
    sub ax, '0';                ;String to Number by substracting 0 
    mov bx, 2                   ;save 2 to 16 bit bx register
Start:  
    mov cl, [ebp+edi]           ; something as a counter to know when to end with dividing ?
    inc edi                     ; increase edi by one
    cmp cl, §                   ; compare if the counter already came here division will jump to Ending
    je Ending       
    div bx                      ; divide ax with bx, which means that result will be in AX and remainder in DX
    cmp dx,0
    je Zero                     ; if the remainder is 0 it will write 0 in the new string 
    jmp Start                   ; jmp to start

Zero:
    mov esi, [esp+edi]


Ending:

    exit
main ENDP

END main

看起来您实际上在做的是将包含十进制数字ASCII表示形式的字符串更改为寄存器中等效的数字。执行此操作的典型方法是从最有效的数字开始,重复将之前的结果乘以10,然后将数字的值相加。在psuedo代码中:

value = 0
for each digit, starting with the most significant:
    value = value * 10 + digitvalue

如果你这样做的话,你显然必须从最重要的数字开始(无论你从什么开始,最多乘以10)@harold:噢!谢谢你指出那个错误。我会找到答案的。