Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

32 bit hex to ASCII conversion code in Assembly

Status
Not open for further replies.

garg29

Advanced Member level 1
Joined
Nov 17, 2004
Messages
443
Helped
25
Reputation
50
Reaction score
10
Trophy points
1,298
Activity points
3,593
hi friends
i am designing a project in which i need 32 bit hex to ascii conversion code in assembly.i m having 16 bit hex to ascii conversion code.i need this code for displaying 8 digit values on lcd. can anybody please provide me 32 bit hex to ascii conversion code in assembly.

thanks
with best regards
amit
 

00001111 8 bits = ascii

Hi garg29,

Believe it or not, its pretty simple, but it depends on the hardware you are using (8 bit, 16, 32? micro). Please specify what micro you are using.

Because 'hex' values are 4-bit, if you are using an 8-bit microcontroller, you can use the 'swap nibbles' function and a look-up table. Pseudo-code would be:

Get an bit number into W reg (accumulator)
And with '00001111' - gets rid of upper nibble
Call your look-up table,
Store in 'Lowhex'
Get your 8-bit number again,
Swap nibbles.
And with '00001111' - gets rid of upper nibble
Call your look-up table,
Store in 'Highhex'

So, you convert an 8-bit binary number, to 2 Hex values (in ascii). So for say, 11010010 -> 'D2' in ascii.

I'm not entirely sure if this is what you want, I need more information.


Goodluck,

BuriedCode.
 

32 bit to decimal 8052

area bss(RAM)
longValue: blk 4

area text(ROM,REL)
HexStr:
DS "0123456789ABCDEF"

PutLong:
mov X, 3
repeat:
mov A, [X + longValue]
call PutHexByte
dec X
jnc repeat
ret

PutHexByte:
push A
asr A
asr A
asr A
asr A
and A,0Fh
mov X, A
mov A, [X + HexStr]
call PutChar
pop A
and A,0Fh
mov X, A
mov A, [X + HexStr]
call PutChar
ret

PutChar:
mov ??, A
:
:
ret



Gomez
 

32 bit hexadecimal ascii code

Thanks for replying, especially buriedcode. I'm using 8-bit uC 89c51. Actually what I want is a 32 bit hex to decimal and decimal to ascii convertor assembly code. can you suggest me something more. Thanks

with regards,
amit

Added after 5 hours 56 minutes:

hi
m having this hex value 1746E6 in register r1 r2 and r3 as

R1= 17h
R2= 46h
R3= E6h

the decimal value for this is 1525478.
how to display this on LCD.
thanks
with best regards
amit
 

convert hex 32 bit to ascii

Hello again!

32 bit hex -> decimal -> ASCII (for LCD) Right? Ok.

I'm less familiar with the 89C51 family than I am with the PIC series, so I can't be too specific regarding instructions.

It gets confusing talking about a hex/decimal/binary number, since they can all be the same number, it just depends of how you write them down.
eg: decimal 137 = 89h = '10001001' binary. All the same value. This always confuses the hell out of me, but I think I know what you're after. You need to convert a binary number (in 4 registers for 32 'bits' in binary) to BCD (binary coded decimal). You can then convert this to ASCII using a simple look-up table, since, in decimal, a 'digit' can only be 0-9. Thats only 10 entries.

In your example:

Im having this hex value 1746E6 in register r1 r2 and r3 as

Sorry to confuse you, but thats a 6-digit HEX value, which is a 24-bit binary number. I'll just assume its binary (confusing myself now :( ).

To work out how many decimal digits you need to display, 2^32 = 4294967296
So thats a maximum of 10 (!!) digits to display on your LCD. Converting binary to decimal isn't that hard, but I imagine it gets harder (not just longer) the larger the binary values you are dealing with. In fact, I struggle with 8-bit binary to BCD, let alone 16, or 32 bit. But I always write my code from scratch ;)

I know you're a 89C51 man, bit PIClist has some great code 'snippets' with good annotation, so you might be able to use them, and change it for your own micro.:



alos see:

**broken link removed**
**broken link removed**


