Here I want to post an example of an assembler function that simple sends a value back to the C function that called it. The concept and approach are pretty general, but it is specifically aimed, via the assembler code for running on the RP2350 RISC-V system. Small changes would be needed for a PIC18, ARM Cortex or other processor.
If all works as intended it should produce the following on the Serial Monitor sub-window in the Arduino IDE:

There are two files for you to create in the Arduino IDE: riscv_C_asm_return_value.ino and return4bytes.S.
Main C code (riscv_C_asm_return_value.ino)
// riscv_C_asm_return_value.ino
// james andrew smith; Dec 11, 2025
extern "C" uint32_t return4bytes(void); // prototype of the assembler routine
void setup() {
/* Establish a serial connection @ 9600 Baud*/
Serial.begin(9600);
while (!Serial); // wait for Serial to be ready
/* Call the assembler routine and accept the output. */
uint32_t result = return4bytes(); // call the assembler routine
/* Print out the returned value as a HEX value (4 bytes)*/
Serial.print("Assembler returned: 0x");
Serial.println(result, HEX);
}
void loop() {
// nothing here
}
next up is the Assembler code...
Assembler function (return4bytes.S)
# =========== TEXT: Code section ===============
.text
.global return4bytes
# Function returns 0xABCD
# No inputs. Just one output: 0xABCD
return4bytes:
li a0, 0xABCD # load immediate 0xABCD into return register a0
ret # return to caller

James Andrew Smith is a Professional Engineer and Associate Professor in the Electrical Engineering and Computer Science Department of York University’s Lassonde School, with degrees in Electrical and Mechanical Engineering from the University of Alberta and McGill University. Previously a program director in biomedical engineering, his research background spans robotics, locomotion, human birth, music and engineering education. While on sabbatical in 2018-19 with his wife and kids he lived in Strasbourg, France and he taught at the INSA Strasbourg and Hochschule Karlsruhe and wrote about his personal and professional perspectives. James is a proponent of using social media to advocate for justice, equity, diversity and inclusion as well as evidence-based applications of research in the public sphere. You can find him on Twitter. You can find him on BlueSky. Originally from Québec City, he now lives in Toronto, Canada.
