How to implement 16-bit operations in assembly language

Chat about anything CX16 related that doesn't fit elsewhere
User avatar
StephenHorn
Posts: 565
Joined: Tue Apr 28, 2020 12:00 am
Contact:

How to implement 16-bit operations in assembly language

Post 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.)

Developer for Box16, the other X16 emulator. (Box16 on GitHub)
I also accept pull requests for x16emu, the official X16 emulator. (x16-emulator on GitHub)
User avatar
JimmyDansbo
Posts: 476
Joined: Sun Apr 26, 2020 8:10 pm
Location: Denmark
Contact:

How to implement 16-bit operations in assembly language

Post 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



Visit my Github repo
or my personal site with CX16/C64/6502 related information.
Feel free to contact me regarding any of my projects or even about meeting up somewhere near Denmark
Johan Kårlin
Posts: 292
Joined: Wed Jun 03, 2020 11:33 am
Location: Kalmar, Sweden

How to implement 16-bit operations in assembly language

Post 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.
User avatar
JimmyDansbo
Posts: 476
Joined: Sun Apr 26, 2020 8:10 pm
Location: Denmark
Contact:

How to implement 16-bit operations in assembly language

Post 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.

Visit my Github repo
or my personal site with CX16/C64/6502 related information.
Feel free to contact me regarding any of my projects or even about meeting up somewhere near Denmark
Post Reply