News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Binary to displayable text, any base

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

Previous topic - Next topic

ahsat

Quote from: NoCforMe on March 31, 2024, 11:18:43 AMWouldn't you need 60 unique "digits" for representation?

I gave you the digits I am using. Again they are:
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkylmnopqrstuvw'

ahsat

Quote from: NoCforMe on March 31, 2024, 11:18:43 AMWouldn't you need 60 unique "digits" for representation?

I gave you the digits I am using. Again they are:
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkylmnopqrstuvw'

jj2007

Quote from: NoCforMe on March 31, 2024, 11:18:43 AMWouldn't you need 60 unique "digits" for representation?

As an alternative to the 0...z scheme (10+2*26=62 digits), you could use A0...A9, B0...B9, ... F0...F9: 60 "digits". The Unicode universe is also quite big, but a base-60 number composed of lots of emojis would be difficult to read.

ahsat

I posted the code, now it seems to have disappeared.

ahsat

This algorithm is not the fastest power of 2 bases like binary, octal and hex. But from looking around a bit, I think AnyToAny and ctAnyToAny are unique. Few programmers ever think about recursion.

I would like to know if any of you will use the routines. The routines can be converted to macros.

The program was linked using:
polink.exe /SUBSYSTEM:CONSOLE /LARGEADDRESSAWARE:NO /ENTRY:main /OUT:AnyBase.exe AnyBase.obj

TITLE AnyBase

OPTION PROC:PRIVATE                ;Don't automatically make procs public

include \masm64\include64\masm64rt.inc

.data

result    db      65 dup (?)        ;max should be 64, plus a null     
newLine   db      0dh, 0ah, 0
numb      qword   ?                 ;holds the number being converted

.CODE

;Got to keep the Sumerian happy, base 60 is the max here.
digits    db      '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkylmnopqrstuvw'
maxBase   equ     $-digits          ;Max base number, should be 60

AnyToAny proc
;----------------------------------------------------------------------------
;          Original by Ray Gwinn
; Any number to any base.
; Parameters:
;   eax    The number to convert
;   ebx    The desired base
;   edi    Where to place the converted string
;----------------------------------------------------------------------------
;
    push  rdx                       ;save rdx, or a digit
    xor   rdx, rdx                  ;zero rdx
    div   rbx                       ;generate next digit in rdx
    test  rax, rax                  ;test if done
    jz    @f                        ;br if done

    invoke AnyToAny                 ;generate next digit
@@: mov   al, digits[rdx]           ;get the ascii value
    stosb                           ;save it
    pop   rdx                       ;restore rdx, or get next digit
    ret
AnyToAny endp

ctAnyToAny proc
;----------------------------------------------------------------------------
;          Original by Ray Gwinn
; Any number to any base with a specified digit length, will pad leading zeros.
; Parameters:
;   eax    The number to convert
;   ebx    The desired base
;   ecx    The desired digit count
;   edi    Where to place the converted string
;----------------------------------------------------------------------------
;
    push  rdx                       ;save rdx, or a digit
    xor   rdx, rdx                  ;zero rdx
    div   rbx                       ;generate next digit in rdx
    dec   ecx                       ;decrement digit count
    jecxz @f                        ;br if done

    invoke ctAnyToAny               ;generate next digit
@@: mov   al, digits[rdx]           ;get the ascii value
    stosb                           ;save it
    pop   rdx                       ;restore rdx, or get next digit
    ret
ctAnyToAny endp

main proc  Public
;----------------------------------------------------------------------------
; Program to demo any number to any base, bases 2 to 60.
;----------------------------------------------------------------------------
;
    mov   numb, 123456789           ;convert this number, over and over
    jmp   @f

m00 db    13, 10, 'This is the number we will be converting', 13, 10, 0
@@: mov   rax, numb                 ;the number to rax
    mov   ebx, 10                   ;the base to rbx
    mov   edi, offset result        ;the destination addr to edi
    invoke AnyToAny
    mov   byte ptr [edi], 0         ;null terminate the result string
    invoke StdOut, addr m00
    invoke StdOut, addr result
    invoke StdOut, addr newLine
    jmp   @f

m01 db    13, 10, 'This is the low byte of the number in hex', 13, 10, 0
@@: mov   ecx,2
    mov   rax, numb                 ;the number to rax
    mov   ebx, 16                   ;the base to rbx
    mov   edi, offset result        ;the destination to edi
    invoke ctAnyToAny
    mov   byte ptr [edi], 0         ;null terminate the result string
    invoke StdOut, addr m01
    invoke StdOut, addr result
    invoke StdOut, addr newLine
    jmp   @f

m02 db    13, 10, 'This is the 16 bit number, in hex', 13, 10, 0
@@: mov   ecx,4
    mov   rax, numb                 ;the number to rax
    mov   ebx, 16                   ;the base to rbx
    mov   edi, offset result        ;the destination to edi
    invoke ctAnyToAny
    mov   byte ptr [edi], 0         ;null terminate the result string
    invoke StdOut, addr m02
    invoke StdOut, addr result
    invoke StdOut, addr newLine
    jmp   @f

