flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [solved] Flat assember G - Macros and address calculation

Author
Thread Post new topic Reply to topic
ctlaltdel



Joined: 10 Aug 2020
Posts: 4
ctlaltdel
Hi all,

I am writing some code for a 'move machine', i.e. a 'one-instruction' CPU that has an instruction format of <src>, <dst> i.e. move byte from 16-bit source address to 16-bit destination address. Memory is byte-addressable. Computation is carried out via pre-computed tables and self-modifying code.

I have a problem with the address calculation of a local label within a macro. Within the 'table_lookup' macro definition, the local label l1 is defined after another macro ('zmgen') is invoked. The 'zmgen' macro is passed l1 as a parameter as it is supposed to overwrite data at this location. 'zmgen' conditionally invokes yet another macro ('pbrk') if the lower 8 bits of the current assembly address is 0x00.
The problem is that it seems the address of l1 is calculated before 'zmgen' and 'pbrk' are assembled, and so that if assembly of the macro starts at 0x??00, the effective address of l1 is incorrect. Code below:

Code:
; insert page break code
        macro   pbrk
                dw      t_ident+12,IP                   ; do jump past this code
                dw      jdest+1,IP+1                    ; do high part of jump
                dw      jdest,IP                        ; do low part of jump
        end macro

; generate one instruction, account for 256-byte page breaks
        macro   zmgen   s,d
                if      ($ and $ff) = 0
                        pbrk
                end if
                P isname s
                if P = 1
                        dw   t_ident+s,d
                else
                        dw   s,d        
                end if                  
        end macro

; table lookup 
        macro   table_lookup    table, offset, dest
                zmgen   offset, l1
                local   l1
l1:
                zmgen   table, dest
        end macro
    


Is there a way to have l1 calculated after 'zmgen' and 'pbrk' are assembled? Is there a better way?

Thanks in advance
Post 10 Aug 2020, 09:21
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7783
Location: Kraków, Poland
Tomasz Grysztar
I see one probable problem here:
Code:
        macro   table_lookup    table, offset, dest
                zmgen   offset, l1
                local   l1
l1:
                zmgen   table, dest
        end macro
    
The LOCAL directive applies only to uses of "l1" symbol that follow in the macro (similarly to how it works in fasm 1). Therefore your "zmgen offset,l1" line is not affected and refers to the global "l1" symbol, while later uses of "l1" within that macro refer to the local one, a distinct symbol specific to each instance the macro is called.

You should move "local l1" line to the beginning of the macro in order for it to affect all uses of the "l1" name.
Post 10 Aug 2020, 09:58
View user's profile Send private message Visit poster's website Reply with quote
ctlaltdel



Joined: 10 Aug 2020
Posts: 4
ctlaltdel
Actually, it was originally at the beginning of the macro - I moved it to see what effect it would have (nothing useful), and forgot to move it back.
Post 10 Aug 2020, 10:07
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7783
Location: Kraków, Poland
Tomasz Grysztar
What value is incorrect then? Can you give a minimal complete source that can be assembled to demonstrate your problem?
Post 10 Aug 2020, 10:10
View user's profile Send private message Visit poster's website Reply with quote
ctlaltdel



Joined: 10 Aug 2020
Posts: 4
ctlaltdel
I'll do a bit more investigation and see if I can come up with a compact demonstration of the problem.
Post 11 Aug 2020, 06:07
View user's profile Send private message Reply with quote
ctlaltdel



Joined: 10 Aug 2020
Posts: 4
ctlaltdel
Ok, found (and fixed) the problem. It was of course, my fault, not the assembler's. I overlooked the fact that the 'zmgen' macro invokes the 'pbrk' macro on a byte boundary, so an earlier instruction that is supposed to overwrite part of the source address of the 'zmgen' instruction actually overwrites part of the 'pbrk' code instead, causing the wheels to fly off. Oops. Added conditional assembly of an offset and all is good.

Anyway, thanks for a great assembler, Tomasz!
Post 11 Aug 2020, 08:26
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.