473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

In and Out on port space within C - any standard?

I'm wanting to write in C some code in which it is necessary to address a device as I/O
space rather than as part of memory. I would like the code to be as standard - and hence
as portable - as possible. It's intended to be later ported to other hardware.

Options?

1) Set up some inline assembler in a header file. I believe I'd need to use the asm
keyword but that this isn't ansi C. Is that correct?
2) Write a separate routine in assembler (to do the actual in and out) and link it with
the C code.
3) Use the appropriate C calls - perhaps they already exist....?

I've seen asm overridden with __asm__ but how does that work? It may stop the compiler
complaining but doesn't it result in the same thing in the end?

Help much appreciated. I have checked the FAQ but searches on "asm" and "assem" returned
nothing.
--
Thanks,
James
Nov 14 '05 #1
9 1402
"James Harris" <no.email.please> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
I'm wanting to write in C some code in which it is necessary to address a device as I/O space rather than as part of memory.
Standard C doesn't support any direct device access at all, 'i/o space',
'memory', or otherwise.
I would like the code to be as standard - and hence
as portable - as possible. It's intended to be later ported to other hardware.
Options?
No portable ones.

1) Set up some inline assembler in a header file. I believe I'd need to use the asm keyword but that this isn't ansi C. Is that correct?
The keyword is, but not the assembler statements themselves.
2) Write a separate routine in assembler (to do the actual in and out) and link it with the C code.
3) Use the appropriate C calls - perhaps they already exist....?
Not standard ones. Standard C does not define any interfaces
with other languages.

I've seen asm overridden with __asm__ but how does that work?
However your implementor designed it. Consult your documentation.
It may stop the compiler
complaining but doesn't it result in the same thing in the end?

Help much appreciated. I have checked the FAQ but searches on "asm" and "assem" returned nothing.


We can't help you here, because your project is outside the domain of
the standard language. Try checking support resources for your
particular implementation.

-Mike
Nov 14 '05 #2

"James Harris" <no.email.please> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
I'm wanting to write in C some code in which it is necessary to address a device as I/O
space rather than as part of memory. I would like the code to be as standard - and hence
as portable - as possible. It's intended to be later ported to other hardware.

Options?

1) Set up some inline assembler in a header file. I believe I'd need to use the asm
keyword but that this isn't ansi C. Is that correct?
2) Write a separate routine in assembler (to do the actual in and out) and link it with
the C code.
3) Use the appropriate C calls - perhaps they already exist....?

I've seen asm overridden with __asm__ but how does that work? It may stop the compiler
complaining but doesn't it result in the same thing in the end?

Help much appreciated. I have checked the FAQ but searches on "asm" and "assem" returned
nothing.


I meant to add that I'd wondered if I should use a Pragma for this but all the FAQ says
about them is they are well defined escape-hatch. What does /that/ mean? - confused....
Nov 14 '05 #3
On Sun, 29 Feb 2004 23:30:21 -0000, "James Harris" <no.email.please> wrote:
I'm wanting to write in C some code in which it is necessary to address a device as I/O
space rather than as part of memory. I would like the code to be as standard - and hence
as portable - as possible. It's intended to be later ported to other hardware.

Options?

1) Set up some inline assembler in a header file. I believe I'd need to use the asm
keyword but that this isn't ansi C. Is that correct?
Correct, nor even ISO C. Raspberries for the asm-in-header-file approach.
2) Write a separate routine in assembler (to do the actual in and out) and link it with
the C code.
That one gets my vote. Localize /all/ the platform-specific code into its
own dedicated module, and then use, say, conditional compilation to select
the platform to compile for.
3) Use the appropriate C calls - perhaps they already exist....?
No such Standard calls exist, no.

I've seen asm overridden with __asm__ but how does that work? It may stop the compiler
complaining but doesn't it result in the same thing in the end?
I don't know, but all that asm stuff should be in the platform-specific
module, however it works.

Help much appreciated. I have checked the FAQ but searches on "asm" and "assem" returned
nothing.


Good luck,
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #4

"James Harris" <no.email.please> wrote in message
I'm wanting to write in C some code in which it is necessary to
address a device as I/O space rather than as part of memory.

There's no way of doing this portably. What you want to do is separate out
all the hardware interface code into a separate file, and then make the
functions C-callable.
How you actually write the hardware layer depends on your platform. Probably
you will have some version of the asm keyword that allows inline assembly
(and does a lot of book-keeping for you). However you will have to rewrite
the code to port to another platform, so keep it separated out and as short
as possible.
Nov 14 '05 #5
On Sun, 29 Feb 2004 23:45:52 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c:
"James Harris" <no.email.please> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
I'm wanting to write in C some code in which it is necessary to address a

device as I/O
space rather than as part of memory.


Standard C doesn't support any direct device access at all, 'i/o space',
'memory', or otherwise.


[snip]
1) Set up some inline assembler in a header file. I believe I'd need to

use the asm
keyword but that this isn't ansi C. Is that correct?


The keyword is, but not the assembler statements themselves.


I'm afraid you are thinking C++ here, not C. There is no "asm"
keyword in C, it has never been part of any standard, not even the
original 1989 ANSI one.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:e8********************************@4ax.com...
On Sun, 29 Feb 2004 23:45:52 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c:

The keyword is, but not the assembler statements themselves.


