satyria
Posts: 21
Joined: Sun May 12, 2013 5:41 pm

Use #define or EQU in assembler

Mon Aug 17, 2020 3:21 pm

Hello,
I want to make my assembler code more readable and wanted to use something like #define (EQU).
I use "Gnu-AS".
Example:

Code: Select all

#define BASE 0xFE000000
   ...
   ...
ldr r0,= BASE
   ...
   ...
Unfortunately, the linker shows an error when compiling:

Code: Select all

build/main.o: in function `wait2$':
(.init+0x8044): undefined reference to `BASE'
By the way: BASE is not used in the WAIT function.
What options are there with the Gnu-AS?

Greetings, Matthias

trejan
Posts: 2238
Joined: Tue Jul 02, 2019 2:28 pm

Re: Use #define or EQU in assembler

Mon Aug 17, 2020 3:25 pm

The equivalent to #define in gas is .set

You need to feed it through the cpp preprocessor first if you want to use #define. Either add that as part of your build script or give your source file the .S extension and use gcc on it.

dwelch67
Posts: 967
Joined: Sat May 26, 2012 5:32 pm

Re: Use #define or EQU in assembler

Mon Aug 17, 2020 3:30 pm

#define is C and not assembly language (gnu assembler), so you would want to use equ. Otherwise it is yet another assembly/hybrid language and you have to pass it through the C preprocessor before it goes to the assembler.

satyria
Posts: 21
Joined: Sun May 12, 2013 5:41 pm

Re: Use #define or EQU in assembler

Mon Aug 17, 2020 4:00 pm

many thanks

Unfortunately, equ did not work either. same error.

I've just read the GAS documentary. The attempt with "=" worked within the code:

Code: Select all

BASE = 0xFE200000
However, when I try to load this with .include, the same error occurs again.
in the "main.s" file:

Code: Select all

.include ../include/base.h
...
...
The base.h:

Code: Select all

BASE = 0xFE200000

satyria
Posts: 21
Joined: Sun May 12, 2013 5:41 pm

Re: Use #define or EQU in assembler

Thu Aug 20, 2020 6:50 am

Sorry, found my mistake. It works as described.
However, I have a new question: I would now like to set a value that I would like to process via .if query. Example:

Code: Select all

...
.equ RPI_Version, "1a"
...
...
.if RPI_Version == "1a" 
   ....
 .endif
 
However, this does not work because the assembler does not understand the "a" under .equ here. How can I avoid that?
How can I use strings for .if?
Greetings, Matthias

LdB
Posts: 1586
Joined: Wed Dec 07, 2016 2:29 pm

Re: Use #define or EQU in assembler

Thu Aug 20, 2020 9:16 am

Use just numbers :-)
The `#if' directive allows you to test the value of an arithmetic expression, rather than the mere existence of one macro. Its syntax is
#if expression
controlled text
#endif

#ifdef in C is for strings but I don't think the assembler can handle them but admittedly I would never do it so check you might get lucky.

satyria
Posts: 21
Joined: Sun May 12, 2013 5:41 pm

Re: Use #define or EQU in assembler

Thu Aug 20, 2020 9:24 am

I found a solution.
Example:

Code: Select all

...
.equ RPI_Version_1a, 1
...
...
.ifdef RPI_Version_1a
	...
.endif 
Greetings Matthias

Return to “Bare metal, Assembly language”