Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Little Problem with converting from decimal to binary

LavitaLavita Member Posts: 3
Hi,

I've a little problem with convertion from decimal to binary in the assembly. If anyone can help me on this. By the way, I'm assembly self learner.

Eg. If user enter decimal integer 10, it should display "00001010"

Million Thanks in Advance.

with regards,
Lavita

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Hi,
    :
    : I've a little problem with convertion from decimal to binary in the
    : assembly. If anyone can help me on this. By the way, I'm assembly
    : self learner.
    :
    : Eg. If user enter decimal integer 10, it should display "00001010"
    :
    : Million Thanks in Advance.
    :
    : with regards,
    : Lavita
    :
    [color=Blue]
    If you search this board you may find the answer.

    Basically, you have the value, say 10 (ten) in a register. You prepare a mask in other register, where only one bit is set, say very left bit: 10000000. And then run a loop 8 times every time shifting the mask and checking the bit against the value. If bit is set - store symbol '1' in your output buffer, if not - store '0'.

    Mask is shifted to the right by one bit, so mask points each time to next bit location:

    10000000
    01000000 - shifted right
    00100000 - shifted right again
    00010000 - shifted right more
    ... and so on

    Code may be as follows:

    [code]
    .data

    buf db '--------$'

    .code

    mov ax, 10 ; Load the value to convert
    mov dx, 80h ; Mask with first bit set = 10000000 in binary
    mov cx, 8 ; How many bits to scan
    mov di, OFFSET buf ; Load address of a first symbol in output buffer

    next_bit:
    test ax, dx ; Testing the bit
    jz store_zero ; Bit is not set

    mov bl, '1' ; Bit is set at this point
    jmp store_ascii_bit

    store_zero:
    mov bl, '0'

    store_ascii_bit:
    mov [di], bl ; Store into 'buf' (DI points to it)
    inc di ; Advancing the address for next store
    shr dx, 1 ; Shifting the mask
    loop next_bit
    [/code]
    NOTE: Code is not tested. Try it.
    [/color]
  • LavitaLavita Member Posts: 3
    Hi Guru,

    Thank you so much for your prompt response. Basically, I have these codes. I just like to convert the decimal which user entered to binary.


    .MODEL SMALL
    .DATA
    INDEC DB 0,1
    VAR1 DB 0
    TEN DB 10

    MSG1 DB 'Enter 2 Digit Integer : ','$'
    MSG2 DB 0DH,0AH,'You Entered the Integer ','$'
    MSG3 DB 13,10,'Its binay representation is ','$'

    .STACK
    .CODE
    .STARTUP


    LEA DX,MSG1
    MOV AH,9
    INT 21H

    MOV AH,1
    INT 21H

    SUB AL,48
    MOV INDEC,AL
    MUL TEN
    MOV VAR1,AL

    MOV AH,1
    INT 21H

    SUB AL,48
    MOV INDEC+1,AL
    ADD AL,VAR1
    MOV VAR1,AL

    LEA DX,MSG2
    MOV AH,9
    INT 21H



    LEA DX,MSG3
    MOV AH,9
    INT 21H



    FIN: MOV AH,4CH
    INT 21H
    .EXIT
    END


    : : Hi,
    : :
    : : I've a little problem with convertion from decimal to binary in the
    : : assembly. If anyone can help me on this. By the way, I'm assembly
    : : self learner.
    : :
    : : Eg. If user enter decimal integer 10, it should display "00001010"
    : :
    : : Million Thanks in Advance.
    : :
    : : with regards,
    : : Lavita
    : :
    : [color=Blue]
    : If you search this board you may find the answer.
    :
    : Basically, you have the value, say 10 (ten) in a register. You
    : prepare a mask in other register, where only one bit is set, say
    : very left bit: 10000000. And then run a loop 8 times every time
    : shifting the mask and checking the bit against the value. If bit is
    : set - store symbol '1' in your output buffer, if not - store '0'.
    :
    : Mask is shifted to the right by one bit, so mask points each time to
    : next bit location:
    :
    : 10000000
    : 01000000 - shifted right
    : 00100000 - shifted right again
    : 00010000 - shifted right more
    : ... and so on
    :
    : Code may be as follows:
    :
    : [code]:
    : .data
    :
    : buf db '--------$'
    :
    : .code
    :
    : mov ax, 10 ; Load the value to convert
    : mov dx, 80h ; Mask with first bit set = 10000000 in binary
    : mov cx, 8 ; How many bits to scan
    : mov di, OFFSET buf ; Load address of a first symbol in output buffer
    :
    : next_bit:
    : test ax, dx ; Testing the bit
    : jz store_zero ; Bit is not set
    :
    : mov bl, '1' ; Bit is set at this point
    : jmp store_ascii_bit
    :
    : store_zero:
    : mov bl, '0'
    :
    : store_ascii_bit:
    : mov [di], bl ; Store into 'buf' (DI points to it)
    : inc di ; Advancing the address for next store
    : shr dx, 1 ; Shifting the mask
    : loop next_bit
    : [/code]:
    : NOTE: Code is not tested. Try it.
    : [/color]

Sign In or Register to comment.