flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > Using load and store with forward reference possible ?

Author
Thread Post new topic Reply to topic
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 12 Jan 2013, 22:13
I am working on the parser / assembler for a new flat assembler version supporting a new target.

My question is, I want to check the generated code in assembler section and tried to evaluate some data created with assembler directives. The problem: This works only with backward reference to already assembled code, not with forward reference.

So I tried to do same with load and store but they also work only with backward reference.

Example:

Code:
use16

db 10h dup('0')

load xyz byte from $$+0fh
store byte xyz at $$+0fh-1

db 10h dup('0')

    


This works. When I try to do this with code generated "after" (second data block) this does not work (try to access $$+10h with load).
Value out of range appear.

This is really a pity. I don't understand why it is not possible because the parser could resolve this when just adding an additional pass.

In my program I only have to read code and adjust some (other) data when a special sequence occur. This does only work backward not forward.

I could write an "after parser" or "after assembler" process but this is very unconvenient to add. So the question is, is there a simple way to get this request running with FASM easily and are there special reasons why forward references are not allowed in this case ?

The data which should be read will not be changed anyway, just other data is adjusted. There will be no situation that the code could not be resolved in my case.
Shocked
Post 12 Jan 2013, 22:13
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20625
Location: In your JS exploiting you and your system
revolution 12 Jan 2013, 22:44
The reason you can't forward reference to future output is because the output does not yet exist when the load/store is encountered.

How would this work?
Code:
org 0
store byte 0xff at 1
dw 0 ;what value is stored here?    
Post 12 Jan 2013, 22:44
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 12 Jan 2013, 23:36
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 22:02; edited 1 time in total
Post 12 Jan 2013, 23:36
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 13 Jan 2013, 00:17
revolution wrote:
The reason you can't forward reference to future output is because the output does not yet exist when the load/store is encountered.

How would this work?
Code:
org 0
store byte 0xff at 1
dw 0 ;what value is stored here?    


It would work the same way like forward reference of labels is resolved.

In the first pass byte at position 1 does not exist and can not be written.
This is the same condition when we reference a label which is not yet defined.

So the usual way to resolve is to do an additional pass and in the second pass the byte at position 1 is defined through the first pass and could be overwritten.
And of course a store reference has a higher priority than a simple data definition.

I think I did describe exactly what I want in my first post. Wink
Post 13 Jan 2013, 00:17
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20625
Location: In your JS exploiting you and your system
revolution 13 Jan 2013, 06:47
Such a scheme would entail delaying the processing of all load/store commands until assembly has finished. I guess it might be possible but it seems like too much effort to solve a problem that would be better solved by another means.
Post 13 Jan 2013, 06:47
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 13 Jan 2013, 22:12
Yes you maybe right, but it would be a nice feature, wouldn't it ? Very Happy
Post 13 Jan 2013, 22:12
View user's profile Send private message Send e-mail Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 13 Jan 2013, 22:28
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 22:01; edited 1 time in total
Post 13 Jan 2013, 22:28
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 15 Jan 2013, 16:29
shutdownall
Quote:
It would work the same way like forward reference of labels is resolved.

Bad analogy. Labels cannot be redefined. Load/store forward referencing is not possible for the same reason, that does not allow redefined numeric constants to be forward referenced. If you do a store, it's already a redefinition of what was stored before.
The revolution's example may be not that clearly emphasizes the problem. Look at this example:
Code:
org 0
store byte 1 at 0
load x byte from 0 
store byte 2 at 0
db 0    

What value should have x? Is that what we stored with the first store? Is that what should be resolved after the final pass? What number of passes do we need to resolve the following?
Code:
org 0
load x byte from 0 
x = x + 1
store byte x at 0
db 0    
Post 15 Jan 2013, 16:29
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 15 Jan 2013, 19:50
Bad example.
It depends on what you do and what you want.
As I pointed out I want to read only data and modify other data depending on it's content.

There can be always situations that can't be resolved regardless of using load / store or not.

What do you say to following code in an assembly section:
Is the redefinition of assembler time variables not allowed because it could be used in a bad way ?

Code:
var1=var2+1
var2=var1
    


Or next example with labels:

Code:
label1:
        db label2+1 dup(0)
label2:
    


How many passes does it need ?

So I can accept that some functions not realized because they are not needed or planned that way but the reason of disabling features only because they could be missused or missinterpreted are not acceptable for me. Razz
Post 15 Jan 2013, 19:50
View user's profile Send private message Send e-mail Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 16 Jan 2013, 09:34
shutdownall
Quote:
Bad example.
It depends on what you do and what you want.

Obviously you didn't understand the examples. If the second example would backward reference the data, it would be a valid and quite common construction. By allowing load/store forward referencing you change the semantics of the load/store, which would also affect backward referencing. Thus your idea would break much more useful things, than it would allow to do.

Quote:
Is the redefinition of assembler time variables not allowed because it could be used in a bad way ?

None of these examples shows redefinition. In fact, if you tried to use redefinition, you wouldn't be able to forward-reference. As I already said the same situation occurs with forward-storing, cause storing is by definition a redefinition.

Quote:
but the reason of disabling features only because they could be missused or missinterpreted are not acceptable for me

The reasoning behind not enabling load/store forward referencing is not just because it can be misused. Actually only rare cases exist, when it's possible to not misuse it.
Post 16 Jan 2013, 09:34
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.