EdouardBERGE / rasm

RASM powerful Z80 assembler
129 stars 16 forks source link

How to check whether the value passed to a macro is a register? #26

Closed anrikun closed 2 years ago

anrikun commented 2 years ago

I'm trying to write a macro that would work no matter passed value is a byte or a register. I've tried this:

macro my_macro value
  switch {value}
    case 'a'
    case 'd'
    case 'e'
    case 'h'
    case 'l'
      ; Do something with the register.
      break
    default
      ; Do something with the byte.
  endswitch
endm

But when I do my_macro a, I get "cannot use register A in this context". Is what I'm trying to achieve supported? Is there a way to check whether a passed value is a register?

EdouardBERGE commented 2 years ago

hey! the error is on the SWITCH [register.asm:16] inside macro => [register.asm:2] cannot use register A in this context

the macro expansion will do

"switch a"

which is not 'a' ! Then the expression parser trigger that A is a reserved word so it cannot be a variable, a label or an alias

in you case, use directly (it's an example) without switch/case

LD A,{value}

anrikun commented 2 years ago

Precisely I wanted to avoid an extra LD A,{value} when not needed.

Basically I wanted to do: If {value} is a byte, writeLD A,{value} : OUT (C),A But if {value} is a register, write OUT (C),{value} directly

I guess I'll write two separate macros then.

EdouardBERGE commented 2 years ago

doing what you want could be done by adding a new function "is_register", so the code could be like this

switch is_register('{value}')
case 0 : ld a,{value} : out (c),a : break
case 1 : out (c),{value}
endswitch
anrikun commented 2 years ago

Can I add such a function myself or does it have to be built in RASM itself?

EdouardBERGE commented 2 years ago

i will add the function, it must be hardcoded ;)

anrikun commented 2 years ago

Great! Thanks a lot! :smile:

EdouardBERGE commented 2 years ago

i did the math function but i'm stuck (for now) with the one letter case...

print is_register('Bc') ; no confusion, this is a string
print is_register('f') ; confusion for expression parser, this could be a one char string or a value => but i choose value 1st...

mindblowing ;)

anrikun commented 2 years ago

Take your time, Edouard and good luck.

EdouardBERGE commented 2 years ago

fixed with last commit https://github.com/EdouardBERGE/rasm/commit/221212fb859b9f651d4d6a8a11200df963801894