News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Binary to displayable text, any base

Started by ahsat, March 31, 2024, 04:37:00 AM

Previous topic - Next topic

ahsat

I am new here and I don't know the current state of things in the world. I have an algorithm I may want to share, if it is not already been invented elsewhere. I came up with the algorithm in the late 70s.

What is the current state of the art in converting binary to a null terminated string of any base, ie binary to hex, to decimal, or binary?

For example, given a number in rax, the base in rbx, the address of the target string in edi, and a table of digits, ie "0123456789abcdef". Where can I find the best example of assembler code that would convert the value in rax into displayable text.

I am not looking for code that will simply do the function, I already know how to do that. I want to see if I know a better way of doing it. Remember this is converting binary to any base, lets say base 2 to base 60. Base 60 is what the Sumerian's used.

Vortex

Hi ahset,

It depends on your expectations, well-optimized routines will require extra work. As a beginning, you can start by studying the functions from masm32.lib :

\masm32\help\masmlib.chm :

QuoteConversions

atodw Convert ascii number to DWORD
htodw Convert HEX string to DWORD
dwtoa Convert DWORD to ascii string
.
.
.

jj2007

Quote from: ahsat on March 31, 2024, 04:37:00 AMWhat is the current state of the art in converting binary to a null terminated string of any base, ie binary to hex, to decimal, or binary?

In addition to what Vortex wrote, there are these number to string macros, part of the Masm32 SDK:
ubyte$() Unsigned BYTE input
sbyte$() Signed BYTE input
xbyte$() Hex notation output BYTE
uword$() Unsigned WORD input
sword$() Signed WORD input
xword$() Hex notation output WORD
udword$() Unsigned DWORD input
sdword$() Signed DWORD input
xdword$() Hex notation output DWORD
uqword$() Unsigned QWORD input
sqword$() Signed QWORD input
xqword$() Hex notation output QWORD
real4$() 32 bit FLOAT input
real8$() 64 bit FLOAT input
real10$() 80 bit FLOAT input

MasmBasic offers
Str$()     input automatically detected (reg32, xmm*, dword, word, byte, ST(0))
Hex$()   input automatically detected (...)
Bin$()    input reg32, dword variable, immediate

ahsat

Quote from: Vortex on March 31, 2024, 05:53:00 AMatodw Convert ascii number to DWORD
htodw Convert HEX string to DWORD
dwtoa Convert DWORD to ascii string

I am sorry at being so new here, but where can I locate those?

Vortex

Hi ahsat,

QuoteI am sorry at being so new here, but where can I locate those?

The location of the source code :

\masm32\m32lib

NoCforMe

Remember, those (most of them) are all 32-bit code.

I'm sure your binary-to-ASCII routine could easily be rewritten in X86/X64 assembly language. Care to post it here?
Assembly language programming should be fun. That's why I do it.

ahsat

Quote from: Vortex on March 31, 2024, 07:47:37 AMThe location of the source code :

I have \masm64\m64lib and it seems not to be there. I will get the 32 bit version.

ahsat

Quote from: NoCforMe on March 31, 2024, 08:21:39 AMI'm sure your binary-to-ASCII routine could easily be rewritten in X86/X64 assembly language. Care to post it here?

If my code is a good as I think it is, I will. But I have been out of the coding world for so long, I don't want to look foolish. I want to see the best that is out there first.

jj2007

Quote from: ahsat on March 31, 2024, 08:30:04 AMI want to see the best that is out there first

Go to forum search and try e.g. fast hex

Btw instead of chasing the fastest bintostring algo, you might simply use them for some more ambitious project of yours.

ahsat

Quote from: jj2007 on March 31, 2024, 08:43:46 AMsome more ambitious project of yours

I'm too old for anything ambitious.

jj2007

Quote from: ahsat on March 31, 2024, 09:20:49 AMI'm too old for anything ambitious.

Beating the algos in the Laboratory is extremely ambitious :biggrin:

NoCforMe

Quote from: ahsat on March 31, 2024, 08:30:04 AM
Quote from: NoCforMe on March 31, 2024, 08:21:39 AMI'm sure your binary-to-ASCII routine could easily be rewritten in X86/X64 assembly language. Care to post it here?

If my code is a good as I think it is, I will. But I have been out of the coding world for so long, I don't want to look foolish. I want to see the best that is out there first.

I, for one, would be curious to see what you have. No need to worry about looking foolish: we're not judgemental like that here. (If there are obvious flaws in your code someone might point them out.) Always interesting to see someone's take on how to perform some function, especially if it was conceived some number of years ago.

And so far as performance goes, I've been driving the same nail here for some time: it really doesn't make any difference how fast such routines are, at least in most applications. Let's say you're using such a routine to convert display user output in a Windows console program: the time it takes to actually display the text completely swamps whatever time it takes to do the conversion.

Now if your code is super-adaptable, can produce output in any radix (base), then that would be something ...
Assembly language programming should be fun. That's why I do it.

ahsat

Quote from: NoCforMe on March 31, 2024, 10:10:38 AMoutput in any radix (base), then that would be something

The code can produce output in any base, I currently have a test program that supports base 2 to base 60. The only thing that limits the base is the characters available to represent the digits. I am currently using

'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkylmnopqrstuvw'

as the digits. Does that meet your definition of a radix (base)?

I also understand you to be saying that you know of nothing like this existing now, is that correct.

ahsat

Quote from: jj2007 on March 31, 2024, 09:45:05 AMBeating the algos in the Laboratory is extremely ambitious

I think I have been doing that all my life.  :biggrin:

NoCforMe

Yes, radix == base.

Base 60: how would that even work? Wouldn't you need 60 unique "digits" for representation? Never dealt with any higher base than 16 (hex).

I don't know of any routine that does such universal conversion, but that certainly doesn't mean it doesn't exist.

I'd still like to see your code if possible.
Assembly language programming should be fun. That's why I do it.