User avatar
mookiedog
Posts: 24
Joined: Sun Jun 17, 2012 3:04 pm
Location: Olympic Peninsula, WA

Including SDK files into assembly source

Wed Jul 06, 2022 12:29 am

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?

hippy
Posts: 12485
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Including SDK files into assembly source

Wed Jul 06, 2022 3:07 pm

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

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)
main.c

Code: Select all

void asmsub(void);

int main() {
  asmsub();
}
asm.s

Code: Select all

.include "hardware/regs/sio.h"

.globl  asmsub
asmsub: nop
        bx      lr
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.
Last edited by hippy on Wed Jul 06, 2022 3:44 pm, edited 2 times in total.

User avatar
mookiedog
Posts: 24
Joined: Sun Jun 17, 2012 3:04 pm
Location: Olympic Peninsula, WA

Re: Including SDK files into assembly source

Wed Jul 06, 2022 3:41 pm

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:
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.
I think this might be telling me that the C preprocessor is not being invoked on the assembly source files before they get assembled.

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

Re: Including SDK files into assembly source

Wed Jul 06, 2022 3:49 pm

You need to name it .S not .s if you want it to run it through the preprocessor first.

kilograham
Raspberry Pi Engineer & Forum Moderator
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

Wed Jul 06, 2022 3:50 pm

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)

hippy
Posts: 12485
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Including SDK files into assembly source

Wed Jul 06, 2022 3:55 pm

mookiedog wrote:
Wed Jul 06, 2022 3:41 pm
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?
Haha ...
included from assembly files (.S, so the C preprocessor can be used)
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 ...
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)'
At least it did the substitution, took it from the #define.

And beaten to it while I was figuring it out 8-)
Last edited by hippy on Wed Jul 06, 2022 3:57 pm, edited 1 time in total.

User avatar
mookiedog
Posts: 24
Joined: Sun Jun 17, 2012 3:04 pm
Location: Olympic Peninsula, WA

Re: Including SDK files into assembly source

Wed Jul 06, 2022 3:57 pm

Thanks to all! That was the problem. My file finally builds!

User avatar
Paeryn
Posts: 3499
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Including SDK files into assembly source

Wed Jul 06, 2022 4:00 pm

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!

hippy
Posts: 12485
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Including SDK files into assembly source

Wed Jul 06, 2022 4:07 pm

hippy wrote:
Wed Jul 06, 2022 3:55 pm
/home/pi/mypico/asm/asm.S:5: Error: garbage following instruction -- `ldr r0,=_u(31)'
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 ?

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

Re: Including SDK files into assembly source

Wed Jul 06, 2022 4:08 pm

hippy wrote:
Wed Jul 06, 2022 4:07 pm
hippy wrote:
Wed Jul 06, 2022 3:55 pm
/home/pi/mypico/asm/asm.S:5: Error: garbage following instruction -- `ldr r0,=_u(31)'
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 ?
The _u macro is part of hardware/platform_defs.h

hippy
Posts: 12485
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Including SDK files into assembly source

Wed Jul 06, 2022 4:16 pm

Thanks. That's sorted it. And I've learned something new.

Return to “SDK”