flat assembler
Message board for the users of flat assembler.

Index > Main > [fasmg] how to use virtual for the output file?

Author
Thread Post new topic Reply to topic
v142857



Joined: 23 Apr 2023
Posts: 5
v142857 23 Apr 2023, 13:39
the docs says
Quote:
The "virtual" creates a special output area which is not written into the main output file.


I would like to have a named section in the output that I can append later, how can I do that?

The context is that I am creating an assembler to program WASM files in fasmg, to get a hang on how everything works
Post 23 Apr 2023, 13:39
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1807
Roman 23 Apr 2023, 13:41
We need in to preprocessing implemented call user dll from Virtual or usr_dll.
User dll get fasm data ,parsed and out back to fasm.

Than fasm can easily calculated sqrt,sin,cos,matrix multiplication, apply vectors xyz and many etc.
Post 23 Apr 2023, 13:41
View user's profile Send private message Reply with quote
v142857



Joined: 23 Apr 2023
Posts: 5
v142857 23 Apr 2023, 14:19
what are you talking about Roman? You got me real confused here...

I want to have sections in the output file that I can later append as I process the source file...

What is this usr_dll?
Post 23 Apr 2023, 14:19
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 980
Location: Russia
macomics 23 Apr 2023, 14:45
Tell us more detail what it means: append later

Everything that will ever be used in the program is described within its source code.
If something needs to be added, then it's easy enough to add it to the source.

You are talking about some incomprehensible action that is not related to what virtual is intended for.
Post 23 Apr 2023, 14:45
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 23 Apr 2023, 14:56
The output areas cannot be expanded after they are defined. But there is a specific pattern that a multi-pass assembler like fasmg allows: reserve exact space that is needed, by forward-referencing the final count, something like:
Code:
rd NUMBER_OF_FUNCTIONS
postpone
  NUMBER_OF_FUNCTIONS := FUNCTION_COUNTER
end postpone    
where FUNCTION_COUNTER is the variable you increment every time you add a function. You can then fill this area with STORE, but even stronger uses of forward-referencing are possible - you could forward-reference all individual addresses through numbered variable names to construct a table, etc.

If you look at my old WASM prototype, you may find similar approach used to construct all the collected tables.
Post 23 Apr 2023, 14:56
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 23 Apr 2023, 16:26
If you prefer to work with appended virtual areas, there's even an option of forward-referencing the entire final content (kept as a string of bytes):
Code:
db LIST_BYTES

virtual at 0
   List::
end virtual

virtual List
  dd 'item'
end virtual

virtual List
  dd 'more'
end virtual

load LIST_BYTES: sizeof List from List:0    
Moreover, a forward LOAD is also allowed (but only from areas that are never modified with STORE):
Code:
load LIST_BYTES: LIST_LENGTH from List:0
db LIST_BYTES

virtual at 0
   List::
end virtual
postpone
  LIST_LENGTH := sizeof List
end postpone

virtual List
  dd 'item'
end virtual

virtual List
  dd 'more'
end virtual    
Post 23 Apr 2023, 16:26
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 23 Apr 2023, 16:42
And yet another option - the macroinstructions can be forward-referenced as well, so you can just collect blocks of source code and then execute them all in a single place to dynamically generate the structure you need:
Code:
repeat NUMBER_OF_OBJECTS
        Object#%
end repeat

OBJECT_ID = 0
postpone
  NUMBER_OF_OBJECTS := OBJECT_ID
end postpone

calminstruction object?
        compute OBJECT_ID, OBJECT_ID + 1
        arrange declaration, =macro =Object#OBJECT_ID
        assemble declaration
end calminstruction

macro end?.object?!
        esc end macro
end macro

object
        db 'abc'
end object

object
        db 'efg'
end object    
This approach could be useful, for example, when you might need to be able to shuffle the objects.
Post 23 Apr 2023, 16:42
View user's profile Send private message Visit poster's website Reply with quote
v142857



Joined: 23 Apr 2023
Posts: 5
v142857 23 Apr 2023, 18:12
macomics wrote:
Tell us more detail what it means: append later

You are talking about some incomprehensible action that is not related to what virtual is intended for.


I am parsing a source code with a syntax that resembles WAT, to WASM encoding.

But the issue is: a WASM module has an explicit order where the types signatures go first, then the export section, then the function section, then the code section...

but my source code can look like
Code:
func main result i32
  i32.const 42
end func
export func main as "main"
    


and this source has to be broken down dynamically and fill each section and the size of each section...

the virtual operation was the closest I saw in the fasmg docs, so thats why I started asking about it

Tomasz already gave multiple options about how to do that so now I have some idea in how to approach this problem
Post 23 Apr 2023, 18:12
View user's profile Send private message Reply with quote
v142857



Joined: 23 Apr 2023
Posts: 5
v142857 23 Apr 2023, 23:01
So, about the virtual memory approach one because looks like the easier to understand and how to apply in my specific case:
Code:
define sections_names custom, type, import, func, table, mem, global, export, start, elem, code, data
iterate name, sections_names
    name#_section:
end iterate
virtual at 0
    iterate name, sections_names
        name::
    end iterate
end virtual
postpone
    iterate name, sections_names
        load name#_section:sizeof name from name:0
    end iterate
end postpone
    


will that assemble the contents of each virtual section to the output, in order, in the end of the assembly process?

I am getting issues with the iterate and I dont have yet the parsing to fill the sections, so thats why I need to ask that in this phase
Post 23 Apr 2023, 23:01
View user's profile Send private message Reply with quote
v142857



Joined: 23 Apr 2023
Posts: 5
v142857 24 Apr 2023, 15:24
just for the record, thats the final form of the solution:
Code:
define sections_names custom, type, import, func, table, mem, global, export, start, elem, code, data
...
iteratelist name, sections_names
    if name#_section <> 0
        db section.name.id
        encode_u sizeof name
        db name#_section
    end if
end iterate
iteratelist name, sections_names
    virtual at 0
        name::
    end virtual
end iterate
    
postpone
    iteratelist name, sections_names
        load name#_section:sizeof name from name:0
    end iterate
end postpone

calminstruction iteratelist?! param, var
    transform var
    arrange param, =iterate param, var
    assemble param
end calminstruction
...
    
Post 24 Apr 2023, 15:24
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.