flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > load directive, forward reference

Author
Thread Post new topic Reply to topic
Tukang Paip



Joined: 09 Jun 2004
Posts: 11
Location: Malaysia
Tukang Paip
1. why the code below compiled OK?
Code:
dd foo
load foo from 0
    


it seems to me the 2 lines are dependent on each other..

2. why it isn't possible to write something like
Code:
dd foo
foo=0
foo=1
    
Post 04 Sep 2004, 07:04
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
2. because symbols that have their value assigned more than once cannot be forward refernced.
Post 04 Sep 2004, 07:27
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tukang Paip



Joined: 09 Jun 2004
Posts: 11
Location: Malaysia
Tukang Paip
Quote:

2. because symbols that have their value assigned more than once cannot be forward refernced.


can you explains more about this?
Post 04 Sep 2004, 07:32
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7756
Location: Kraków, Poland
Tomasz Grysztar
1. Load cannot know such dependencies - it just loads the bytes from code. This is an advanced directive and you should be careful when using it this way. All other assembler directives are designed to keep the whole code resolved correctly, but the "load"/"store" directive are an exception - this is a pair of advanced directives, to allow some advanced hacks to be done at assembly time. I wouldn't recommend them for beginner though.
Post 04 Sep 2004, 08:26
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
2. if you define constant only once in code (like label, which is too just a numeric constant), then it has same value anywhere in code, even before it is defined. but you are allowed to create variables that doesn't have same value everywhere in code, so you can do things like "a = a + 1". then, value of last time when such "variable" was redefined is used, so
Code:
a = 1
;here a is 1
a = a*5
;here a is 5
a = a+5
;here a is 10
    

But you can't read such constant before some it's value is set first time:
Code:
;what should be value here???
a = 1
a = a*5
a = a+5
    
Post 04 Sep 2004, 11:33
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tukang Paip



Joined: 09 Jun 2004
Posts: 11
Location: Malaysia
Tukang Paip
i'm confused here..

1.
since foo is not yet known in dd foo, it cannot be assembled yet. So load directive should produce error since there was no code/data assembled at offset 0.

2. since fasm is multi-pass, why it just cannot resolve foo to the last value assigned?
Code:
dd foo     ;foo = 1 here
foo = 0
foo = 1
    


i don't really know the internal working of fasm's parser and assembler.
Post 05 Sep 2004, 03:17
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
Dragontamer



Joined: 24 Aug 2003
Posts: 84
Dragontamer
The syntax of the code is considered "the LAST time the value was assigned"

Thus, the thing doesn't "loop" so to speak. If someone saw this:

foo = 0
dd foo


Then you would think that foo is 0, right?

But if they had your method:

foo = 0
dd foo
; two hundred lines of code
foo = 1

Then the above, foo is 1, not zero.
Post 05 Sep 2004, 04:03
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7756
Location: Kraków, Poland
Tomasz Grysztar
1.
Tukang Paip wrote:
since foo is not yet known in dd foo, it cannot be assembled yet. So load directive should produce error since there was no code/data assembled at offset 0.

When "foo" is not known, the data is assembled with value 0 - the assembler has to put generate some code there in each pass, otherwise it would never be able to resolve the offsets.

2. Using the last value assigned is the thing fasm would do naturally, but I had put the error check code there to avoid the amibuguity when the constant gets redefined.
Dragontamer: you sample doesn't show this exactly, it should have the order as in Tukang's sample - in your the "foo" is anyway 0 at point of "dd foo" definition.
Post 05 Sep 2004, 07:48
View user's profile Send private message Visit poster's website Reply with quote
Dragontamer



Joined: 24 Aug 2003
Posts: 84
Dragontamer
I get it. K then.
Post 05 Sep 2004, 15:59
View user's profile Send private message Reply with quote
Tukang Paip



Joined: 09 Jun 2004
Posts: 11
Location: Malaysia
Tukang Paip
now i get it clear.. thanks privalov.. here's another thing (i don't want to create a new thread)

there's a problem with output filename for source file without extension, the last character is missing, e.g, for flat binary program :

source filename : "foo"
output filename : "fo.bin"

this happens when we don't explicitly specify output file name. My solution is to fix formats.inc :

original
Code:
      find_extension:
        dec     eax
        cmp     eax,[free_additional_memory]
        jb      extension_found
        cmp     byte [eax],'\'
        je      extension_found
        cmp     byte [eax],'/'
        je      extension_found
        cmp     byte [eax],'.'
        jne     find_extension
        mov     edi,eax
      extension_found:
    


fix :
Code:
      find_extension:
        dec     eax
        cmp     eax,[free_additional_memory]
        jb      no_file_extension
        cmp     byte [eax],'\'
        je      no_file_extension
        cmp     byte [eax],'/'
        je      no_file_extension
        cmp     byte [eax],'.'
        jne     find_extension
        mov     edi,eax
        jmp     extension_found
      no_file_extension:
        inc     edi
      extension_found:
    
Post 12 Sep 2004, 03:36
View user's profile Send private message Visit poster's website MSN Messenger 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.