If you are not well-versed in writing programs and doing 'radix' mathematics, I suggest you serach google for 'BCD, 'binary coded decimal' and 'packed BCD'.

As far as any code from me, I have little experience with BCD, but I would suggest starting at the LSbyte (least significant) and use subroutines for 'carry' situations where you have, say, 10's = 9 and you add d'10', therefore, 10's will become '0' and 100's will be incremnted by '1'. You should do this for all digits. It may take a while (20ms?).

Good Luck,

BuriedCode.
 

hex to ascii assembly

Hi I ma designing a digital volt meter using the 10 bit a/d converter of the PIC16F877. I need the code to convert the 10bit value stored in ADRESH:ADRESLregister pair into ASCII to display this on an LCD. COULD you please send me a code in assembly language[/quote]
 

asm hextoascii

Thanks Buried code for providing the code from h**p://www.8052.com/users/RRAGHU/CODELIB.ASM
but the code isn't working. The author hasn't described the values to be provided to some registers used. I think lot of data is missing. Someone please help me....


Code:
;-------------------------------------------------------------------------------------------------
	
	; R0 should point to the LSB byte of the 32bit number to be converted.BCD 
	; result is stored starting from BCD0(MSB) to BCD4(LSB). 

	H32BCD:    	LCALL dCNVa             ; Clear/load conversion buffers
			LCALL dCNVb             ; Convert first byte
			LCALL dCNVb
			LCALL dCNVb
			LCALL dCNVb             ; Convert fourth byte
			MOV R1,#dACCU+dBYTS     ; Pointer to converted bytes+1
			MOV R6,#dBYTS           ; Counter for 5 BCD coded bytes
			LCALL BCD_STOR
			RET

	BCD_STOR:   	NOP                     	; Store 5 BCD bytes in decreasing order @R1   
			MOV R0,#BCD0          	; Point to the BCD result area
	STORL:      	DEC   R1              
			MOV   A,@R1
			MOV   @R0,A             	; Store BCD value
			INC R0                  	; Increment result pointer
			DJNZ  R6,STORL
			RET

	dCNVa:      	MOV   A,#0
			MOV   dACCU+0,A      ; Init DEC conversion dACCU:=0 ; dREG1:=1
			MOV   dACCU+1,A
			MOV   dACCU+2,A
			MOV   dACCU+3,A
			MOV   dACCU+4,A
			MOV   dREG1+0,#1
			MOV   dREG1+1,A
			MOV   dREG1+2,A
			MOV   dREG1+3,A
			MOV   dREG1+4,A
			RET

	dCNVb:      	MOV   A,@R0             ; Convert Byte at R0 , & inc R0
			INC   R0
	DCNVb1:     	MOV   R2,A              	; Convert Byte ACC
			MOV   R6,#8             	; Convert R2 using dACCU and dREG1
	dCNV1:      	MOV   A,R2              	; Get bit from R2
			RRC   A
			MOV   R2,A
			JNC   dCNV2
			LCALL ADD1              	; If CY add dREG1 to dACCU using BCD
	dCNV2:      	LCALL DOUBL1            ; dREG1:=2*dREG1
			DJNZ  R6,dCNV1          ; Loop until all 8 bits done
			RET

	DOUBL1:     	MOV   A,dREG1+0         ; dREG1:=2*dREG1 , BCD coded 5 bytes
			ADD   A,dREG1+0
			DA    A
			MOV   dREG1+0,A
			MOV   A,dREG1+1
			ADDC  A,dREG1+1
			DA    A
			MOV   dREG1+1,A
			MOV   A,dREG1+2
			ADDC  A,dREG1+2
			DA    A
			MOV   dREG1+2,A
			MOV   A,dREG1+3
			ADDC  A,dREG1+3
			DA    A
			MOV   dREG1+3,A
			MOV   A,dREG1+4
			ADDC  A,dREG1+4
			DA    A
			MOV   dREG1+4,A
			RET

	ADD1:  	MOV   A,dACCU+0         ; dACCU:=dACCU+dREG1 , bcd coded 5 bytes
			ADD   A,dREG1+0
			DA    A
			MOV   dACCU+0,A
			MOV   A,dACCU+1
			ADDC  A,dREG1+1
			DA    A
			MOV   dACCU+1,A
			MOV   A,dACCU+2
			ADDC  A,dREG1+2
			DA    A
			MOV   dACCU+2,A
			MOV   A,dACCU+3
			ADDC  A,dREG1+3
			DA    A
			MOV   dACCU+3,A
			MOV   A,dACCU+4
			ADDC  A,dREG1+4
			DA    A
			MOV   dACCU+4,A
			RET

	;-----------------------------------------------------------------------------------------------------
 

