OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Mar 19, 2024 8:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Bad Register Name
PostPosted: Mon Jan 09, 2023 6:29 pm 
Offline
Member
Member

Joined: Tue Sep 13, 2022 9:29 pm
Posts: 61
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:
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!


Top
 Profile  
 
 Post subject: Re: Bad Register Name
PostPosted: Mon Jan 09, 2023 6:39 pm 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
You haven't specified any inputs or outputs. Without them, the "%0" syntax does not work and an actual register name is expected.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: Bad Register Name
PostPosted: Mon Jan 09, 2023 6:44 pm 
Offline
Member
Member

Joined: Tue Sep 13, 2022 9:29 pm
Posts: 61
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.

Top
 Profile  
 
 Post subject: Re: Bad Register Name
PostPosted: Mon Jan 09, 2023 6:53 pm 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
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

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: Bad Register Name
PostPosted: Mon Jan 09, 2023 6:55 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5069
Do you need inline assembly in the first place? This is the sort of thing the volatile keyword is intended for.
Code:
void write_reg( size_t addr, uint32_t val )
{
    *(volatile uint32_t *)addr = val;
}


Top
 Profile  
 
 Post subject: Re: Bad Register Name
PostPosted: Mon Jan 09, 2023 7:27 pm 
Offline
Member
Member

Joined: Tue Sep 13, 2022 9:29 pm
Posts: 61
Oh, ok! I had seen this used before but could never really figure out the use or need for it. Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group