I am very new to assembly.  I am trying to convert some numbers to binary.  I have written most of the code so far, but can't figure out how to convert to binary.  The user enters a number that number is stored in one register in another final register initialized to 0 the value is multiplied by 10 and the stored number is added so that if the user enters 1 the answer is one if they then enter a 2 the answer is 12, and so on.  I then have to convert the final number entered to binary.  That is where I am stuck.  Here is what I have so far...I have been trying test, but that doesnt seem to work. 
 mov         eax,char_prompt
            call        print_string            ; request a char. input
            call        print_nl
            mov        edx,0

L1:                                            ;Loop 1
            call        get_kb                  ; read input character
            cmp        AL, 0Dh                ; compare the key entered to CR
            je          false2
            cmp        AL, '0'                ; compare AL to 0
            jl          false2                  ; no match do second else

            cmp        Al, '9'                ;compare AL to 9
            jg          false2                  ;no match do second else
            sub        al,'0'                  ; subtract 0 from what ever is in AL
            mov        ,al            ;move whatever is in AL to inputdigit
            imul        edx,10                ;multiply edx by 10
            add        edx,            ;add value in input1 to value in edx
            mov        eax,edx                ;move value in edx to eax
            call        print_int
            call        print_nl
            loop        L1
            mov        eax,out_msg3
            call        print_string            ;jump to error statement
            call        print_nl
Posted on 2008-06-20 00:47:35 by cannsyl
When you have the decimal number in a DWORD var or a register,  to show that into binary, for the 32 bits you just have to test whether the highest bit is set and print "0" or "1".

mov eax,value

mov ecx,0
.while ecx<32
    .if eax & 80000000h ; is highest bit set?
            print "1"
            print "0"
    shl eax,1 ; make bit 30 become bit 31

    inc ecx

But this will always print 32 bits, won't skip the leading zeroes. So, let's skip them:

mov eax,value
.if !eax
print "0"

xor ecx,ecx
.while !(eax & 80000000h)
shl eax,1
inc ecx

    .if eax & 80000000h
        print "1"
        print "0"
    shl eax,1
    inc ecx
    cmp ecx,32
    jl next_bit

Of course, many tricks spawn as optimizations or fun around that code
Posted on 2008-06-20 10:11:12 by Ultrano
Thanks so much for the help.  I kind of understand the concept you gave, but I can't use .if how would I do a !eax in assembly with gvim?  Thanks
Posted on 2008-06-23 10:12:06 by cannsyl
test eax,eax
jnz _notZero

So, the second code I posted above can be transformed with Carry-flag tricks like this:

PrintBinary proc uses edi Value,lpszText
mov edx,Value
mov edi,lpszText
test edx,edx
jz _onZero
xor ecx,ecx

mov ecx,32
;----[ find the first '1' bit ]----[
dec ecx
shl edx,1
jnc @B


;----[ restore that '1' bit we took-away ]---[
shr edx,1
or edx,80000000h

;---[ for each next bit ]----[
shl edx,1  ; move highest bit into Carry-Flag
mov al,'0' ; we want to print "0" or "1", so put "0" into AL
adc al,0   ; this will add 0 or 1 to al, depending on CarryFlag. So, if(CarryFlag==false)al="0"; else al="1";
stosb      ; equals "mov ,al | inc edi"
dec ecx    ; decrese number of bits left to check
jge next_bit ; repeat code if bits_left_to_check >=0 . (we had decremented ecx once too much while searching for the first "1", so we use ">=0" instead of ">0")

mov byte ptr,0 ;zero-terminate
mov word ptr,'0' ; put '0' in string, and zero-terminate
PrintBinary endp

MyStringResult db 100 dup (?)
invoke PrintBinary,0FFh,addr MyStringResult

invoke MessageBox,0,addr MyStringResult,0,0
Posted on 2008-06-23 11:54:50 by Ultrano
I just wrote this one in NASM. It returns the number of bytes copied to the destination string.

; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧  
    ; DWORD __DecToBinStr (DWORD DecimalValue, char* DestinationString, Boolean SkipTrailingZeros); StdCall;
    ; Returns the number of bytes copied to the string (Including the null-terminator)
    PUSH      EBX
    PUSH      ECX
    PUSH      EDX
    PUSH      ESI
    PUSH      EDI
    PUSH      EBP
    MOV      EBP , ESP
    ; = DecimalValue
    ; = DestinationString
    ; = SkipTrailingZeros

      XOR      ECX , ECX
      MOV      EDI , DWORD PTR ;
      MOV      ESI , DWORD PTR ;
      MOV      EDX , DWORD PTR ;
      MOV      EAX , 0x00000020

      ; EDI =
      ; ESI =
      ; EDX =

      TEST      EDI , EDI
      JZ        .EP
      INC      ECX
      MOV      EBX , 0x00000030
      TEST      ESI , ESI
      JNZ      .CheckForSkippingTrailingZeros
      MOV      DWORD PTR , EBX     
      JMP      .SetResult

      TEST      EDX , EDX
      JZ        .PrepareToTransfer
        DEC      EAX
        SHL      ESI , 0x00000001
        JNC      .RemoveLeadingZeros
        RCR      ESI , 0x00000001
        INC      EDX

    ; EDX = Free
    ; ECX = Return Value
    ; EBX = '0', 0, 0, 0
    ; EDI =
    ; ESI =
    ; EAX = Number of bits that we have to transfer to the destination string
      MOV      DWORD PTR , EBX
      SHL      ESI , 0x00000001
      JNC      .MoveForward
      INC      DWORD PTR
        INC      EDI
        INC      ECX
        DEC      EAX
        JNZ      .PrepareToTransfer

      MOV      EAX , ECX                 
      MOV      EAX , ECX
      POP      EBP
      POP      EDI
      POP      ESI
      POP      EDX
      POP      ECX
      POP      EBX
    RET      (0x03 * 0x04)
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧
Posted on 2008-06-28 15:13:56 by XCHG