32 bit hex to ascii

Visit **broken link removed**

Regards
Sarang
 

32-bit hex

hey
it is very simple donnn worry
i think u have got routines to display something on lcd like lcdputc() or some functions ok just what ever value u wann to get displayed on lcd add 48 to it ie 0x30h suppose if u wann 8 to b displayed on lcd write 56 in the function where u r displaying on the lcd
i have a doc for lcd commands if u wann i can upload that also for u not right now but tomm asi donn have it right now
 

hex to ascii+32bit

Hello once again,

There seems to be a lot of confusion in this thread, not to mention 'repitition'. I hate to nag, but would people please read the thread before they answer a question.
Otherwise we could have 8 people all providing the same link.

garg29,

Thanks Buried code for providing the code from...but the code isn't working. ... I think lot of data is missing

I didn't say it was going to be that simple :) A lot of code for micro's on the web can be compiled and go straight into your chip, but some code is just the 'main function' of the program. That code in your post appears to be a load of nested subroutines, all called from the main routine 'H32BCD'. Obviously it needs register setup, for variables, and chip setup (pins, peripherals, clock etc..)

Now, thats the price we pay for using pre-written code, and not writing our own from scratch. But its a lot quicker, easier and a fantastic way to learn!! You still haven't mentioned the sort of 'level' you are at with microcontrollers? I mean, is this your first big program? Or is this the first time you've used the 89c51? I don't want to miss out information I assume you know.

Anyway, as I said, I've only written very simple programs for thr 8051 series, but its fairly similar. So, here's what I 'think' the code needs to work. YOu must initialise the registers. Ie: give them an address. And it uses a lot of 'indirect addressing' (if you don't know what that means, search google).

R0 should point to the LSB byte of the 32bit number to be converted.BCD
So, R0 should contain the address of the register for the LSbyte of your 32-bit number. Also, it says that the converted values (in BCD) are stored in
'BCD0(MSB) to BCD4(LSB).' Thats 5 registers. Standard BCD has one register per decimal digit, you need 10 digits, but there are 5 registers. That means (I could be wrong!) thats its 'packed BCD'. So, each byte actually contains two decimal digits.
(because a decimal digit is 0-9, and 4 bits can be 0-15, we must use at least 4 bits per decimal digit. Hence, 2 digits in a byte). I hope I'm not confusing you.

What I'm saying is, even if you get the code working, by initiaizing the registers, clock etc.. all it will do is, take a 32-bit number which you must provide in your code, and fill in 5 registers, with 2 decimal digits (5*2 = 10). You've still got to get these numbers, unpack them (so you have 10 registers, one for each digit) and THEN convert them to ASCII (tiny look-up table) and send them to your LCD.

It sounds like a lot, but broken down into sub programs, its just a lot of little easy progs :)

One more question, where are you getting these '32-bit' numbers? From a UART, or from a keyboard, or is it purely an excercise in writing assembly language?

Regards, and sorry I could be more help.

BuriedCode.

*update*
janakiram.sistla has a good point! To convert a decimal number to ASCII, just add 0x30h!. Well done janakiram.sistla, I had forgotten about that little trick :D
 

man hextoascii

I don't know much of assembly. have a look at this sample C code logic and convert to assembly.
Code:
void lcdoutulong(unsigned long lnum)
{
   unsigned char digit;

   while(lnum != 0)
   {
      digit = lum % 10;
      /* convert to ASCII '0' = 0x30 */
      digit = digit + '0'; 
      lcdoutdigit(digit);
      lnum = lnum / 10;
   }
}

