I am struggling to write my first Pico project that contains assembly language source which is trying to access SDK header files. Basically, I can't get the assembler to either find the paths to the files in the SDK. or perform the substitutions defined inside the .h
The following:
.include "hardware/regs/sio.h"
generates "Error: can't open hardware/regs/sio.h for reading: No such file or directory"
If I change that to:
#include "hardware/regs/sio.h"
the assembler apparently finds the file, because the "can't open..." error above goes away. The weird part is that the #define statements in the apparently-included .h file don't do anything: no substitutions occur in my source text. Predictably, bad things happen: the assembler assumes that all my references to register offsets are actually external symbol references which causes quite a few errors.
I seem to be missing a subtlety here. Or more likely, maybe it is something totally obvious. Any ideas?
Re: Including SDK files into assembly source
Are you sure the "#" isn't simply commenting the line out, hence no error, no inclusion, no definitions for what you need to define. If so I would expect similar issues when it comes to '#define' even if you can include the right file.
I'm no assembler expert but this worked for me ...
CMakeLists.txt
main.c
asm.s
Change that to include "xxx.h" and I get "Not found". Put a # in front and I don't so guess my comment notion is sound. And it doesn't say "not found" as it is so guess it's finding it.
I'm no assembler expert but this worked for me ...
CMakeLists.txt
Code: Select all
set(PROJECT asm_test)
cmake_minimum_required(VERSION 3.12)
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
project(${PROJECT} C CXX ASM)
pico_sdk_init()
add_executable(${PROJECT} main.c asm.s)
target_link_libraries(${PROJECT} pico_stdlib)
pico_add_extra_outputs(${PROJECT})
pico_enable_stdio_usb(${PROJECT} 0)
pico_enable_stdio_uart(${PROJECT} 0)
Code: Select all
void asmsub(void);
int main() {
asmsub();
}
Code: Select all
.include "hardware/regs/sio.h"
.globl asmsub
asmsub: nop
bx lr
Last edited by hippy on Wed Jul 06, 2022 3:44 pm, edited 2 times in total.
Re: Including SDK files into assembly source
Well, well. Right you are: apparently, the '#' char starts a comment in the Gnu assembler. But that begs the next question: how would I expect an assembly language file to effectively access to the contents of an include file full of register addresses and fields that are all created using #define? The SDK documentation is clear that the hardware regs files are meant to be includable in C or assembly source:
I think this might be telling me that the C preprocessor is not being invoked on the assembly source files before they get assembled.The headers in hardware_regs contain only comments and #define statements. This means they can be included from
assembly files (.S, so the C preprocessor can be used), as well as C and C++ files.
Re: Including SDK files into assembly source
You need to name it .S not .s if you want it to run it through the preprocessor first.
-
- Raspberry Pi Engineer & Forum Moderator
- Posts: 1279
- Joined: Fri Apr 12, 2019 11:00 am
- Location: austin tx
Re: Including SDK files into assembly source
if your file is asm.s it does not get processed by the pre-processor; if it is asm.S it does (this is a GCC/other compiler thing)
Re: Including SDK files into assembly source
Haha ...
That's dot then uppercase "S". Change to 'asm.S' then '#include' seems to work but there seems to be more to it than that ...included from assembly files (.S, so the C preprocessor can be used)
asm.S
Code: Select all
#include "hardware/regs/sio.h"
.globl asmsub
asmsub: nop
ldr r0, =SIO_CPUID_MSB
bx lr
Code: Select all
/home/pi/mypico/asm/asm.S:5: Error: garbage following instruction -- `ldr r0,=_u(31)'
And beaten to it while I was figuring it out

Last edited by hippy on Wed Jul 06, 2022 3:57 pm, edited 1 time in total.
Re: Including SDK files into assembly source
Thanks to all! That was the problem. My file finally builds!
Re: Including SDK files into assembly source
If you want asm files to be processed by CPP (C Pre Processor) first then they need to be named xxx.S rather than xxx.s (of course this only applies when using gcc to assemble the files, directly calling as or gas won't use CPP). (Pre post edit, others answered whislt I was typing).
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!
Re: Including SDK files into assembly source
Adding a "#define _u(n) n' seems to have fixed 'mov r0,#SIO_CPUID_MSB' and 'ldr r0,=SIO_CPUID_MSB'
But how should that be done properly, without me having to supply #defines ?
Re: Including SDK files into assembly source
Re: Including SDK files into assembly source
Thanks. That's sorted it. And I've learned something new.