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

时间:2014-11-17 11:40:32

标签: assembly masm irvine32 ia-32

您好我正在尝试创建一个代码,该代码将在< 0;范围内传输十进制数字。 65 535>到二进制,到目前为止,我的想法是将原始的字符串数字转移到数字,然后在BX中将其除以2,因此结果将在AX中,其余部分在DX中...但到目前为止我还不知道怎么去此外,我可能会比较剩余区域中是否有0或1,即每个除法后的DX,然后通过新的String中的符号来写它?

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

1 个答案:

答案 0 :(得分:1)

您实际上正在做的是将包含十进制数字的ASCII表示的字符串更改为寄存器中的等效数字。执行此操作的典型方法是从最高位开始,并将前一个结果重复乘以10并添加数字的值。在伪代码中:

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