flat assembler
Message board for the users of flat assembler.

Index > Main > [solved] [fasmg] Peculiar results in CALM with define

Author
Thread Post new topic Reply to topic
Calanor



Joined: 19 Jul 2015
Posts: 45
Location: Sweden
Calanor 14 Jan 2021, 10:55
So, I figured I'd write a small CALM instruction in order to try to get the hang of things. The idea was to write code that would define and assign a value to a name if it has not been previously defined. Inspired by Tomasz suggestion to use take or transform to check if a name's been defined, I wrote a small instruction to try things out.
Code:
calminstruction setdefaultvalue? var*, arg*&
        transform var
        jyes var_defined
                local cmd
                arrange cmd, =define var arg
                assemble cmd
        var_defined:
end calminstruction

;define testing 'defined'
SetDefaultValue testing, 'default'
display testing, 13, 10    

This will result in the compiler reporting that it failed to generate the code within the allowed number of passes. I also tried using arrange at the start of the code to copy "var" into a temporary variable and use that one instead when assembling the line, but that made no apparent difference.

The final "display" is actually executed in spite of the generated error and will display the correct result. I've noticed when writing other CALM code that the compiler doesn't actually seem to stop when encountering a CALM-related error, which has caused some confusion and made bug hunting more complicated.

Regarding arrange, I don't necessarily have to declare a variable as local before using it as the destination for arrange. How is this handled by the compiler? Will it automatically consider the variable to be a new local one and if so is there any actual reason why one would like to declare it as local anyway? Will the compile speed be affected in any way?
Post 14 Jan 2021, 10:55
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 14 Jan 2021, 11:41
When a symbol is defined exactly once in the source and it is not forced by any other means to be considered "variable", it is allowed to be forward-referenced. Your problem is therefore similar to the classic one:
Code:
if ~ defined testing
    testing = 1
end if    
which is an example of self-contradicting code that fasm[g] cannot resolve and has been discussed even in the old manual for fasm 1 (I also wrote an article about pitfalls of optimistic multi-pass assembly, which delves deeper into the topic).

In some of the macro sets for fasmg you may notice specific methods used to ensure that no disruptive forward-referencing occurs:
Code:
if ~ defined SSE

        restore SSE     ; this ensures that symbol cannot be forward-referenced
        SSE = 1    

Calanor wrote:
I've noticed when writing other CALM code that the compiler doesn't actually seem to stop when encountering a CALM-related error, which has caused some confusion and made bug hunting more complicated.
This is in fact related to the fact that fasmg allows to forward-reference almost everything - the assembler cannot terminate the process prematurely on any error, because it may turn out that something constructed later in the source actually makes the error go away in the future passes.

Calanor wrote:
Regarding arrange, I don't necessarily have to declare a variable as local before using it as the destination for arrange. How is this handled by the compiler? Will it automatically consider the variable to be a new local one and if so is there any actual reason why one would like to declare it as local anyway? Will the compile speed be affected in any way?
During CALM definition you are inside its dedicated namespace very similarly to when you enter a child namespace with NAMESPACE directive. The usual rules apply and the symbols that have not yet been defined locally are looked up in the chain of parent namespaces, and if no definition is found, it falls back to the default, which is case-sensitive symbol in the local namespace. Pre-defining the symbol as local makes it skip the parent lookup, but this would only affect the speed of processing the CALM definition - the actual CALM code is going to be just as fast either way, as it no longer refers to names, but uses direct pointers to symbol leafs.
Post 14 Jan 2021, 11:41
View user's profile Send private message Visit poster's website Reply with quote
Calanor



Joined: 19 Jul 2015
Posts: 45
Location: Sweden
Calanor 14 Jan 2021, 11:57
Even though I was aware of the define mechanics in fasm/fasmg, I admit that it's not how I'm used to think, so I occasionally mind blank when encountering this issue. Using restore before the define did indeed solve this problem, many thanks!
Post 14 Jan 2021, 11:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20339
Location: In your JS exploiting you and your system
revolution 14 Jan 2021, 12:08
Tomasz Grysztar wrote:
When a symbol is defined exactly once in the source and it is not forced by any other means to be considered "variable", it is allowed to be forward-referenced. Your problem is therefore similar to the classic one:
Code:
if ~ defined testing
    testing = 1
end if    
which is an example of self-contradicting code that fasm[g] cannot resolve and has been discussed even in the old manual for fasm 1 (I also wrote an article about pitfalls of optimistic multi-pass assembly, which delves deeper into the topic).
We can do this to resolve:
Code:
if ~ defined testing
        testing = 1
        testing = 1
end if
display testing + '0'    
Post 14 Jan 2021, 12:08
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:  


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