Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > 64tass: hex to string into source, how?
2012-05-18 06:54
Flavioweb

Registered: Nov 2011
Posts: 463
64tass: hex to string into source, how?

I i use
number = $1000
.text ^number

result is:
$34, $30, $39, $36

How to do the same but with result
$31, $30, $30, $30?
2012-05-18 07:12
Conrad

Registered: Nov 2006
Posts: 849
I coded this macro a while back (from PoP source), which convert hex to a PETSCII string:

Macro:
HEX2STR .macro
	.if \2 = 4
		.if (\1 >> 12)<10
			.byte (\1 >> 12) + $30
		.else
			.byte (\1 >> 12) - 10 + $41	
		.endif
	.fi
	.if \2 >= 3
		.if ((\1 >> 8) & $f)<10
			.byte ((\1 >> 8) & $f) + $30
		.else
			.byte ((\1 >> 8) & $f) - 10 + $41	
		.endif
	.fi
	.if \2 >= 2
		.if ((\1 >> 4) & $f)<10
			.byte ((\1 >> 4) & $f) + $30
		.else
			.byte ((\1 >> 4) & $f) - 10 + $41	
		.endif
	.fi
	.if \2 >= 1		
		.if (\1 & $f)<10
			.byte (\1 & $f) + $30
		.else
			.byte (\1 & $f) - 10 + $41	
		.endif
	.fi
.endm



Then use:

#HEX2STR $1000,4


Where 2nd parameter is the number of nybbles to print, starting from most significant first.

Needs optimising of course. :)
2012-05-18 07:23
Conrad

Registered: Nov 2006
Posts: 849
Another version:

HEX2STR .macro
	.for i=(\2)-1,i>=0,i=i-1
		.if (\1 >> (i*4))<10
			.byte (\1 >> (i*4)) + $30
		.else
			.byte (\1 >> (i*4)) - 10 + $41	
		.endif
	.next
.endm

#HEX2STR $1000,4


This one should take values bigger than $ffff.
Haven't tried it out yet though.
2012-05-18 07:24
Flavioweb

Registered: Nov 2011
Posts: 463
Thanks a lot !!!

But seems there is a problem:
Assembling file:   hex2str.s
hex2str.s:6: (hex2str.s:2) (hex2str.s:11) Constant too large ".byte ($1000 >> (i*4)) - 10 + $41"
hex2str.s:6: (hex2str.s:2) (hex2str.s:11) Constant too large ".byte ($1000 >> (i*4)) - 10 + $41"

...
2012-05-18 07:37
Conrad

Registered: Nov 2006
Posts: 849
Ok. Just use the first example for now. I can't test the second one because I'm on my PC at work which has no C64 tools. I'll be happy if someone else can adjust it.

Also, if you want the "A-F" characters to use bytes $01-$06 from the charset, replace "$41" with "$01".
2012-05-18 07:59
Flavioweb

Registered: Nov 2011
Posts: 463
This version works fine:
HEX2STR .macro
    .if \2 = 4
        .if (\1 >> 12)<10
            .byte (\1 >> 12) + $30
        .else
            .byte (\1 >> 12) - 10 + $01 
        .endif
    .fi
    .if \2 >= 3
        .if ((\1 >> 8) & $f)<10
            .byte ((\1 >> 8) & $f) + $30
        .else
            .byte ((\1 >> 8) & $f) - 10 + $01   
        .endif
    .fi
    .if \2 >= 2
        .if ((\1 >> 4) & $f)<10
            .byte ((\1 >> 4) & $f) + $30
        .else
            .byte ((\1 >> 4) & $f) - 10 + $01   
        .endif
    .fi
    .if \2 >= 1     
        .if (\1 & $f)<10
            .byte (\1 & $f) + $30
        .else
            .byte (\1 & $f) - 10 + $01  
        .endif
    .fi
.endm
2012-05-18 10:51
Conrad

Registered: Nov 2006
Posts: 849
Revision II of the other version (hope it works):

HEX2STR .macro
	.for i=(\2)-1,i>=0,i=i-1
		.if ((\1 >> (i*4)) & $f) < 10
			.byte ((\1 >> (i*4)) & $f) + $30
		.else
			.byte ((\1 >> (i*4)) & $f) - 9
		.endif
	.next
.endm

#HEX2STR $1234abcd,8


The error before was due to forgetting to mask the bit-shift result with $f.
2012-05-18 18:50
Flavioweb

Registered: Nov 2011
Posts: 463
Quoting Conrad
Revision II of the other version (hope it works):

