使用数组将十进制转换为二进制

问题描述:

我是emu8086中的一个begginer,而且我似乎无法修复此代码。我需要从十进制转换为二进制,有时它做得很好,例如,当我使用像4,8,15,16,255这样的数字时,一切正常。但是,如果我使用例如2,9,17,254,它不会显示正确的数字。我真的需要帮助。使用数组将十进制转换为二进制

.model small 

.data 

exp db 8 dup (?) 

num dw 09 

var dw 2 

.code 

start: 
    mov ax,@data 
    mov ds,ax 

    mov di,0 
    mov ax,num ;I put my number in ax 

    Binary: ;Here I make the conversion from decimal to binary 
     div var 
     mov exp[di],dl 
     inc di 
     cmp al,0 ;If my number is equal to 0 it breaks the cicle and shows the array in the next function 
     ja Binary 


    dec di  
    mov cx,di   
    Show: ;Here I show the array backwards so we can see the real binary number 
     mov bl,exp[di] 
     add bl,30h 

     mov dl,bl 
     sub bl,30h 

     mov ah,2 
     int 21h  
     dec di 
    loop Show 

int 21h  
end start: 

+0

不要使用'div'除以2!它比一个班次慢大约30倍,并且不能立即操作。 https://stackoverflow.com/questions/40354978/why-is-this-c-code-faster-than-my-hand-written-assembly-for-testing-the-collat​​/40355466#40355466 –

+0

谢谢彼得。 –

div var除以dx:ax通过var。在除法指令之前,您需要将dx置零。

+0

有一个规范的问答没有零或签署扩展到[e] dx:[e] ax div或idiv之前,这是一个非常好的dup目标。 ([x86标记wiki](https://stackoverflow.com/tags/x86/info)中有链接,只需搜索'div')。 –