m03 db    13, 10, 'This is the 32 bit number, in hex', 13, 10, 0
@@: mov   ecx,8
    mov   rax, numb                 ;the number to rax
    mov   ebx, 16                   ;the base to rbx
    mov   edi, offset result        ;the destination to edi
    invoke ctAnyToAny
    mov   byte ptr [edi], 0         ;null terminate the result string
    invoke StdOut, addr m03
    invoke StdOut, addr result
    invoke StdOut, addr newLine
    jmp   @f

m04 db    13, 10, 'This is the 64 bit number, in hex', 13, 10, 0
@@: mov   ecx,16
    mov   rax, numb                 ;the number to rax
    mov   ebx, 16                   ;the base to rbx
    mov   edi, offset result        ;the destination to edi
    invoke ctAnyToAny
    mov   byte ptr [edi], 0         ;null terminate the result string
    invoke StdOut, addr m04
    invoke StdOut, addr result
    invoke StdOut, addr newLine
    jmp   @f

m05 db    13, 10, 'This is the 64 bit number, in binary', 13, 10, 0
@@: mov   ecx,64
    mov   rax, numb                 ;the number to rax
    mov   ebx, 2                    ;the base to rbx
    mov   edi, offset result        ;the destination to edi
    invoke ctAnyToAny
    mov   byte ptr [edi], 0         ;null terminate the result string
    invoke StdOut, addr m05
    invoke StdOut, addr result
    invoke StdOut, addr newLine
    jmp   @f

m06 db    13, 10, 'And in Sumerian Sexagesimal, base 60', 13, 10, 0
@@: mov   ecx,10
    mov   rax, numb                 ;the number to rax
    mov   ebx, 60                   ;the base to rbx
    mov   edi, offset result        ;the destination to edi
    invoke ctAnyToAny
    mov   byte ptr [edi], 0         ;null terminate the result string
    invoke StdOut, addr m06
    invoke StdOut, addr result
    invoke StdOut, addr newLine

    invoke ExitProcess, 0           ;Error code 0
    ret
main ENDP

end

jj2007

It works :thumbsup:

This is the number we will be converting
123456789

This is the low byte of the number in hex
15

This is the 16 bit number, in hex
CD15

This is the 32 bit number, in hex
075BCD15

This is the 64 bit number, in hex
00000000075BCD15

This is the 64 bit number, in binary
0000000000000000000000000000000000000111010110111100110100010101

And in Sumerian Sexagesimal, base 60
000009VXX9

NoCforMe

Quote from: ahsat on March 31, 2024, 11:33:25 AM
Quote from: NoCforMe on March 31, 2024, 11:18:43 AMWouldn't you need 60 unique "digits" for representation?

I gave you the digits I am using. Again they are:
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkylmnopqrstuvw'

Yes, sorry, I miscounted (I missed that you put kyl in there). Pretty kewl ... like the recursion, too.
Assembly language programming should be fun. That's why I do it.

ahsat


ahsat

Quote from: NoCforMe on March 31, 2024, 12:13:07 PMlike the recursion, too.

My first program was a plug board. Recursion was common then.

NoCforMe

Assembly language programming should be fun. That's why I do it.

ahsat


jj2007

Quote from: ahsat on March 31, 2024, 11:44:31 AMThis algorithm is not the fastest power of 2 bases like binary, octal and hex. But from looking around a bit, I think AnyToAny and ctAnyToAny are unique. Few programmers ever think about recursion.

I like the idea. I made some quick tests: 10 Million numbertohex conversions run in about 900 milliseconds on my AMD Athlon Gold 3150U. For comparison, Hex$ does it in less than a quarter of the time, but since it's 32-bit code, it's limited to DWORDs.

Compliments, that's really good, even for somebody who is only 14 years older than I am ;-)

NoCforMe

Assembly language programming should be fun. That's why I do it.

ahsat

Quote from: NoCforMe on March 31, 2024, 01:00:44 PMSo what is "plug board" programming then?

Some of the original computers used wiring boards. Instructions were plugging wires from one hole in the board to another hole. You then added wires to tell that instruction what registers to use. The one I programmed had only registers, no memory. I can't remember all the details, but the registers were very large, probably 128 bits or more. I also can't remember how many registers it had, probably 16 or 32.

ahsat

Quote from: jj2007 on March 31, 2024, 12:58:43 PM10 Million numbertohex conversions run in about 900 milliseconds

I think the algorithms will hold their own, speed wise, on bases like base 10. You are obviously faster than I am. You are probably active, and I have been inactive since around 2000.

I would be very interested in a speed test of base 10. Also, it should be simple and quick to convert to 32 bit. Since coming alive again, I have only been fooling around with 64 bit. It would probably take me a day or so to setup a 32 bit version.