1,105,391 Community Members

Converting Hexa to Binary in Assembly8086

Member Avatar
wayn3san
Newbie Poster
3 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want to write an assembly program to convert Hexadecimal to Binary in 8086 assembly language. I am using Winasm as my compiler.
However i have no idea how to write this code.
Can anyone shed some light?

thanks in advance!

Member Avatar
tesuji
Master Poster
721 posts since Apr 2008
Reputation Points: 135 [?]
Q&As Helped to Solve: 98 [?]
Skill Endorsements: 0 [?]
 
0
 

I want to write an assembly program to convert Hexadecimal to Binary in 8086 assembly language. I am using Winasm as my compiler.
However i have no idea how to write this code.
Can anyone shed some light?

thanks in advance!

Hello,

how is your hex value coded and stored, char, reg/data segment?

If you want to output the binary (0/1) of a register's content, for example reg bl, you only need to get its bits by left-rotating bl through the carry flag (rcl). But if you really want to convert a hexadecimal number into its binary representation you have to code the hex number like a char priorly.

This macro converts aByte into its binary, converts it to asccii 30h/31h and output the eight figures 0/1 on screen by using dos function 02h (sorry, not tested till now):

bioBin macro aByte                      ; binary (0/1) output of aByte to screen 
local lup                               ; local symbol
      push   ax                         ; save our regs (flag reg should also be saved)
      push   cx                         ;
      push   dx                         ;
      mov    bl, aByte                  ; move aByte into bl reg
      mov    cx, 8                      ; Loop counter, a byte has 8 bits
      mov    ah, 2h                     ; DOS function to output one char from dl
                                        ; the gist of the matter is the four-line lup
lup:  rcl    bl, 1                      ; left-rotating bl through carry CF
      mov    dl, 30h                    ; make an ASCII out of it, ascii = number + '0'
      adc    dl, 0                      ; Add with carry, that is ascii=30h+0 or 30h+1
      int    21h                        ; jump to DOS and execute 2h there
      loop   lup                        ; dec cx and repeat until cx = 0
      pop    dx                         ; revive our regs
      pop    cx                         ;
      pop    ax                         ;
endm  bioBin

I hope it will work fine (took some code fragments from my older resources and modified them into a macro, not tested till now, yet, i am hopeful it will work) Btw, aByte can be a constant, a reg, also an address like byte ptr [bx].

; How to make use of the macro?
bioBin dh             ; reg dh contains a byte to output in a binary way
;or
bioBin 15H            ; 15H to output its binary  rep. (possibly your case?) 
;or
bioBin byte ptr [bx]  ; where bx points to a byte in data segment

If you don't care macros:
1. Drop lines 1, 2 and 18, maybe the pushs and pops too
2. In line #6 replace aByte by for example 20H

-- tesu

Member Avatar
tesuji
Master Poster
721 posts since Apr 2008
Reputation Points: 135 [?]
Q&As Helped to Solve: 98 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi wayn3san

I have neither MSN nor ICQ. You can easily post your questions here.

Here on daniweb you are really sure that your problems be read and may also be solved by other more adept experts.

-- tesu

Member Avatar
wayn3san
Newbie Poster
3 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

its because bro,i dont really get what are you projecting to me..thats why i really need your help.im really weak at this assembly language!

btw,"how is your hex value coded and stored, char, reg/data segment?"
i think i want to store it at data segment. how am i gonna write this in?

and , if i input your aByte macro,will it work to convert my Hexa to Binary? as i've not tried it.im still working on the Input of the program..

and , regarding the last syntax, i dont really get what are you trying to say to me..
enlighten me please ?

THANKS A MILLION

Member Avatar
wayn3san
Newbie Poster
3 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
title convert from hexadecimal to binary
dosseg
.model small
.stack 100h
.data

msgstar		db 0dh,0ah,0dh,0ah,"*******************************************************************************",0dh,0ah,"$"
msgintro	db "                                 PROGRAM FUNCTION",0dh,0ah,
				   "                      TO CONVERT HEXADECIMAL NUMBER INTO BINARY NUMBER","$"
msginput	db  0dh,0ah,"Your HEXADECIMAL INPUT (MAXIMUM FF) :",0dh,0ah,"$"
msgafter	db 	0dh,0ah,"Your INPUT AFTER CONVERT INTO BINARY : ",0dh,0ah,"$"
msgagain	db	0dh,0ah,0dh,0ah,"DO YOU WANT TO TRY AGAIN ?",0dh,0ah,"PRESS (y OR Y) TO TRY AGAIN,PRESS OTHER KEY TO TERMINATE:",0dh,0ah,"$"
msgmax		db	0dh,0ah,0dh,0ah,"YOUR INPUT HAD REACH MAXIMUM 8 DIGITS / BIT , CALCULATION WILL START NOW",0dh,0ah,"$"
blank		db	0dh,0ah,"$"
error		db  0dh,0ah,"INVALID INPUT, ONLY 1 AND 0 IS ALLOW. PROGRAM WILL TERMINATE NOW.",0dh,0ah,"$"
print		dw	0
count		dw	0
num		dw	0

.code
main	proc
		mov ax,@data
		mov ds,ax
		
		mov ax,0
		mov bx,0
		mov cx,0
		mov dx,0
		mov print,0	; reset evrything used in program to makesure the calculation will be correct

		mov count,0	; after user request repeat
		mov num,0
		
		mov dx,offset msgstar ;display start (decoration)
		mov ah,9
		int 21h
		
		mov dx,offset msgintro ;display intro message
		mov ah,9
		int 21h
		
		mov dx,offset msgstar ;display start (decoration)
		mov ah,9
		int 21h
		
		
		mov	dx,offset msginput ;display input message
		mov ah,9
		int 21h
		
		mov cx,-1 ; assign -1 into cx to act as counter
		
	

input:	mov ah, 00h
		int 16h
		cmp ah, 1ch
		je exit

number: cmp al, '0'
		jb input
		cmp al, '9'
		ja uppercase
		sub al, 30h
		call process
		jmp input

uppercase: 	cmp al, 'A'
	   		 jb input
		    cmp al, 'F'
			ja lowercase
			sub al, 37h
			call process
			jmp input

lowercase: 	cmp al, 'a'
			jb input
			cmp al, 'f'
			ja input
			sub al, 57h
			call process
			jmp input

			loop input

process: 	mov ch, 4
			mov cl, 3
			mov bl, al

convert:	mov al, bl
			ror al, cl
			and al, 01
			add al, 30h

			mov ah, 02h
			mov dl, al
			int 21h

			dec cl
			dec ch
			jnz convert

			mov dl, 20h
			int 21h
ret

exit:
int 20h

	
	
main EndP
end main

this is my code but im not able to project the Input that i insert.
whenever i input the number,it automatic will output the hexa into Binary

how can i make it project its Input?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article