flat assembler
Message board for the users of flat assembler.

Index > Main > [fasmg] Ways to Access the Low and High Bytes of Label?

Author
Thread Post new topic Reply to topic
zicklag



Joined: 10 May 2025
Posts: 6
zicklag 22 May 2025, 00:21
I'm using the excellent mos 6502 instruction macros to make an NES game with fasmg, and I'm really liking it so far!

One inconvenient thing I'm running into, though, is that I often need to extract the low and high bits from the address of a label.

I made a little macro that would help me do that with a custom instruction for assigning low and high variables, but it's not very readable to use.

Code:
macro low_byte name, value*
  name = ( value ) and $FF
end macro

macro high_byte name, value*
  name = ( ( value ) shr 8) and $FF
end macro

macro high_low_bytes high_name*, low_name*, value*
  high_byte high_name, value
  low_byte  low_name, value
end macro
    


I saw somebody add an instruction that defined a label and automatically added `.lo` and `.hi` labels using the defined label as a namespace, which seems like a great idea, but I'd like to be able to do that across the whole project for any label.

Another possibility might be to add the `<` and `>` syntax that I've seen in other assemblers that allow you to reference the low or high byte of a label when passing it into the instruction.

I think that'd require modifying the 6502 assembler macros I'm using.

Has anybody else needed to do anything similar and had any solutions they liked?[/code]
Post 22 May 2025, 00:21
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8434
Location: Kraków, Poland
Tomasz Grysztar 22 May 2025, 03:43
zicklag wrote:
I saw somebody add an instruction that defined a label and automatically added `.lo` and `.hi` labels using the defined label as a namespace, which seems like a great idea, but I'd like to be able to do that across the whole project for any label.
If you take a look at "How are the labels processed?" section of fasmg's basic guide (also available as fasmg.txt in the official package), you should find some helpful hints. A simple label interceptor could be enough to add the ".lo" and ".hi" symbols, for example:
Code:
struc ? definition&
     . definition
     .lo := $ and 0FFh
     .hi := ($ shr 8) and 0FFh
end struc     

my_label:
        db my_label.hi, my_label.lo    
The inline macro package gives another simple-to-use option:
Code:
include 'inline.inc'

inlinemacro lo(value)
        return = (value) and 0FFh
end inlinemacro

inlinemacro hi(value)
        return = ((value) shr 8) and 0FFh
end inlinemacro

my_label:
        db hi(my_label), lo(my_label)    
And if you would like to try making own custom syntax with line interceptors, I recommend CALM, since regular macros are clunky in such areas, and may also be slow in larger projects (depends on your use cases). You could also consider taking the modular preprocessor as a foundation to make things easier.
Post 22 May 2025, 03:43
View user's profile Send private message Visit poster's website Reply with quote
zicklag



Joined: 10 May 2025
Posts: 6
zicklag 22 May 2025, 16:28
Whoa! The inline macro is something I really wanted to do, I'll check out the different options you give here, thanks for the help!

I also discovered that I could add this macro to the top of the 6502.inc and it worked pretty well to add the #< or #> syntax for loading the high or low byte of an immediate:

Code:
macro ? line&
  match before #< label, line
    before #(label and $FF)
  else match before #< label after, line
    before #(label and $FF) after
  else match before #> label, line
    before #((label shr 8) and $FF)
  else match before #> label after, line
    before #((label shr 8) and $FF) after
  else
    line
  end match
end macro
    
Post 22 May 2025, 16:28
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.