Cheers
idlebain
 

hex to ascii 32

garg29 said:
hi friends
i am designing a project in which i need 32 bit hex to ascii conversion code in assembly.i m having 16 bit hex to ascii conversion code.i need this code for displaying 8 digit values on lcd. can anybody please provide me 32 bit hex to ascii conversion code in assembly.

thanks
with best regards
amit

Hi,
This is an old routine I made a long time ago. It is made for a 24bit binary value, but 32 is just som more values in the table. You'll also need to increase the horisontal size of the table.

It is made for 68HC16 but should be compatible to most 8 bit micros.

Code:
*-----------------------------------------------------------
*
*	bin6bcd - routine compute from binary value to bcd
*
*	Entry:  reg_e:d - binary value (24 bit)
*
*	Exit:	reg_e:d - 8 bcd digits (0000:0000-1677:7215)
*
*-----------------------------------------------------------

BIN6BCD:
	PSHM	K,X,Z
	AIS	#-10
	TSZ				; scratchpad
	STE	0,Z
	STD	2,Z
	CLRW	4,Z
	CLRW	6,Z
	CLRW	8,Z
	LDAA	#24
	STAA	8,Z			; counter

BIN46BCD:
	LDAB	#.HIGH4.BINXBCDTAB
	TBXK
	LDX	#.LOW16.BINXBCDTAB

BIN46BCD1:
	RORW	0,Z
	RORW	2,Z
	BCC	BIN46BCD2		; not set!
	LDAA	3,X
	ADDA	7,Z
	DAA
	STAA	7,Z
	LDAA	2,X
	ADCA	6,Z
	DAA
	STAA	6,Z
	LDAA	1,X
	ADCA	5,Z
	DAA
	STAA	5,Z
	LDAA	0,X
	ADCA	4,Z
	DAA
	STAA	4,Z			; add in bcd value for bit

BIN46BCD2:
	AIX	#4
	DEC	8,Z
	BNE	BIN46BCD1
	LDE	4,Z
	LDD	6,Z			; result in e:d
	AIS	#10
	PULM	K,X,Z
	RTS

BINXBCDTAB:
	DB	$00,$00,$00,$01		; bit  0
	DB	$00,$00,$00,$02		; bit  1
	DB	$00,$00,$00,$04		; bit  2
	DB	$00,$00,$00,$08		; bit  3
	DB	$00,$00,$00,$16		; bit  4
	DB	$00,$00,$00,$32		; bit  5
	DB	$00,$00,$00,$64		; bit  6
	DB	$00,$00,$01,$28		; bit  7
	DB	$00,$00,$02,$56		; bit  8
	DB	$00,$00,$05,$12		; bit  9
	DB	$00,$00,$10,$24		; bit 10
	DB	$00,$00,$20,$48		; bit 11
	DB	$00,$00,$40,$96		; bit 12
	DB	$00,$00,$81,$92		; bit 13
	DB	$00,$01,$63,$84		; bit 14
	DB	$00,$03,$27,$68		; bit 15
	DB	$00,$06,$55,$36		; bit 16
	DB	$00,$13,$10,$72		; bit 17
	DB	$00,$26,$21,$44		; bit 18
	DB	$00,$52,$42,$88		; bit 19
	DB	$01,$04,$85,$76		; bit 20
	DB	$02,$09,$71,$52		; bit 21
	DB	$04,$19,$43,$04		; bit 22
	DB	$08,$38,$86,$08		; bit 23

Hope you can use some of it.

TOK ;)
 

Re: ascii to 32 bit hex

garg29 said:
hi friends
i am designing a project in which i need 32 bit hex to ascii conversion code in assembly.i m having 16 bit hex to ascii conversion code.i need this code for displaying 8 digit values on lcd. can anybody please provide me 32 bit hex to ascii conversion code in assembly.

thanks
with best regards
amit


hi amit can u help me out with 16 bit hex to ascii assembly code i need it thx
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top