Page 2 of 2

How to implement 16-bit operations in assembly language

Posted: Thu Dec 31, 2020 9:52 pm
by StephenHorn


On 12/8/2020 at 1:22 PM, Johan Kårlin said:




Thanks for your answers! 



@StephenHorn I took a look at math.inc. It is clearly a better way of doing it compared to what I am doing now. I guess I have reached the limit for what ACME can do. Both for this and other macro implementations it would really be a relief to not have to write multiple macros just to cope with the fact that parameters can be passed by value or reference. After my current project I will probably start developing with ca65 instead. By the way - why not finish math.inc and upload it as a development tool : )?



Bump.

There is a drawback, though, to trying to use smart macros. So I have tried to go ahead and expand on the smart macros for my math.inc, and things mostly worked... mostly. I'm having some issues with certain cases being mis-identified, resulting in bugs. For instance, an easy way to foil the smart macros seems to be anything in the form of this:


Quote




.repeat 3, i

   MY_MACRO #(arg+i)

.endrep



This should be an immediate (prefixed with "#"), but the assembler macro logic to detect that fails.

I'm not sure if this is a bug with CA65, but more generally, you can force syntax errors with statements like this:


Quote




.out .string(#1)



...which I would expect would cause the assembler to output "#1".

So. Smart macros. A clever idea, but if there are bugs like this then maybe I'll end up leaving them be while *also* implementing dumber, more explicit versions.

(This is the second assembler with which I've managed to hoist myself on my own petard, trying to make a smart macro library. At least this time I'm able to diagnose where the problem lies.)


How to implement 16-bit operations in assembly language

Posted: Fri Jan 01, 2021 9:08 am
by JimmyDansbo


On 12/8/2020 at 8:22 PM, Johan Kårlin said:




I guess I have reached the limit for what ACME can do. Both for this and other macro implementations it would really be a relief to not have to write multiple macros just to cope with the fact that parameters can be passed by value or reference.



What I have done in the past is add an optional parameter to my ACME macros to indicate if the variable is an absolute or an indirect, like this.


Quote




!macro LOADVAL .val {



lda .val  ; load absolute value into .A register



}



!macro LOADVAL .val, .immed {



lda #.val  ; load immediate value into .A register



}



+LOADVAL $10  ; Load value stored at address $0010 into .A register



+LOADVAL $10,1 ; Load the value #$10 into the .A register



You still have to write separate macro's to handle the different scenarios, but as a macro with an optional parameter can use the macro without the optional parameter, you can just "add-on" the needed code to handle the optional parameters.

For a few more examples of macro's with optional parameters, have a look at the VERA_SET_ADDR macro in my VERA include file:

https://github.com/JimmyDansbo/cx16stuff/blob/master/vera0.9.inc

Another way of doing it could be:


Quote




!macro LOADVAL .val, .immed {



if .immed = 0 {



    lda .val  ; Load absolute value into .A register



} else {



    lda #.val  ; Load immediate value into .A register



}



}



+LOADVAL $10, 0  ; Load value stored at address $0010 into .A register



+LOADVAL $10, 1  ; Load the value #$10 into the .A register




How to implement 16-bit operations in assembly language

Posted: Fri Jan 01, 2021 4:54 pm
by Johan Kårlin
Interesting, not least to see your VERA macros! But it still feels a bit awkward. I can end up having a numbers of flags for macros with several variables.

How to implement 16-bit operations in assembly language

Posted: Fri Jan 01, 2021 4:57 pm
by JimmyDansbo


1 minute ago, Johan Kårlin said:




But it still feels a bit awkward.



Yeah, hopefully the macro's will eventually mature a bit more in the Acme assembler.