English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 04 January 2024, 18:19   #1
hop
Registered User
 
Join Date: Apr 2019
Location: UK
Posts: 172
Redefining or undefining vasm macros

I'm trying to use vasm to assemble a disk image containing file binaries and associated file table, using a C preprocessor pattern. Here's a contrived example:

FileList.h:
Code:
// n.b. No include guards in this header, which is designed to be included multiple times in the same compilation unit.
#ifndef FILE_LIST_MACRO
#error FILE_LIST_MACRO is not defined
#endif

FILE_LIST_MACRO(FileA);
FILE_LIST_MACRO(FileB);
...

#undef FILE_LIST_MACRO  // allow macro to be redefined
main.cpp: note that FILE_LIST_MACRO can be redefined within the same file.
Code:
#define FILE_LIST_MACRO(T) static File* Get##T() {...}
#include "FileList.h"

#define FILE_LIST_MACRO(T) static int GetSize##T() {...}
#include "FileList.h"
I can almost translate this pattern to VASM to generate a disk image containing filetable and file binaries. However, I can't manage to re-define a macro within a file. Here's what I have had to use instead (simplified):

FileList.inc:
Code:
	FILE_MACRO	caves_.cms
        FILE_MACRO      city__.cms
FileList2.inc: (don't want this file!)
Code:
	FILE_MACRO2	caves_.cms
        FILE_MACRO2	city__.cms
DiskImage.asm:
Code:
	ORG 0
	INCLUDE "bootblock"
	ORG $400

FILE_TABLE_OFFSET	EQU	$50000
FILE_TABLE_SIZE_BYTES	EQU	$1800	; original format
	ORG	FILE_TABLE_OFFSET
	; Use vasm macros to generate file table

	MACRO	FILE_MACRO	; \1 = file name
	DC.B	'\1'		; filename
	DCB.B	16-\?1,0	; zero-pad to 16 bytes
	DC.L	\1 	 	; disk address
	DC.L	\1_SIZE_BYTES	; size in bytes
	ENDM
	INCLUDE "FileList.inc"

	ASSERT *-FILE_TABLE_OFFSET<=FILE_TABLE_SIZE_BYTES
	ORG	FILE_TABLE_OFFSET+FILE_TABLE_SIZE_BYTES

Files:
	MACRO	FILE_MACRO2 		; want to re-use FILE_MACRO here
\1:	INCBIN	"Files/\1"
\1_SIZE_BYTES:	EQU	*-\1
	ENDM
	INCLUDE "FileList2.inc" ; want to include FileList.inc here
If I try this:

DiskImage.asm:
Code:
	ORG 0
	INCLUDE "bootblock"
	ORG $400

FILE_TABLE_OFFSET	EQU	$50000
FILE_TABLE_SIZE_BYTES	EQU	$1800	; original format
	ORG	FILE_TABLE_OFFSET
	; Use vasm macros to generate file table

	MACRO	FILE_MACRO	; \1 = file name
	DC.B	'\1'		; filename
	DCB.B	16-\?1,0	; zero-pad to 16 bytes
	DC.L	\1 	 	; disk address
	DC.L	\1_SIZE_BYTES	; size in bytes
	ENDM
	INCLUDE "FileList.inc"

	ASSERT *-FILE_TABLE_OFFSET<=FILE_TABLE_SIZE_BYTES
	ORG	FILE_TABLE_OFFSET+FILE_TABLE_SIZE_BYTES

Files:
	MACRO	FILE_MACRO
\1:	INCBIN	"Files/\1"
\1_SIZE_BYTES:	EQU	*-\1
	ENDM
	INCLUDE "FileList.inc"
Then there are errors:

Code:
error 3007: undefined symbol <city__.cms_SIZE_BYTES>
error 3007: undefined symbol <city__.cms>
error 3007: undefined symbol <caves_.cms_SIZE_BYTES>
error 3007: undefined symbol <caves_.cms>
n.b. Assembling with -ldots option.

Is it possible to achieve this? Thanks for any advice.

Last edited by hop; 04 January 2024 at 18:28. Reason: Correction
hop is offline  
Old 05 January 2024, 02:09   #2
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,500
Quote:
Originally Posted by hop View Post
I can almost translate this pattern to VASM to generate a disk image containing filetable and file binaries. However, I can't manage to re-define a macro within a file.
To make this work you would have to undefine the macro first, as you did in your example for the C preprocessor.

Good news is that vasm can indeed undefine macros. But the mot-syntax module doesn't implement any directive to do that. Only madmac-syntax offers
macundef
for now.

And then there might also be a bug, which I should address soon: The second macro definition with the same name shouldn't be accepted without error message (but I have to check other assemblers first).

Before you ask: Yes, maybe I could implement a macro-undef directive for mot-syntax. But I have rarely seen one among assemblers following the Motorola syntax, and I would prefer to adopt a directive which is commonly used instead of inventing new ones.
phx is offline  
Old 05 January 2024, 11:09   #3
hop
Registered User
 
Join Date: Apr 2019
Location: UK
Posts: 172
Quote:
Originally Posted by phx View Post
To make this work you would have to undefine the macro first, as you did in your example for the C preprocessor.

Good news is that vasm can indeed undefine macros. But the mot-syntax module doesn't implement any directive to do that. Only madmac-syntax offers
macundef
for now.

And then there might also be a bug, which I should address soon: The second macro definition with the same name shouldn't be accepted without error message (but I have to check other assemblers first).
Thanks very much for explaining this. I was confused because it looked like it should work, but didn't. Adding an error message when a macro is redefined sounds like a good idea if it is straightforward.

Quote:
Originally Posted by phx View Post
Before you ask: Yes, maybe I could implement a macro-undef directive for mot-syntax. But I have rarely seen one among assemblers following the Motorola syntax, and I would prefer to adopt a directive which is commonly used instead of inventing new ones.
I don't think it is worth it. I was just seeing what could be done with the current feature set.

Using filenames as labels was a bad idea too! Filenames starting with numbers (5000pts.bal) didn't work with the macro and filenames containing hyphens (bonus--1.ald) were no good as labels. I tried renaming (invasive), and prefixing the generated labels with an underscore e.g. _\1 and hit further problems that I didn't understand. I couldn't see how to use the the unique id / global id / stack to connect the labels in the generated output of two seperate sets of macro invokations.

I've come to the conclusion that programatically generated assembly language in the build pipeline is the way to go. Far more flexible and straightforward.

Thanks again.

EDIT: Is it possible to view the generated vasm asm after macro expansion?

Last edited by hop; 05 January 2024 at 11:44.
hop is offline  
Old 05 January 2024, 16:45   #4
Wepl
Moderator
 
Wepl's Avatar
 
Join Date: Nov 2001
Location: Germany
Posts: 866
Quote:
Originally Posted by phx View Post
And then there might also be a bug, which I should address soon: The second macro definition with the same name shouldn't be accepted without error message (but I have to check other assemblers first).
Silently redefining macros is a ugly thing. I had this recently and spent some time to find out that this was the reason.
Should at least throw a warning.
Wepl is offline  
Old 05 January 2024, 17:09   #5
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,500
Quote:
Originally Posted by hop View Post
I was confused because it looked like it should work, but didn't.
Yes, by accident it will even work most of the time, as the new macro is seen before the old one in the hash table.

Quote:
Adding an error message when a macro is redefined sounds like a good idea if it is straightforward.
Abolutely! (Now I compared with some other assemblers.)

Quote:
I've come to the conclusion that programatically generated assembly language in the build pipeline is the way to go. Far more flexible and straightforward.
Always use the best tools for the job!

Quote:
Is it possible to view the generated vasm asm after macro expansion?
In the listing file? Here is my (working) example for macro redefinition:
Code:
abc     macro
        dc.l    \1
        endm

        abc     1

abc     macro
        dc.b    "\1",0
        endm

        abc     hej
The listing file:
Code:
Source: "tst.s"
                                     1: abc     macro
                                     2:         dc.l    \1
                                     3:         endm
                                     4:
                                     5:         abc     1
00:00000000 00000001                 1M         dc.l    1
                                     6:
                                     7: abc     macro
                                     8:         dc.b    "\1"
                                     9:         endm
                                    10:
                                    11:         abc     hej
00:00000004 68656A                   1M         dc.b    "hej"
                                    12:
The expanded macro lines got a capital 'M' attached to their line number.


Quote:
Originally Posted by Wepl View Post
Silently redefining macros is a ugly thing. I had this recently and spent some time to find out that this was the reason.
Agreed. Fixed it. You may try tomorrow's source snapshot.
phx is offline  
Old 07 January 2024, 17:20   #6
hop
Registered User
 
Join Date: Apr 2019
Location: UK
Posts: 172
Quote:
Originally Posted by phx View Post
Yes, by accident it will even work most of the time, as the new macro is seen before the old one in the hash table.

Here is my (working) example for macro redefinition:
Code:
abc     macro
        dc.l    \1
        endm

        abc     1

abc     macro
        dc.b    "\1",0
        endm

        abc     hej
Agreed. Fixed it. You may try tomorrow's source snapshot.
Many thanks for making the change to catching macro redefinition. I've tested on Windows with your example code and it seems to be working as expected.

vasm 1.9a:
Code:
vasm 1.9a (c) in 2002-2022 Volker Barthelmann
vasm M68k/CPU32/ColdFire cpu backend 2.5c (c) 2002-2022 Frank Wille
vasm motorola syntax module 3.16 (c) 2002-2022 Frank Wille
vasm binary output module 2.1 (c) 2002-2021 Volker Barthelmann and Frank Wille

CODE(acrx1):               8 bytes
vasm 2.0beta (daily source snapshot 06/01/2024):
Code:
vasm 2.0beta (c) in 2002-2024 Volker Barthelmann
vasm M68k/CPU32/ColdFire cpu backend 2.6c (c) 2002-2023 Frank Wille
vasm motorola syntax module 3.18 (c) 2002-2023 Frank Wille
vasm binary output module 2.3b (c) 2002-2023 Volker Barthelmann and Frank Wille

error 89 in line 7 of "test.asm": macro redefinition
>abc     macro

 *  The terminal process "C:\Amiga\Crack\VenusTheFlytrap\BuildTest.bat" terminated with exit code: 1.

Quote:
Originally Posted by phx View Post
In the listing file? ... The expanded macro lines got a capital 'M' attached to their line number.
Of course. Can't belive I didn't think to look. Thanks.
hop is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about XREF vs XDEF (vasm 1.8 vs vasm 1.9) roondar Coders. Asm / Hardware 8 01 May 2023 20:59
vasm - Use defined object code in macros/expressions? dansalvato Coders. Asm / Hardware 2 24 March 2022 01:38
vasm/mot: macros sparhawk Coders. Asm / Hardware 8 06 January 2020 18:32
VASM macros/equates for registers jotd Coders. Asm / Hardware 5 06 September 2019 18:47
Macros for debugging in VASM mcgeezer Coders. Asm / Hardware 11 14 May 2019 15:27

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 15:03.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.10804 seconds with 15 queries