Calling a function in an assembler routine

When calling the function "shiftOutByte" outside of an assembler block it is recognized correctly + compiles but when called from inside an assembler block it is not recognized + I get an error message:

In function nrf24_writeRegs': ..Arduino\libraries\nRF24L01_t13-main/nRF24L01.c:369: undefined reference to shiftOutByte'

Eg this code works perfectly

uint8_t nrf24_readReg(uint8_t cmd) {
	NRF24L01_CSN_SET();		//
	NRF24L01_CSN_CLR();
	cmd = shiftOutByte(cmd);
	NRF24L01_CSN_SET();	
	NRF24L01_CSN_CLR();
	return cmd;
}

but this code fails + gives the error message

void nrf24_writeReg(uint8_t cmd, uint8_t value) {
	NRF24L01_CSN_SET();
	NRF24L01_CSN_CLR();		
	asm volatile (
		"rcall shiftOutByte			\n\t"
		"mov	r24, r22			\n\t"
		"rcall shiftOutByte			\n\t"
		:: 
	);
	NRF24L01_CSN_SET();
	NRF24L01_CSN_CLR();
}

Any help to solve this is welcome

Where is the shiftOutByte function located? You are using an RCALL instruction, which is a relative call, and implies a short distance. Try using CALL instead. But, without further knowledge of your code, it’s hard to say.

Furthermore, the C Compiler always passes function arguments and returns values in specific register locations. Knowing these locations are essential to writing efficient inline assembly code, especially when interfacing with the C language. An 8-bit, byte-sized value is passed to and from a function via the r24 register. What are you doing with r22?

Find included a link to the library (not made by myself) that I'm trying to use.
I'm using arduino ide to program an ATTINY

The function is defined in the nRF24L01.c file

[GitHub - dalxo/nRF24L01_t13: OSI Layer 2 driver for nRF24L01+ on ATtiny13A in ultra-low-power wireless applications]

This function is on purpose not defined in the nRF24L01.h header file, as it is not general usable but only in a limited context.
Anyway I've tried to add it to the header file and also that is not working.
I've also tried to add an "extern" statement to it and also that is not working.

The simple sketch below is reproducing the problem:

#define F_CPU 1200000UL //ONLY used if fuse was set to lower internal frequency
#include <avr/io.h>
#include <util/delay.h>
#include "projdefs.h"
#include <nRF24L01.h>

void setup() {
}

void loop() {
}

The function is multiple times. When called inside an assembler block it is not found.
When called outside an assembler block it is found.
Any suggestions how to change the library code?

Rewrite the function in C/C++?

This library uses assembler to reduce the footprint of the code, and that is exactly why I'm using it. It's intended for an ATTINY13 with 1K of ram only.
Converting to C will likely blow up that footprint

You might be surprised.

presumably shiftOutByte() is defined in C++ code (.cpp or .ino file), and is therefore "name mangled" into a symbol that reflects the full function signature (including argument types.) Try prototyping it like:

extern "C" void shiftOutByte(byte b);

See also Standard C++

casting does not solve it
also using

extern "C" {
  #include "projdefs.h"
  #include <nRF24L01.h>
}

does not solve it

The ATtiny13 does no have "call", everything is reachable via "rcall."

The nRF24 library being used is C, not C++, so my comments about "extern "C"" are irrelevant.

Sigh. Could you please provide a full compilable program (and compile instructions, including which core you're using?

(Never mind. I put one together myself.)

Turn off "link time optimization" - it's a board option if you're using ATTinyCore.
With LTO enabled, the compile step produces some sort of weird lightly-processed code, and that apparently doesn't include the ability to resolve symbols within inline assembly.

The clue was looking at the individual .o files:

> avr-nm *.o   
nRF24L01.c.o:
avr-nm: nRF24L01.c.o: plugin needed to handle lto object
  :

(turning off LTO may "use up" more space than you're saving by having inline asm there in the first place... YMMV.)

I'm using latest arduino IDE version 1.8.19 (and the same problem is seen on 1.8.13)
Settings are


With or without LTO active it keeps failing

sketch\ATTINY_nrf24L01_Sensor.ino.cpp.o: In function nrf24_writeReg(unsigned char, unsigned char)': C:\Users\Nout\Documents\Arduino\ATTINY_nrf24L01_Sensor/ATTINY_nrf24L01_Sensor.ino:470: undefined reference to shiftOutByte'
C:\Users\Nout\Documents\Arduino\ATTINY_nrf24L01_Sensor/ATTINY_nrf24L01_Sensor.ino:470: undefined reference to shiftOutByte' sketch\ATTINY_nrf24L01_Sensor.ino.cpp.o: In function nrf24_writeRegs(unsigned char, unsigned char const*, unsigned char)':
C:\Users\Nout\Documents\Arduino\ATTINY_nrf24L01_Sensor/ATTINY_nrf24L01_Sensor.ino:470: undefined reference to shiftOutByte' C:\Users\Nout\Documents\Arduino\ATTINY_nrf24L01_Sensor/ATTINY_nrf24L01_Sensor.ino:470: **undefined reference to shiftOutByte'**
collect2.exe: error: ld returned 1 exit status
exit status 1

Your picture shows LTO still enabled, BTW...

the following compiled fine (also for tiny85, since I don't have a tiny13 board installed.)

#include "nRF24l01.h"
 
void nrf24_writeReg(uint8_t cmd, uint8_t value) {
  asm volatile (
    "rcall shiftOutByte     \n\t"
    "mov  r24, r22      \n\t"
    "rcall shiftOutByte     \n\t"
    :: 
  );
}

void setup() { }

void loop() {
  nrf24_writeReg(12, 34);
}

(Edit: Using DrAzzy's ATtinyCore. I haven't heard of "DIY Attiny" - include a link to that as well.)

(Edit2: I found DIY ATtiny (here: https://raw.githubusercontent.com/sleemanj/optiboot/master/dists/package_gogo_diy_attiny_index.json ), and it still compiles OK for me. Even for the ATtiny13.)

I was able to discussed the topic with the guy who made the library and he solved it. His new release will include the updates to make it run for Arduino as well., but only when LTO is disabled.

This topic can be closed

Do you have a pointer to "how"?

It was done by elaborating further on your suggestion and apply it on a few more places.

I suggest you view it in the library rather then copying all changes out of context.
It remains a pitty that LTO cannot be enabled. Solving that in Arduino idee would stille save on valuable memory size.