Thursday, February 10, 2011

Binary to Decimal



%OUT-----------------------------------------------------------------------------------------
%OUT- Prints a 8-bit binary number in its equivalent decimal form                               
%out- input: 8-bit binary string accepted from the user                                                  
%out- output: 3 digit decimal equivalent of the binary string is printed 
%out – author: Ayon and Ariyam                                            
%OUT-----------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack

.data
count equ 08h                                                                                ;for 8-bit binary string
ip db "Enter a 8-bit binary number: ","$"                                             
;a request to prompt the user for entering the binary no.
op db "  Decimal form:  ","$"                                                        ;output message
err db " Invalid Entry!","$"                                                             ;error message
heading db "***** BINARY to DECIMAL *****","$"                   ;a heading

.code
           
main proc
            clrscrn                                                            ;clearing the screen
            mov dx,0105h
            cursor                                                             ;setting up the cursor
            showmsg heading                                     ;displaying heading
           
            mov dx,0303h                                             ;pointing to next line
            cursor                                                             ;setting up the cursor
            showmsg ip                                                  ;prompt the user to enter the binary string
                       
            xor bh,bh                                                      ;clearing register Bh
            mov cx,count                                             ;loading cx with length of the binary string

           
continue:      rol bh,01h                                        
;rotate BH left, 0 will come in the LSB of BH, as intially BH was cleared

                                    mov ah,01h
                                    int 21h
                                    sub al,30h                             ;getting the binary digit (bit 0 or 1) in AL
                                    cmp al,01h                           ;when bit is greater than 0 or 1
                                    ja WRONG
                                    add bh,al                             ;add the input bit (0 or 1) to BH
                                    loop continue                     ;iterate till 8 bits are accepted from the user
           
            ; Hexadecimal no. is stored in BH, LSD of its decimal equivalent is in BH
            ; Remaining part is stored in CH in hexadecimal form

            xor ch,ch                                                                  ;clearing reg. CH

            do1:                cmp bh,0ah
                                    jb complete1
                                    sub bh,0ah
                                    inc ch
                                    jmp do1
                       
            complete1:  mov cl,bh     ;LSD of decimal equivalent is now stored in CL
                                    mov bh,ch   ;dealing with remaining hexadecimal part same way
                                    xor ch,ch      ;clearing reg. CH

                                    do2:    cmp bh,0ah            ;middle digit is stored in BH & MSD in CH
                                                jb complete2
                                                sub bh,0ah
                                                inc ch
                                                jmp do2
                       
            complete2:  rol ch,1
                                    rol ch,1
                                    rol ch,1
                                    rol ch,1
                                    or bh,ch
           
            showmsg op            ;display output message, content of BH not hampered
            output                      ;calling output macro to display first 2 digits of the decimal number
           
            mov dl,cl                               ;displaying last decimal digit
            add dl,30h                           ;converting to ASCII value
            mov ah,02h
            int 21h
           
            jmp FINISH


           
WRONG:       showmsg err                        ;displaying error message           
FINISH:                       exit
           
main endp
end main
           

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.