Bad Register Name

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Bad Register Name

Post 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!
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: Bad Register Name

Post 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.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Bad Register Name

Post by FunnyGuy9796 »

Pardon my ignorance but wouldn’t ‘addr’ and ‘val’ translate to ‘%0’ and ‘%1’?
Last edited by FunnyGuy9796 on Mon Jan 09, 2023 6:54 pm, edited 1 time in total.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: Bad Register Name

Post 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
Octocontrabass
Member
Member
Posts: 5562
Joined: Mon Mar 25, 2013 7:01 pm

Re: Bad Register Name

Post 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;
}
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Bad Register Name

Post by FunnyGuy9796 »

Oh, ok! I had seen this used before but could never really figure out the use or need for it. Thank you!
Post Reply