Page 1 of 1

Bad Register Name

Posted: Mon Jan 09, 2023 6:29 pm
by FunnyGuy9796
I am trying to write to a register and want to be able to set the register as a variable. However, when I try to use inline assembly in order to set the register to a variable I get an error saying "Bad register name '%0'"

Here is the code:

Code: Select all

void write_reg(uint32_t addr, uint32_t val) {
    __asm__ volatile("mov %0, %1");
}
I believe I am doing it correctly by setting the register and value as variables using "%0" and "%1". Help would be much appreciated!

Re: Bad Register Name

Posted: Mon Jan 09, 2023 6:39 pm
by klange
You haven't specified any inputs or outputs. Without them, the "%0" syntax does not work and an actual register name is expected.

Re: Bad Register Name

Posted: Mon Jan 09, 2023 6:44 pm
by FunnyGuy9796
Pardon my ignorance but wouldn’t ‘addr’ and ‘val’ translate to ‘%0’ and ‘%1’?

Re: Bad Register Name

Posted: Mon Jan 09, 2023 6:53 pm
by klange
FunnyGuy9796 wrote:Arson my ignorance but wouldn’t ‘addr’ and ‘val’ translate to ‘%0’ and ‘%1’?
No. You need to pass them as inputs, and also mark clobbers. They are not associated with your arguments by default - and your arguments don't exist in registers by default in x86-32 anyway as they are passed on the stack. You need explicitly ask for them to be put into registers for use by the inline assembly.

Here's a quick link to the manual for reference: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html

Re: Bad Register Name

Posted: Mon Jan 09, 2023 6:55 pm
by Octocontrabass
Do you need inline assembly in the first place? This is the sort of thing the volatile keyword is intended for.

Code: Select all

void write_reg( size_t addr, uint32_t val )
{
    *(volatile uint32_t *)addr = val;
}

Re: Bad Register Name

Posted: Mon Jan 09, 2023 7:27 pm
by FunnyGuy9796
Oh, ok! I had seen this used before but could never really figure out the use or need for it. Thank you!