flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How in macro convert text to int or float number ?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1839
Roman 16 Apr 2023, 14:38
Fasmw 1.73
Code:
macro fltDat {}

fltDat "5.55" ;convert text to dd 5.55

flt1 equ '4.5430'
fltDat flt1 ;convert text to dd 4.5430
    
Post 16 Apr 2023, 14:38
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1020
Location: Russia
macomics 16 Apr 2023, 15:23
1)First step
Code:
db 'dd ', fltDat, 13, 10, 'dd ', fil1    

Second
Code:
fasm -m 1024 ./temp.asm ./temp.fasm
flat assembler  version 1.73.30  (1024 kilobytes memory)
1 passes, 15 bytes.    

Third
Code:
fasm -m 1024 ./temp.fasm ./temp.bin
flat assembler  version 1.73.30  (1024 kilobytes memory)
1 passes, 8 bytes.    

Forth
Code:
; data section
file 'temp.bin'    


2)
Code:
macro cvrt_int dir=dd, val&
{ ; It takes a long time to write in the browser. I'll write a little later. But the meaning is this:
  ; in the virtual block, you collect text using a separator character (say ';') and in the same virtual,
  ; you collect cycles of reading text by characters through repeat and accumulating values in a variable until you meet a separator.
  ; When you hit the separator, you save the accumulated value in, say, label and collect the label name in a list.
  ; After closing virtual, use dir to create values from this list by label names
}

; macro cvrt_flt ...    


3) Write a separate program to convert the text read from the file and save it to a binary file.
Post 16 Apr 2023, 15:23
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 16 Apr 2023, 15:37
Third step not needed.

Fourth step (now the new third step):
Code:
include 'temp.fasm'    
Post 16 Apr 2023, 15:37
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 16 Apr 2023, 15:39
macomics wrote:
Forth
Freudian slip? Laughing

Sometimes I wonder if Roman is writing a fully-fledged HLL compiler with fasm macros. Confused
Post 16 Apr 2023, 15:39
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1839
Roman 16 Apr 2023, 15:58
Quote:

Write a separate program to convert the text read from the file and save it to a binary file.


This variant I do and using now.
But I want do simple convert in fasm macro. Sometime its handful.
Post 16 Apr 2023, 15:58
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1020
Location: Russia
macomics 16 Apr 2023, 16:07
revolution wrote:
Third step not needed.

Fourth step (now the new third step):
So apparently the file contains constant data. Of course, they can be included as a source file. But isn't it better to process it separately and once?
Post 16 Apr 2023, 16:07
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1839
Roman 16 Apr 2023, 16:30
Idea little trick.
macro fltDat search text in Tabflts.
Code:
Virtual at 0
Tabflts::
Txt   db "5.55"
      dd 5.55
      db "4.5430"
      dd 4.5430
end virtual

;or this
Virtual at 0
Tabflts::
Txt   db "5.55"
      db "4.5430"
end virtual
Virtual at 0
Flts::
      dd 5.55
      dd 4.5430
end virtual
    
Post 16 Apr 2023, 16:30
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 16 Apr 2023, 17:13
Well, if you are that desperate, you could try something like this:
Code:
macro fltDat str* {
  local buffer, next
  define buffer f
  rept 8 i:1 \{
        rept 1 c: str shr ((8-i)*8) and 0FFh - 48 \\{
          match f, buffer \\\{
             define next 0
             match -=48, c \\\\{ define next 1 \\\\}
             match -=2, c \\\\{ define buffer .\\\#f
                                define next 1 \\\\}
             match =0, next \\\\{ define buffer c\\\#f \\\\}
          \\\} \\} \}
  dd buffer
}

fltDat "5.55"

flt1 equ '4.5430'
fltDat flt1    
But I have a feeling that what you asked for is not what you really want.
Post 16 Apr 2023, 17:13
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1839
Roman 16 Apr 2023, 17:23
Thanks.
Cool macro.
Post 16 Apr 2023, 17:23
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1020
Location: Russia
macomics 16 Apr 2023, 17:29
Tomasz Grysztar wrote:
Well, if you are that desperate, you could try something like this:
And what happens if the string length exceeds 8 characters?

Code:
macro fltDat str* {
  local buffer, next
  define buffer f
  rept 8 i:1 \{
        rept 1 c: str shr ((8-i)*8) and 0FFh - 48 \\{
          match f, buffer \\\{
             define next 0
             match -=48, c \\\\{ define next 1 \\\\}
             match -=2, c \\\\{ define buffer .\\\#f
                                define next 1 \\\\}
             match =0, next \\\\{ define buffer c\\\#f \\\\}
          \\\} \\} \}
  dd buffer
}

fltDat "5.50000500000"

flt1 equ '4.54300000000000001'
fltDat flt1    

Code:
fasm -m 1024 ./temp.asm ./temp.fasm
flat assembler  version 1.73.30  (1024 kilobytes memory)
./temp.asm [16]:
fltDat "5.50000500000"
./temp.asm [5] fltDat [4]:
        rept 1 c: str shr ((8-i)*8) and 0FFh - 48 \\{
./temp.asm [5] rept [1]:
        rept 1 c: str shr ((8-i)*8) and 0FFh - 48 \\{
error: value out of range.    
Post 16 Apr 2023, 17:29
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 16 Apr 2023, 17:34
The limitations of fasm's engine are obvious, but this doesn't matter anyway, because this macro is useless for any practical purpose. It can only handle literals that available at preprocessing time.
Post 16 Apr 2023, 17:34
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 16 Apr 2023, 17:37
Next we need a triple precision module to handle strings up to 24 characters. Razz
Code:
fltDat "3.141592635389793238e-245" ; 25 characters! It won't fit into triple precision.    
Oops, I mean quadruple precision. Smile
Post 16 Apr 2023, 17:37
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1839
Roman 16 Apr 2023, 17:58
For me normal 3.1415926 accuracy 7 digits

"-1.0" wrong value macro fltDat


Last edited by Roman on 16 Apr 2023, 18:06; edited 2 times in total
Post 16 Apr 2023, 17:58
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 16 Apr 2023, 18:05
Roman wrote:
For me normal 3.1415926 accuracy 7 digits
9 characters.
Post 16 Apr 2023, 18:05
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1839
Roman 16 Apr 2023, 18:07
Ok 3.141592 for me
Post 16 Apr 2023, 18:07
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 16 Apr 2023, 18:15
Roman wrote:
"-1.0" wrong value macro fltDat
Add handling for the "-" character:
Code:
             match -=3, c \\\\{ define buffer -f
                                define next 1 \\\\}    
Post 16 Apr 2023, 18:15
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 16 Apr 2023, 18:17
Code:
1e6    
Smile

And
Code:
1E6    
And
Code:
1.2f    
And
Code:
1.61F    
Post 16 Apr 2023, 18:17
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 16 Apr 2023, 18:21
I'm waiting for something else.
Post 16 Apr 2023, 18:21
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 16 Apr 2023, 18:23
We need arithmetic also.
Code:
3.2*1.8-2.1/7.8**-0.1+sqrt(7.02)    
Post 16 Apr 2023, 18:23
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 16 Apr 2023, 18:24
The macro does exactly what was asked for. But I'm pretty sure that's mostly useless.
Post 16 Apr 2023, 18:24
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.