conversion number ascii

All 680x0 related coding posts in this section please.

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

conversion number ascii

Postby rockyone » Wed Apr 13, 2016 6:15 pm

Research example to convert a hexadecimal number in ASCII
ex :$1492 in $35, $32, $36, $36

User avatar
Anima
Atari Super Hero
Atari Super Hero
Posts: 642
Joined: Fri Mar 06, 2009 9:43 am
Contact:

Re: conversion number ascii

Postby Anima » Wed Apr 13, 2016 7:22 pm

Input: d0.w
Output: hexval

Code: Select all

tohex:
 lea hexval,a0
 move.l #'0000',(a0)+
_loop:
 moveq #$f,d1
 and d0,d1
 move.b hextbl(pc,d1),-(a0)
 lsr #4,d0
 bne _loop
 rts

hexval:
 dc.b 0,0,0,0,0
 
hextbl:
 dc.b '0123456789abcdef'


Edit: bug fix.
Last edited by Anima on Wed Apr 13, 2016 11:05 pm, edited 1 time in total.

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Wed Apr 13, 2016 9:26 pm

Thank you Anima

User avatar
Anima
Atari Super Hero
Atari Super Hero
Posts: 642
Joined: Fri Mar 06, 2009 9:43 am
Contact:

Re: conversion number ascii

Postby Anima » Wed Apr 13, 2016 11:07 pm

Please note the fixed bug: lsr #8,d0 changed to lsr #4,d0 ;)

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Wed Apr 13, 2016 11:30 pm

yes, I saw, but I board not tested

In fact, I want the decimal value in bytes
$1492 =5266
in AsCII $35, $32, $36, $36

The decimal value of the number Would Be enough!

User avatar
Anima
Atari Super Hero
Atari Super Hero
Posts: 642
Joined: Fri Mar 06, 2009 9:43 am
Contact:

Re: conversion number ascii

Postby Anima » Thu Apr 14, 2016 4:04 am

Input: d0.w (unsigned)
Output: decval

Code: Select all

todec:
 lea decval,a0
 move.l #'0000',(a0)+
 move.b #'0',(a0)+
_loop:
 ext.l d0
 beq _exit
 divu #10,d0
 swap d0
 add.b d0,-(a0)
 swap d0
 bra _loop
exit:
 rts

decval:
 dc.b 0,0,0,0,0,0

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Thu Apr 14, 2016 12:23 pm

thank you Anima
It is enough to add
add.b $30,(a0)

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Fri Apr 15, 2016 11:47 am

I adapted to my needs.
how to optimize?
d1 = minimum 3 maximum 32000

Code: Select all

hex_ascii:
   moveq.l   #0,d2
   
decimal:
   ext.l      d1
   beq.s      ascii
   divu.w     #10,d1
   swap       d1
   lsl.l      #4,d2
   add.b      d1,d2
   swap       d1
   bra.s      decimal
      
ascii:
   move.b     d2,(a2)
   and.b      #$f,(a2)
   add.b      #$30,(a2)+
   lsr.l      #4,d2
   bne.s      ascii

   move.b     #$d,(a2)+
   move.b     #$a,(a2)+
   rts

nanard
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 107
Joined: Mon Apr 04, 2016 2:11 pm

Re: conversion number ascii

Postby nanard » Sat Apr 16, 2016 10:24 pm

rockyone wrote:I adapted to my needs.
how to optimize?
d1 = minimum 3 maximum 32000

Code: Select all

hex_ascii:
   moveq.l   #0,d2
   
decimal:
   ext.l      d1
   beq.s      ascii
   divu.w     #10,d1
   swap       d1
   lsl.l      #4,d2
   add.b      d1,d2
   swap       d1
   bra.s      decimal
      
ascii:
   move.b     d2,(a2)
   and.b      #$f,(a2)
   add.b      #$30,(a2)+
   lsr.l      #4,d2
   bne.s      ascii

   move.b     #$d,(a2)+
   move.b     #$a,(a2)+
   rts

