1 Replies - 755 Views - Last Post: 22 March 2014 - 11:39 AM

#1 MCroz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 14

what is wrong here in my decimal to binary converter?

Posted 21 March 2014 - 07:40 AM

title decimal_to_binary
.model small
.stack 100h
.data
     msg   db  'Enter a decimal number from [00-99]:$'
     msg2  db   0dh,0ah,'Its equiavlent binary is:$' 
.code
main proc
   mov   ax,@data
   mov   ds,ax

   lea   dx,msg
   mov   ah,9     ;print message
   int   21h

   mov   ah,1
   int   21h      ;read data from user

   xor bx,bx
   mov bl,al
   sub bl,30h
   xor ax,ax
   mov ax,10h
   mul bx
   mov bx,ax

   mov ah,1
   int 21h

   sub al,30h
   xor ah,ah
   add bx,ax

   xor ax,ax
   mov ax,bx
   mov cx,19h
   xor dx,dx
   div cx

   mov bx,ax
   mov cx,dx

   lea   dx,msg2  ;print message
   mov   ah,9
   int   21h

   mov   bh,bl    ;move contents of bl into bh

   shr   bh,3    ;right shift bh register three times
   add   bh,30h   ;make binary value visible as 0 or 1

   mov   ah,2     ;print binary value
   mov   dl,bh
   int   21h

   xor   bh,bh    ;clear bh register
   mov   bh,bl    

   and   bh,04h   ;clear all bits except third last bit

   shr   bh,2
   add   bh,30h

   mov   ah,2     ;print binary value of third last bit 
   mov   dl,bh
   int   21h

   xor   bh,bh
   mov   bh,bl

   and   bh,02h   ;clear all bits except second last bit
   shr   bh,1

   add   bh,30h

   mov   ah,2     ;print second last bit 
   mov   dl,bh
   int   21h

   xor   bh,bh
   mov   bh,bl     

   and   bh,01h   ;clear all bits except the last bit
   add   bh,30h

   mov   ah,2     ;print last bit in binary
   mov   dl,bh
   int   21h

   mov   ah,2
   mov   dl,20h
   int   21h

   mov   ch,cl    ;move contents of cl into ch

   shr   ch,3    ;right shift ch register three times
   add   ch,30h   ;make binary value visible as 0 or 1

   mov   ah,2     ;print binary value
   mov   dl,ch
   int   21h

   xor   ch,ch    ;clear ch register
   mov   ch,cl    

   and   ch,04h   ;clear all bits except third last bit

   shr   ch,2
   add   ch,30h

   mov   ah,2     ;print binary value of third last bit 
   mov   dl,ch
   int   21h

   xor   ch,ch
   mov   ch,cl

   and   ch,02h   ;clear all bits except second last bit
   shr   ch,1

   add   ch,30h

   mov   ah,2     ;print second last bit 
   mov   dl,ch
   int   21h

   xor   ch,ch
   mov   ch,cl     

   and   ch,01h   ;clear all bits except the last bit
   add   ch,30h

   mov   ah,2     ;print last bit in binary
   mov   dl,ch
   int   21h





   jmp   exit


exit:

   mov   ah,4ch
   int   21h

main endp
         end main





what's wrong here?
the first 4 number in binary is correct but the last 4 is wrong.
example if i input 95 the output is 0101 3000
but if i input 99 the output is 0110 0011 and it is correct.
some decimal gives wrong output can someone help me debug this im using TASM.

Is This A Good Question/Topic? 0
  • +

Replies To: what is wrong here in my decimal to binary converter?

#2 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: what is wrong here in my decimal to binary converter?

Posted 22 March 2014 - 11:39 AM

Looks like code from another post here. Copy and pasting without understanding what is happening will do you no good. It is very simple to print the binary form. Since everything to the CPU is already in binary, you just have to convert the zeros and ones into printable digits. We do this by Rotating the Nibbles to the left by 1. The number that is rotated out, gets tested for a 0 or 1 and the correct ASCII code it put into dl for display. We then test our loop counter to print a space.

Attached Image

    mov     ah,1
    int     21h         ; read data from user

    sub     al, "0"     ; convert to number
    mov     bl, 10
    mul     bl          ; 4 becomes 40, 3 > 30, 6 > 60 etc...
    mov     bl, al
    
    mov     ah, 1
    int     21h         ; read data from user
    sub     al, "0"
    add     bl, al      ; add second number to first number
    
    lea     dx, msg2
    mov     ah, 9       ; print message
    int     21h
       
    mov     cl, 7       ; loop 8 times
NextNibble:
    rcl     bl, 1       ; put MSB into carry
    jc      Zero
    mov     dl, "1"
    jmp     Show
   
Zero:
    mov     dl, "0"

Show:
    mov     ah, 2
    int     21h         ; display "0" or "1"

    cmp     cl, 4
    jne     Next        ; if cl != 4, no space printed
    
    mov     dl, " "
    mov     ah, 2
    int     21h         ; print space
Next:
    dec     cl
    jns     NextNibble  ; cl !< 0


I have added a bug to this code that will print the wrong digits. zeros will be ones and ones, zeros. Up to you to figure out how to fix it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1