I'm afraid you are thinking C++ here, not C. There is no "asm"
keyword in C, it has never been part of any standard, not even the
original 1989 ANSI one.


Yes, oops. Thanks for the correction.

-Mike
Nov 14 '05 #7
"James Harris" <no.email.please> a écrit ...
I'm wanting to write in C some code in which it is necessary to address a device as I/O space rather than as part of memory. I would like the code to be as standard - and hence as portable - as possible. It's intended to be later ported to other hardware.
Options?

1) Set up some inline assembler in a header file. I believe I'd need to use the asm keyword but that this isn't ansi C. Is that correct?
In C99 standard:

J.5.10 The asm keyword
1 The asm keyword may be used to insert assembly language directly into the
translator output (6.8). The most common implementation is via a statement
of the form:
asm ( character-string-literal );

Maybe you can try anything like writing conditional blocks in a header, one
per hardware.

typedef unsigned char u8;

For Intel with short port address (0 to 255), 8 bits wide ports:

const u8 PORT_IN = 0xF0;
const u8 PORT_OUT = 0xF4;

inline void getPort(u8 MemVar)
{
asm
{
push ax
in ax, PORT_IN
mov byte[MemVar], ax
pop ax
}
}

inline void setPort(u8 MemVar)
{
asm
{
push ax
mov ax, byte[MemVar]
out PORT_OUT, ax
pop ax
}
}

For another hardware with memory-mapped I/O:

const volatile u8* const PORT_IN = (volatile u8*) 0x00000412;
u8* const PORT_OUT = (volatile u8*) 0x0000041A;

inline void getPort(u8 MemVar){MemVar = PORT_IN;}

inline void setPort(u8 MemVar){PORT-_OUT = MemeVar;}

2) Write a separate routine in assembler (to do the actual in and out) and link it with the C code.

Slow, no ? And no more portable.

Pierre


Nov 14 '05 #8
"Pierre Maurette" <mmaauurreettttttee.ppiieerrrree@@ffrreeee.ffrr>
wrote:
"James Harris" <no.email.please> a écrit ...
1) Set up some inline assembler in a header file. I believe I'd need to
use the asm keyword but that this isn't ansi C. Is that correct?
In C99 standard:

J.5.10 The asm keyword


Note, first, that the name of that section is "Common Extensions", and
second, that most appendices, J included, aren't normative. IOW, this is
a description of what compiler writers have added to their compilers
besides C proper, it is not ISO C.
This means, for example, that when invoked in ISO mode, _no_ compiler is
allowed to complain about asm being used as a variable name, no matter
whether it provides an assembly extension or not.
1 The asm keyword may be used to insert assembly language directly into the
translator output (6.8). The most common implementation is via a statement
of the form:


Note: most common. _Not_ required, or even allowed in pure ISO mode.

Richard
Nov 14 '05 #9
Pierre Maurette wrote:
"James Harris" <no.email.please> a écrit ...
I'm wanting to write in C some code in which it is necessary to
address a device as I/O space rather than as part of memory. I
would like the code to be as standard - and hence as portable
as possible. It's intended to be later ported to other hardware.

Options?

1) Set up some inline assembler in a header file. I believe I'd
need to use the asm keyword but that this isn't ansi C. Is that
correct?


In C99 standard:

J.5.10 The asm keyword
1 The asm keyword may be used to insert assembly language directly
into the translator output (6.8). The most common implementation
is via a statement of the form:

asm ( character-string-literal );


You omitted the precursor to that section, which is:

J.5 Common extensions

[#1] The following extensions are widely used in many
systems, but are not portable to all implementations. The
inclusion of any extension that may cause a strictly
conforming program to become invalid renders an
implementation nonconforming. Examples of such extensions
are new keywords, extra library functions declared in
standard headers, or predefined macros with names that do
not begin with an underscore.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan | last post by:
I am running two database instances of mysql (one on a nonstandard port). After granting users permissions I find that I can access mysql from other machines only if I use the standard port. When...
2
by: nik | last post by:
Hi all I'm struggling to find an answer to this one! We've configured our SQL boxes to run under a different port number, e.g. from 1433 to 4533. This works fine under our Citrix enviornmnet...
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
2
by: bernatik | last post by:
hi all I am trying to control special device through serial port from Linux. For that purpose I need to control DTR signal. I am watching the signal on a oscilloscope, so I know exactly what is...
2
by: Greg W. | last post by:
In our setup project, we have the standard web application folder, but we also have 4 web custom folders (virtual directories) that are created at the same level as the web application folder...
15
by: xkenneth | last post by:
Hi, I'm writing a couple python applications that use the serial port (RS-232) quite extensively. Is there any way I can monitor all activity on the serial port and have it printed as the...
0
by: basuritta | last post by:
I am trying to add a SQL2000 subscriptor to a SQL2005 publisher and distributor using the Management Studio. I'm using a non-standard port in both ends, but I have set the port for the TCP/IP...
0
by: dcm684 | last post by:
I am communicating with a microcontroller device and when I try to send a zero character my computer ends up sending a space instead. Occasionally, the computer will end up sending the '0' instead....
1
by: sdbranum | last post by:
For those who have found it impossible to get help in communicating with Wasp's WDT-2200 Barcode Scanner through a COM Port, except by using their supplied DTComm software, I am presenting what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.