I am writing code for a RISCV microcontroller. I am searching for a trick to get the program counter (PC) of an extended inline asm block, from a file different from the inline asm itself. Note: this asm block is not the initial code in a function.
The reason is that the hardware will trigger some side effects when (or if) the execution reaches that specific PC.
A pseudo-C solution can be the following (the code won't work ofc).
File_A.c
//some_code
chat routine_name[10] = 'routine';
uint32_t routine_start_PC = get_start_PC(routine_name);
// some_other_code
File_B.c
void foo_rou(){
//some_code_BEFORE (IMPORTANT!)
routine:
asm volatile(
...
);
}
Does anyone have a suggestion?
This is my suggestion on godbolt:
It uses the separate function
print_ptr()to identify the actual addresses.The assembly listing shows that
routineis handled correctly:The important part is the declaration of the function
routine. This lets the compiler know that there is a function of that name, so you can reference it. General advice: Don't do anything else with it.You can declare the prototype with a parameter list and return type, and call it from C, as long as the ABI is met. (WARNING: Do so only if you exactly know what you are doing. It is very brittle! The smallest change of your source or compile options can break it.)
Since such a declaration has no definition, you define the symbol as shown in the inline assembler part.
Please note that some compilers add an underscore as a prefix to C symbols. For such compilers you will need to use
_routine.