EASy68K
http://easy68k.com/EASy68Kforum/

Macro argument range checking using conditional assembly?
http://easy68k.com/EASy68Kforum/viewtopic.php?f=5&t=1525
Page 1 of 1

Author:  RetroGames [ Mon Dec 28, 2015 4:49 am ]
Post subject:  Macro argument range checking using conditional assembly?

I'm using EASy68K to write software for the Sega Genesis/Mega Drive. Even a modest program written for the Genesis platform entails a hefty amount of complexity, so I'm trying to think of ways to make my code more readable, manageable, and easier to debug. In the case of debugging, one idea I had was to implement bounds checking on arguments passed to macros. I was hoping I could use conditional assembly directives to do the checking, but I'm having a hard time figuring out how, if it's even possible at all. Here's a pseudocode example of what I'm trying to do:

Code:
TestMac MACRO

    ; move some immediate data \1 to a register \2, but only if \1 is in the range of #0-#23, and \2 is one of d0-d7

 IF \1 > #23, OR \1 < #0
    FAIL arg 1 is out of range - enter a value from #0-#23
    MEXIT
 ELSE
    IFNC \2 'd'?, OR \2 'D'?    ;  ? represents single character wild card
        FAIL arg 2 is invalid - enter a data register symbol from d0-d7
        MEXIT
    ENDC
 ENDIF

move.b \1, \2   ; args are in range if we've made it here

ENDM


     ORG $1000

StartLabel:

 TestMac #0, d4  ; this works, d4 = $xxxxxx00
 TestMac #24, d0   ; won't work, arg 1 is > #23
 TestMac #-1, d7   ; won't work, arg 1 is < #0
 TestMac #23, a0  ; won't work, arg 2 isn't a data register

 END StartLabel


After fiddling with this on my development PC, I get the sense that conditional assembly isn't suited to this task, but I'm still not sure. Does this approach to bounds checking for macro arguments make sense? If so, how would I make it work?

Author:  profkelly [ Mon Dec 28, 2015 1:18 pm ]
Post subject:  Re: Macro argument range checking using conditional assembly

Try this:

Code:
TestMac MACRO
    ; move some immediate data \1 to a register \2, but only
    ; if \1 is in the range of #0-#23 and \2 is one of d0-d7

    ; if \1 is in the range of #0-#23
    IFLT \1     ; if argument \1 is < 0
        FAIL arg 1 is out of range. Enter a value from #0-#23
        MEXIT
    ENDC
    IFGT \1-23  ; if argument \1 is > 23
        FAIL arg 1 is out of range - enter a value from #0-#23
        MEXIT
    ENDC
   
    ; if \2 is not one of D0-D7
    IFLT ('\2'-'D0')
        FAIL arg 2 is invalid. Enter a data register from D0-D7
        MEXIT
    ENDC

    IFGT ('\2'-'D7')
        FAIL arg 2 is invalid. Enter a data register from D0-D7
        MEXIT
    ENDC

    move.b #\1, \2   ; args are in range if we've made it here
    ENDM

    ORG $1000

StartLabel:

    TestMac  0,d4  ; this works, d4 = $xxxxxx00
    TestMac 24,d0  ; won't work, arg 1 is > #23
    TestMac -1,d7  ; won't work, arg 1 is < #0
    TestMac 23,a0  ; won't work, arg 2 isn't a data register

    SIMHALT             ; halt simulator

* Put variables and constants here

    END StartLabel


Author:  RetroGames [ Fri Jan 01, 2016 2:42 pm ]
Post subject:  Re: Macro argument range checking using conditional assembly

Thanks for your help, Professor. Your code did the trick! After experimenting with it, I think I understand how EASy68K's conditional assembly and macro processing work together. Thank you again for your gracious assistance, I never would have figured this out on my own!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/