I think your version has issues with numbers ending with 0. Like 1000. Because d2 will be 0 just after you write the last non zero digit.
There is a way to fix. add 1 to value stored in d2.

Code: Select all

   lsl.l      #4,d2
   add.b      d1,d2
+   add.b      #1,d2
   swap       d1
   bra.s      decimal
      
ascii:
   move.b     d2,d1
   and.b     #$f,d1
   add.b   #$2f,d1
   move.b   d1,(a2)+
-   move.b     d2,(a2)
-   and.b      #$f,(a2)
-   add.b      #$30,(a2)+
   lsr.l      #4,d2
   bne.s      ascii
4MB STE + CosmosEx /|\ MegaST4 + MegaFile 44

nanard
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 107
Joined: Mon Apr 04, 2016 2:11 pm

Re: conversion number ascii

Postby nanard » Sat Apr 16, 2016 10:27 pm

see my own code, which works between 0 and 655359 (the result of divu.w has to fit in 16bit) :
https://github.com/miniupnp/AtariST/blo ... ply.s#L356
4MB STE + CosmosEx /|\ MegaST4 + MegaFile 44

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Sun Apr 17, 2016 3:55 am

Oh yes, I Was left with $1492 :oops:

I had done some tests aléatoires..without zeros :mrgreen:

would I have seen the bug quickly ?
with the probalitées than the number ends in zero .. :coffe:


Google translate :twisted:

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Sun Apr 17, 2016 4:58 am

this should be better now
I do not know the address of (a2) when I wrote the Nombe ascii.
(A2)
- is an output buffer.
- Its address is odd or even at the beginning of the subroutine.
- No space before and after the number ascii

Code: Select all

hex_ascii:
   moveq.l   #0,d2
   
decimal:
   ext.l      d1
   beq.s      ascii
   divu.w     #10,d1
   swap       d1
   lsl.l      #4,d2
   add.b      d1,d2
   add.b    #1,d2             ;+1
   swap       d1
   bra.s      decimal
     
ascii:
   move.b     d2,(a2)       ; even or odd address
   and.b      #$f,(a2)
   add.b      #$2f,(a2)+      ; -1
   lsr.l      #4,d2
   bne.s      ascii

   move.b     #$d,(a2)+
   move.b     #$a,(a2)+
   rts

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Tue Apr 19, 2016 12:24 pm

always bored with the conversions.
How to convert back?

nanard
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 107
Joined: Mon Apr 04, 2016 2:11 pm

Re: conversion number ascii

Postby nanard » Wed Apr 20, 2016 9:09 pm

rockyone wrote:always bored with the conversions.
How to convert back?

you mean implementing atoi() ? just mulu #10 and add each digit...
4MB STE + CosmosEx /|\ MegaST4 + MegaFile 44

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Thu Apr 21, 2016 4:15 am

no
I resumed the same number as example
#5266 = #$1492
ascii conversion '35323636 ' good

Now, how to convert '32353636' to $1492


ca seems very complicated to me 68000.
Fortunately the number that interests me always begins in the twelfth bytes

I am bad :angel:
I obtained $5266 :lol:

nanard
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 107
Joined: Mon Apr 04, 2016 2:11 pm

Re: conversion number ascii

Postby nanard » Sat Apr 23, 2016 11:20 pm

That's really simple :

Code: Select all

atoi:
  moveq.l #0,d0
  moveq.l #0,d1
.loop:
  move.b (a0)+,d1
  tst.b d1
  beq .end
  subi.b #$30,d1
  mulu.w #10,d0
  add.w d1,d0
  bra .loop
.end:
  rts
4MB STE + CosmosEx /|\ MegaST4 + MegaFile 44

rockyone
Captain Atari
Captain Atari
Posts: 307
Joined: Thu Jan 20, 2011 8:47 pm
Contact:

Re: conversion number ascii

Postby rockyone » Sun Apr 24, 2016 8:06 pm

Actually yes it is very simple.
Thanks for the help


Social Media

     

Return to “680x0”

Who is online

Users browsing this forum: No registered users and 3 guests