Skip to main content Skip to local navigation

Exploring RISC-V Options: the RP2350 (Part 8 -- Returning a Single Array Value)

Here, we'll return the value from an array defined in the assembler file to the calling C function. When writing programs like this it's handy to refer to a "cheat sheet" like this one.

If everything works as it should your serial monitor should have the following output:

Serial monitor stating: "Assembler returned: 0x20.  0x20 is the 2nd value in the array defined in the assembler file.
Serial monitor stating: "Assembler returned: 0x20. 0x20 is the 2nd value in the array defined in the assembler file.

There are two files here: returnArrayValue.S and riscv_C_asm_BasicArray.ino.

The C file: riscv_C_asm_BasicArray

// riscv_C_asm_BasicArray.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
}

The ASM file: returnArrayValue.S

# returnArrayValue.S
# =========== DATA: data section ===============
.data
theArray: .word 0x0010, 0x0020, 0x0030, 0x0040, 0x0050

# =========== TEXT: Code section ===============
.text
.global return4bytes

# Function returns 0xABCD
# No inputs.  Just one output: a value from the array.
# Here, we'll offset by 4 bytes from start of array to get 
# the 2nd 32-bit value in the array (0x0020)
return4bytes:
    la t1, theArray     # load address of theArray into t1
    lw a0, 4(t1)        # load contents of 2nd location in array into a0
    ret                 # return to caller with value stored in a0

a pen

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.