OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Sep 26, 2017 8:27 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Inline Assembly Examples
PostPosted: Mon Mar 07, 2016 6:25 am 
Offline
Member
Member
User avatar

Joined: Thu Dec 19, 2013 1:40 am
Posts: 100
Location: Asia, Singapore
Hi guys,
I know the wiki is free for editing, but I just want to check if I am right before I make any edits (Especially since I am not so good at inline assembly). Regarding the INx function, am I right to say there are some issues with the code? Should the line
Code:
asm volatile ( "inb %[port], %[ret]"

be replaced with
Code:
asm volatile ( "inb %[port], %[result]"

Thanks.

_________________
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.


Top
 Profile  
 
 Post subject: Re: Inline Assembly Examples
PostPosted: Mon Mar 07, 2016 6:38 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7127
Location: Germany
I'd probably drop the "symbolic operand name" example instead; the very error itself is a good example as to why you should avoid introducing identifiers when you don't have to.

Code:
static inline uint8_t inb(uint16_t port)
{
    uint8_t ret;
    asm volatile ( "inb %1, %0"
                   : "=a"(ret)
                   : "Nd"(port) );
    return ret;
}


Then again, I am by no means an expert on the inline assembly syntax, have never actually used it myself, and have no idea if the code above does what it should or will launch da bomb. :oops: :twisted:

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: Inline Assembly Examples
PostPosted: Mon Mar 07, 2016 8:11 am 
Offline
Member
Member
User avatar

Joined: Thu Dec 19, 2013 1:40 am
Posts: 100
Location: Asia, Singapore
Hm...I guess since there is only two operands anyway, it should be still fine to not use the symbolic names and use them in any order.
Though I feel having symbolic names feel more natural, but its probably personal preference.
And I am sure that code can't mess up that badly if it is wrong. Unless the OS happens to use this function and for some reason is used to control a nuclear reactor. I guess that will really drop the bomb.

_________________
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.


Top
 Profile  
 
 Post subject: Re: Inline Assembly Examples
PostPosted: Tue Mar 08, 2016 4:07 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7127
Location: Germany
hometue wrote:
Though I feel having symbolic names feel more natural, but its probably personal preference.


It is.

I've spent the last 15 years doing maintenance on other people's code. Every identifier removed from the code in favor of directly using a temporary means one thing less to keep track of, mentally; and with C++11, having a value being an unnamed temporary means move semantics, further boosting my personal preference.

YMMV.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: Inline Assembly Examples
PostPosted: Fri Mar 18, 2016 4:23 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 953
Location: Athens, GA, USA
I think that a lot of programmers - especially early in their careers - often take the general defensive programming advice 'avoid hard-coded constants with arbitrary or otherwise non-obvious meanings, especially if they may change in the future' as meaning 'always use a new symbolic name for every new value'. This can color ones view in several areas, including this sort of anonymous temporaries. As with the usual advice of avoiding gotos in HLLs, it takes some time to learn the nuances of where to relax that discipline, where following the heuristic makes things worse rather than better, etc.

Also, that advice, while generally applicable, doesn't fit this context very well; another heuristic is that inline assembly should not exceed 5-7 lines without an overriding reason, which means that unless the context is an unusual one, there is little information lost from using anon temporaries in inline assembly, while using the named variables from one local namespace in a completely independent namespace can lead to exactly this sort of collision.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 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