Thursday, February 10, 2011

Binary to Hexadecimal



%OUT-----------------------------------------------------------------------------------------
%OUT- Prints a 8-bit binary number in its equivalent hexadecimal form                     
%out- input: 8-bit binary string accepted from the user                                                  
%out- output: 1 byte Hexadecimal 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 "  Hexadecimal form:  ","$"                                                           ;output message
err db " Invalid Entry!","$"                                                             ;error message
heading db "***** BINARY to HEXADECIMAL *****","$"                     ;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
           
            showmsg op                                    ;display output message, content of BH not hampered
            output                                              ;calling output macro
            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.