So I am using spim and I am trying to figure out how to take a users input say "16" and then print it as 9 digit binary number. for example 16, 000010000 or say 4, 000000100, 67 01000011 etc etc always 9 spaces and in binary. I have all the input and I am currently just printing the number itself, but I want to print the binary anyone got any ideas/ tips?

Recommended Answers

All 2 Replies

Try something like this:

NineDigits db "123456789$" ;the target sting - any old junk will do

mov cx,9            ;loop count
move ax,value       ;value to convert
move bx NineDigits  ;Pointer to NineDigits
LoopHere:
    shr ax          ;low order bit into carry flag
    mov [bx],'0'    ;Assume bit is not set
    jnc NextDigit   ;if no cary flag, it was 0
    mov [bx],'1'    ;if carry flag, it is 1
NextDigit:
    inc bx
    loop LoopHere
    ; code to print the NineDigits

Forgot offset operator on pointer load. Line 5 should have been:

 move bx offset NineDigits ;Load Pointer to NineDigits

The offset could also be coded directly into the instruction like this:

 mov NineDigits[bx], '0'

In this case bx is just an offset into the array. The mov [bx],'1' could also be replaced with an inc. I also notice that I built the string in reverse. I can also get rid of the loop instruction by using bx as both a loop count and an offset. With these changes, the whole snippet then becomes:

NineDigits db "123456789$" ;the target string - any old junk will do

    move ax,value          ;value to convert
    mov bx,8               ;Offset to end of string

LoopHere:
    shr ax                 ;low order bit into carry flag
    mov NineDigits[bx],'0' ;Store char '0' in array
    jnc PrevDigit          ;if no cary flag, leave it '0'
      inc NineDigits[bx]   ;if carry flag, turn '0' into '1'

PrevDigit:
    dec bx                 ;previous element in array
    jb LoopHere            ;loop till bx is negative

; more of your code to print the NineDigits
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.