The error before was due to forgetting to mask the bit-shift result with $f.

Just for precision, this version work with 8 digits or minus.

$1234abcd, works, $12345abcd still return "Constant too large" error.

Nothing really important... just saying...
2012-05-18 19:30
iAN CooG

Registered: May 2002
Posts: 3194
$12345abcd is not a 32 bit integer, of course it's not valid...
2012-05-18 22:29
TWW

Registered: Jul 2009
Posts: 545
Quote: I i use
number = $1000
.text ^number

result is:
$34, $30, $39, $36

How to do the same but with result
$31, $30, $30, $30?


number = $03e8
.text ^number


?
2012-05-19 04:29
Flavioweb

Registered: Nov 2011
Posts: 463
Quoting TWW
number = $03e8
.text ^number


?


Ok, but i can't know the -result- before... the "$1000" is just an example...

But i like the concept of always finding different ways to solve problems =)
2012-05-19 07:05
Conrad

Registered: Nov 2006
Posts: 849
^number only prints the result out in decimal, according to the manual.

As for larger numbers than 32-bit, 64Tass is only available on 32-bit OSs. A 64-bit compiled version would probably work. For now, you'll just have to use two 32-bit variables for bigger hex strings.
2012-05-19 07:41
Flavioweb

Registered: Nov 2011
Posts: 463
Quoting Conrad
^number only prints the result out in decimal, according to the manual.

As for larger numbers than 32-bit, 64Tass is only available on 32-bit OSs. A 64-bit compiled version would probably work. For now, you'll just have to use two 32-bit variables for bigger hex strings.

I'm using a -self compiled- version of 64Tass on OpenSuse 12.1 64bit, but seems that only 32bit numbers can be used...
In makefile
#CFLAGS = -O2 -march=i486 -mcpu=i486 -pipe
#CFLAGS = -Wall -O3 -march=i686 -pipe -fomit-frame-pointer -fno-exceptions
#CFLAGS = -Wall

...
2013-08-27 01:16
soci

Registered: Sep 2003
Posts: 480
I know that digging up old threads is not nice, but anyway just to close it up properly.

Use string formatting with hexadecimal output in some recent version:

.text "%04x" % (mylabel,)

And as of 1.51 I would not worry about running out of bits in integers on any platform.
2013-08-27 07:11
Flavioweb

Registered: Nov 2011
Posts: 463
Quote: I know that digging up old threads is not nice, but anyway just to close it up properly.

Use string formatting with hexadecimal output in some recent version:

.text "%04x" % (mylabel,)

And as of 1.51 I would not worry about running out of bits in integers on any platform.


Just in these days i had this problem again using 1.5 (not 1.51!) and i solved using

.TEXT REPR (VALUE)

with
value=1234

.text "%04x" % (value,)
the compiler give a "type mismatch" error, that isn't if i use
.text "%04x" % (1234,)
instead.
Maybe it's fixed in 1.51...
=)
2013-08-27 13:50
soci

Registered: Sep 2003
Posts: 480
Yes it didn't resolve identifiers in the formatting argument tuple in some versions of 1.50. This was so since tuples could contain identifiers and not just values.

When I noticed it the modifications for 1.51 were ongoing but it was unusable yet. So I worked it around it in my sources with dummy calculations like '(label+0, text.."")'.

Fixing it in 1.50 would have been a sort of a hack, but it's fixed now in 1.51 in a clean way now.
2014-03-06 23:22
soci

Registered: Sep 2003
Posts: 480
Quote: I know that digging up old threads is not nice, but anyway just to close it up properly.

Use string formatting with hexadecimal output in some recent version:

.text "%04x" % (mylabel,)

And as of 1.51 I would not worry about running out of bits in integers on any platform.


I don't really like to dig up old threads, but anyway it's necessary for historical reasons.

Since then I realized that this percent formatting syntax was not really good idea as it's too easy to confuse with a normal modulo, especially when variables are used.

So it's gone now in r668. Instead formatting is done using a function with variable number of arguments:

.text format("%04x", mylabel)

If for whatever reason the arguments were contained in variable then the conversion looks like this:

.text format("%d: %s", *somelist)

I hope I don't have to update this thread again anytime soon. But one can never know, it's in development after all...
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Guests online: 93
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Swappers
1 Derbyshire Ram  (10)
2 Jerry  (9.8)
3 Violator  (9.7)
4 Acidchild  (9.7)
5 Cash  (9.6)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.043 sec.