[Z88dk-users] Macros in asm?

Bridge to the z88dk-users mailing list
Post Reply
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

[Z88dk-users] Macros in asm?

Post by obiwanjacobi »

I've tried to search for it.. :rolleyes:

Anyway: is it possible to use some kind of macros in asm files?
I want to 'inline' some asm routines in the bios I am writing for my zalt target/machine...



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

I've tried to search for it.. :rolleyes:

Anyway: is it possible to use some kind of macros in asm files?
I want to 'inline' some asm routines in the bios I am writing for my zalt target/machine...
z80asm itself does not do macros yet but we've adopted m4 as a macro pre-processor. This is the standard macro processor in linux (the windows build also includes a binary) - it takes getting used to but it's very powerful. Links to the manual and a quick overview can be found here: https://www.z88dk.org/wiki/doku.php?id=temp:front#m4

zcc will invoke m4 on any file ending in .m4. The intention is to use it with file extensions like these:

.h.m4 -- zcc expands to .h file
.c.m4 -- zcc expands to .c file
.asm.m4 -- zcc expands to .asm file

The expansion means zcc writes the resulting file type into the original source directory immediately. So if you have a "foo.asm.m4" file, zcc will create the file "foo.asm" after m4 expansion in the same source directory alongside "foo.asm.m4". zcc will then process the resulting .c or .asm file as normal. The reason why these files have to be written to the source directory is so that include paths are properly resolved. This also means any file ending in ".ext.m4" also reserves the filename ending in ".ext" in the source directory as zcc will overwrite any file with that name during macro expansion.

Very recently (ie last couple of days) I added a few standard z88dk m4 macros to the file "z88dk.m4" in m4's path. If you include that in your m4 file like so:

include(`z88dk.m4')

you can use those macros. You can see the macro definitions online: http://z88dk.cvs.sourceforge.net/viewvc ... 4/z88dk.m4
This will give you a for loop and a foreach loop. It's just a beginning and I expect we will put more useful macros in as time passes.

Here's a short example using m4 as macro processor for an asm file:

test.asm.m4
"
include(`../../../../clib_target_cfg.asm')
include(`z88dk.m4')

SECTION NIRVANAP

PUBLIC __NIRVANAP_bitmaps

__NIRVANAP_bitmaps:

; lookup table with screen addresses

defs 16

z88dk_for(ROWREPT, 0, eval(NIRVANAP_TOTAL_ROWS - 1),
`define(`HROW', eval((ROWREPT+1)/8))define(`LROW', eval((ROWREPT+1)%8))
defw 16384 + (HROW*2048) + (LROW*32)
defw 16384 + (HROW*2048) + 512 + (LROW*32)
defw 16384 + (HROW*2048) + 1024 + (LROW*32)
defw 16384 + (HROW*2048) + 1536 + (LROW*32)')

defs 4*(23 - NIRVANAP_TOTAL_ROWS)
"

The example above uses a for loop (z88dk_for) which is equivalent to:

for(ROWREPT = 0; ROWREPT <= NIRVANAP_TOTAL_ROWS - 1; ROWREPT++)
{
HROW = (ROWREPT+1)/8
LROW = (ROWREPT+1)%8
defw 16384 + (HROW*2048) + (LROW*32)
defw 16384 + (HROW*2048) + 512 + (LROW*32)
defw 16384 + (HROW*2048) + 1024 + (LROW*32)
defw 16384 + (HROW*2048) + 1536 + (LROW*32)
}

m4 looks bizarre to the uninitiated (and it still does to me since I'm only a beginner) but it's hidden everywhere in the linux world so knowing it can be a useful skill.


I can compile the above using zcc:

zcc +zalt .... test.asm.m4 -o test -create-app

Or I can stop zcc after m4 expansion to examine the resulting asm file:

zcc +zalt -m4 .... test.asm.m4



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Post by obiwanjacobi »

I was afraid you were going to say that ... :|
[..] it takes getting used to
and then some...

I think I'd rather use an editor with code snippets then.



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

I was afraid you were going to say that ... :|
[..] it takes getting used to
and then some...

I think I'd rather use an editor with code snippets then.
You can still use it for simple macro expansions without have to know too much.

define(`mymacro', `
ld b,$1
djnz ASMPC
')

Then:

mymacro(100)

expands to:

ld b,100
djnz ASMPC

The "$n" expands argument number "n" in place. The quoting is done with opening back tick and closing apostrophe. The argument to the define mymacro is in a quoted section so it is reproduced as a unit inline. Whitespace is carefully copied to output so that's why the formatting inside the quote is carefully done. After the opening backtick and before the "ld b,$1" there is a \n so there will be an initial blank line as part of the macro. If you don't want that you can put in a "dnl" which means do-not-list to not echo following characters up to and including the \n.



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Post by obiwanjacobi »

I was afraid you were going to say that ... :|
[..] it takes getting used to
and then some...

I think I'd rather use an editor with code snippets then.
You can still use it for simple macro expansions without have to know too much.

define(`mymacro', `
ld b,$1
djnz ASMPC
')

Then:

mymacro(100)

expands to:

ld b,100
djnz ASMPC

The "$n" expands argument number "n" in place. The quoting is done with opening back tick and closing apostrophe. The argument to the define mymacro is in a quoted section so it is reproduced as a unit inline. Whitespace is carefully copied to output so that's why the formatting inside the quote is carefully done. After the opening backtick and before the "ld b,$1" there is a \n so there will be an initial blank line as part of the macro. If you don't want that you can put in a "dnl" which means do-not-list to not echo following characters up to and including the \n.
Yes, I read some of the docs and indeed this simple case is still fairly readable and will probably cover 80% of the cases. Problem with snippets is that you cannot change used instances after the fact - with macro's you can, which is a big bonus.

Thanx.



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
Post Reply