Jump to content
Sign in to follow this  
snicklin

MADS: Type Based Macros

Recommended Posts

Hi All,

 

I am looking to see if it is possible to do something with MADS (any version).

 

What I would like to be able to do is to have a macro called 'ADD' for example. This could then either accept bytes or words and then give an appropriate output.

 

For example (in pseudo code of course), I would like to achieve something like this....

 

ADD(123, 580) ; Byte and a word added together to give a word // Note that these are values, I am not looking at memory locations...

 

I then appreciate that this could have just as easily been either of these derivatives:

ADD(580, 123)

ADD(580,580)

ADD(1,2)

 

So I would need a macro something along these lines:

 

.macro ADD(value1, value2)

#if type(:1)=.byte and type(:2)=.byte

    ADDBB(:1,:2)

#end if

 

#if type(:1)=.byte and type(:2)=.word

    ADDBW(:1,:2)

#end if

 

#if type(:1)=.word and type(:2)=.byte

    ADDWB(:1,:2)

#end if

 

#if type(:1)=.word and type(:2)=.word

    ADDWW(:1,:2)

#end if

 

.endm

 

This way I could build up a vastly more powerful library of tools which are easier to use than having to remember whether to use ADDBB, ADDBW and other derivatives.

 

Is this possible??

 

 

Share this post


Link to post
Share on other sites

MADS instruction - MACRO COMMANDS

http://mads.atari8.info/mads_eng.html


ADB, SBB
These macro commands add or subtract a value from a byte location in memory and store it either back or to another location.

  ADB SRC #$40 -> LDA SRC       ADB A B C  -> LDA A
               -> CLC                      -> CLC
               -> ADC #$40                 -> ADC B
               -> STA SRC                  -> STA C

  SBB SRC #$80 -> LDA SRC       SBB A B C  -> LDA A
               -> SEC                      -> SEC
               -> SBC #$80                 -> SBC B
               -> STA SRC                  -> STA C


ADW, SBW
These macro commands add or subtract a value from a word location in memory and store it either back or to another location.

  ADW SRC #$40 -> CLC             ADW A B C  -> CLC
               -> LDA SRC                    -> LDA A
               -> ADC #$40                   -> ADC B
               -> STA SRC                    -> STA C
               -> SCC                        -> LDA A+1
               -> INC SRC+1                  -> ADC B+1
                                             -> STA C+1

  ADW SRC #$40 SRC -> CLC
                   -> LDA SRC
                   -> ADC #$40
                   -> STA SRC
                   -> LDA SRC+1
                   -> ADC #$00
                   -> STA SRC+1
                                             
  SBW SRC #$4080 -> SEC           SBW A B C  -> SEC
                 -> LDA SRC                  -> LDA A
                 -> SBC <$4080               -> SBC B
                 -> STA SRC                  -> STA C
                 -> LDA SRC+1                -> LDA A+1
                 -> SBC >$4080               -> SBC B+1
                 -> STA SRC+1                -> STA C+1 

Edited by tebe

Share this post


Link to post
Share on other sites

Mads doesn't have types for variables. It doesn't even have variables. Just labels.

You can tell the type for constant, that's simple. If the number is less than 256, it's a byte. If it's more than 255, but less than 65536, it's a word, and so on.

But for variables, you're out of luck.

As tebe mentions, Mads has similar built-in macros, and it will correctly expand these variants:

 

adw var #40

adw var #4040

 

But if you want to add 8 bit zero page variable to 16 bit zero page variable, there is no way to do it.

 

Share this post


Link to post
Share on other sites

If the macros provided by mads are not enough for your case you can do your own.

But you need to decide first where you want to put the results (A, X or Y, or some memory addresses).

Also if you want to support negative numbers and how you handle the cases where there is overflow (adding two bytes could give you a word result, for example).

 

Anyway the syntax would be something like:

 

.macro Add

.if [:1 < 256] .and [:2 < 256]
// code to add two bytes..

.elseif [:1 < 256] .and [:2 > 255]
// code to add one byte and a word..

.elseif [:1 > 255] .and [:2 < 256]
// code to add one word and a byte..

.else
// code to add two words..

.endif

.endm

 

 

Share this post


Link to post
Share on other sites

Also check out the syntax with .macro " " .. it's in MADS manual from phareon. It allows you to use both direct and indirect modes, ie. adding #10 or 10 as an address, the same way adw and similar build-in macros do.

 

Share this post


Link to post
Share on other sites

Hey, thank you all for your replies.

 

I am looking to use Roger/NRV's solution to be able to add things together. I am not going to cater for negative numbers at the moment, so the method used there will be good.

 

As for Tebe's kind suggestion of using the macro commands, I will use them in conjunction with Roger/NRV's method. I have been using them already, but I would like to have a very generic 'ADD' which can take anything.

 

I'll give you a story about why I am looking to do this. I was considering writing a language which would suit what I am aiming to produce: A football/soccer management game. I would like the language to be quite simple to use, use an AtariMax cartridge so that I can have LOTS of functions/methods pre-built to be re-used across projects (and I really mean a lot, I could devote 100's of kilobytes to reusable code). Code speed isn't overly important, but speed of development of the final projects is.

 

All in all, there is a lot of work to be done. I had the thought that I would write it as a pre-processing step before MADS does the compile. Then I thought to myself, "I need to do as much with MADS as I possibly can", using it's directives and macros to the fullest extent. That would mean that I could write something that looks like another language, without it actually being another language. That way I wouldn't have to write a compiler. So the first thing I wanted to achieve is some generic ADD routine.

 

Thanks for your time.

Share this post


Link to post
Share on other sites

Oh, didn't I promise you demo of my brand new speech synth ? Sadly since all parties is on halt, there is always something more important. So I leave it at 'soon' for now.

Share this post


Link to post
Share on other sites
On 5/31/2020 at 6:02 AM, R0ger said:

Oh, didn't I promise you demo of my brand new speech synth ? Sadly since all parties is on halt, there is always something more important. So I leave it at 'soon' for now.

I can't remember if you did or didn't, but feel free to show me when it is ready. It will be interesting to see.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...