Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C/C++ pointer to port Posted by basurapr on 24 Jun 2007 at 10:40 AM
Hi, I'm trying to make an experiment with pointers, to try to access a port actual value. This is the code I'm trying on.

#include <cstdio>
using namespace std;

 enum ports {
    KEYBOARD = 0x60
};

typedef unsigned char byte;

int main(int agrc, char** argv){
    byte* port;
    port = (byte*) KEYBOARD;

    printf("%d",*port);
    
    return 0;
}


The code compiles just find, but when I run the executable it gives a runtime error. If I comment the printf statement //printf("%d",*port); it doesn't give any runtime error. Can some tell me if what I'm doing is a silly thing without sense?

Compiler: GCC
Operating System: Windows XP
IDE: Code::Blocks
Report
Re: C/C++ pointer to port Posted by BitByBit_Thor on 24 Jun 2007 at 1:52 PM
: The code compiles just find, but when I run the executable it gives
: a runtime error. If I comment the printf statement
: //printf("%d",*port); it doesn't give any runtime error. Can some
: tell me if what I'm doing is a silly thing without sense?
:
: Compiler: GCC
: Operating System: Windows XP
: IDE: Code::Blocks
:

You're not allowed to directly access memory in Windows.
Basically, the memory block you are accessing isn't yours and thus the CPU+Windows put a stop to the read operation.

You should test pointers to memory within your own program.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: C/C++ pointer to port Posted by basurapr on 24 Jun 2007 at 3:10 PM
: You're not allowed to directly access memory in Windows.
: Basically, the memory block you are accessing isn't yours and thus
: the CPU+Windows put a stop to the read operation.
:
: You should test pointers to memory within your own program.

Thanks for the response, so I can't implement my own outp and inp functions like those found in the non standard library conio.h?

I'm trying this because I'm interested in embedded systems programming.
Also some body explain me that the operating system is on protected mode and doesn't let you directly access ports and memory.

So if I would like to access the keyboard and other kind of ports I would need to use the win32 api, but for embedded systems it should work right?
Report
Re: C/C++ pointer to port Posted by Lundin on 24 Jun 2007 at 11:34 PM
: : You're not allowed to directly access memory in Windows.
: : Basically, the memory block you are accessing isn't yours and thus
: : the CPU+Windows put a stop to the read operation.
: :
: : You should test pointers to memory within your own program.
:
: Thanks for the response, so I can't implement my own outp and inp
: functions like those found in the non standard library conio.h?
:
: I'm trying this because I'm interested in embedded systems
: programming.
: Also some body explain me that the operating system is on protected
: mode and doesn't let you directly access ports and memory.
:
: So if I would like to access the keyboard and other kind of ports I
: would need to use the win32 api, but for embedded systems it should
: work right?
:


There are ways to get around the issue. http://www.beyondlogic.org/ has a solution. The reason why you aren't allowed to direct access things in Windows is a mix between "too many morons writing viruses" and "MS believes that everyone using their programs are morons".

Though accessing ports in Windows has little to do with embedded systems... In Windows, the recommended way is to use the api. On embedded systems, you usually have direct access to everything. Windows != embedded system. And that goes for the joke "Windows CE" too.
Report
Re: C/C++ pointer to port Posted by basurapr on 25 Jun 2007 at 5:48 AM
: There are ways to get around the issue. http://www.beyondlogic.org/
: has a solution. The reason why you aren't allowed to direct access
: things in Windows is a mix between "too many morons writing viruses"
: and "MS believes that everyone using their programs are morons".
:
: Though accessing ports in Windows has little to do with embedded
: systems... In Windows, the recommended way is to use the api. On
: embedded systems, you usually have direct access to everything.
: Windows != embedded system. And that goes for the joke "Windows CE"
: too.

JaJa, thats pretty true, Well I need some emulator like qemu for
practicing. I use UBUNTU too for compiling my applications for both
systems using cross platform libraries.

I read somewhere here that accessing ports in linux can be done using
the fopen function, if true is pretty nice. I just was experimenting.
Recompiling some info because I would like to make a small operating
system that says hello world and accept some input, just for learning.
I'm fascinated with the idea of making such one by my own without the
use of assembly (pure c/c++).

So I'm getting out of the topic of this forum may be, I should move the the Embedded C/C++ forum.

And WOW, that web page have a lot of nice Info, Thank you very much.
Report
Re: C/C++ pointer to port Posted by Lundin on 25 Jun 2007 at 6:44 AM
: JaJa, thats pretty true, Well I need some emulator like qemu for
: practicing. I use UBUNTU too for compiling my applications for both
: systems using cross platform libraries.
:
: I read somewhere here that accessing ports in linux can be done using
: the fopen function, if true is pretty nice. I just was experimenting.
: Recompiling some info because I would like to make a small operating
: system that says hello world and accept some input, just for
: learning.
: I'm fascinated with the idea of making such one by my own without the
: use of assembly (pure c/c++).
:
: So I'm getting out of the topic of this forum may be, I should move
: the the Embedded C/C++ forum.
:
: And WOW, that web page have a lot of nice Info, Thank you very much.
:


Yeah, ports in Linux should work just like files. Actually, ports in Windows work like files too, you just can't use the ANSI C function to access them. Instead you have to use CreateFile(), ReadFile() and such.

I don't think it would be possible to make a "Hello world OS" on a PC without using any assembler. Mainly because of the lack of a compiler that would produce pure op-code binaries, without any calls to Windows-specific kernel dlls and stuff like that. Perhaps there are C/C++ compilers made purely for the pentium processors without the need for an OS, I don't know.

It would be way easier to make an OS on an entirely different platform, like a MCU/MPU/DSP etc.
Report
Re: C/C++ pointer to port Posted by basurapr on 25 Jun 2007 at 7:40 AM
: I don't think it would be possible to make a "Hello world OS" on a
: PC without using any assembler. Mainly because of the lack of a
: compiler that would produce pure op-code binaries, without any calls
: to Windows-specific kernel dlls and stuff like that. Perhaps there
: are C/C++ compilers made purely for the pentium processors without
: the need for an OS, I don't know.

Well I bought a book of C that describe the functionalities of the GCC
compiler and one is that it can produce freestanding programs. It says that
you can compile the code without linking to the "C Runtime" (an object that
has special initialization code for the operating system that you are
compiling for)

The compiler options are -ffreestanding, -nostartfiles and -nostdlib. With
this compiler options the programmer is in charge of implementing the
standard functions that he is going to use, like any printf or scanf.
But I have read many articles of this kind of programming that always
utilize Assembly. But if the code is not linked with any operating system
(pure independet code) then there should be a way of programming without
the use of assembly in any way.

Also GCC compiles code for different kind of processors.

This is an example of what it would be great:

typedef unsigned char* register;

enum registers{ax=placeofax,bx=placeofbx,cx=placeofcx}

register AX = (register) ax;
register BX = (register) bx;
register CX = (register) cx;

*AX = 10;


If that example could be true, it will be fabulous. Instead of using:
mov ax,10
mov ch 12Fh
jmp blah

or the c++ asm reserved keyword: asm("mov ax,10");

Is difficult to program in assembly. And I also don't understand how to use the asm keyword to interact with c++ code. Like for example create a
function in asm from within c++ using the asm keyword and then call it.

Is really complicated :(
Report
Re: C/C++ pointer to port Posted by BitByBit_Thor on 25 Jun 2007 at 9:40 AM
int myCfunction(parameters)
{
  return 0;
}

//Asm equivalent:
int myASMfunction(parameters)
{
  __asm {
    mov eax, 0
  }
}


Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: C/C++ pointer to port Posted by basurapr on 25 Jun 2007 at 10:49 AM
:
: int myCfunction(parameters)
: {
:   return 0;
: }
: 
: //Asm equivalent:
: int myASMfunction(parameters)
: {
:   __asm {
:     mov eax, 0
:   }
: }
: 


Nice example! Thanks!

The correct syntax on G++ is:

int myASMfunction(int parameter)
{
  asm ("movl $0,%eax");
}


But what if I want to substitute the 0 with the int parameter?

Well I saw some code that implements the inportb, here it is:

inline static unsigned char inportb(int port)
{
	register unsigned char r;
   
   	asm volatile
	( 
		"inb %%dx, %%al\n\t" 
		: "=a" (r) 
		: "d" (port)
	);

   	return (r);
}


This code was taken from here http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6743&lngWId=3

The problem is that I don't understand how it works. Does somebody
understand the syntax and logic?
Report
Re: C/C++ pointer to port Posted by BitByBit_Thor on 25 Jun 2007 at 2:46 PM
Well, I'm a bit more familair with NASM syntax, but it looks like it's getting a byte from an IO port (inb) (credit for me knowing this goes to MT2002, with his Bootloader tutorial ;) ).
But this I take *just* from the fact that it says "inb". The rest of the syntax is like chinese to me. Did you copy it right?

If you want to know why this code would work, then it's because in Protected Mode, you're not allowed to access memory directly, but you are allowed to directly access Ports (in fact, it's the ONLY way to communicate with hardware).

If you're genuinely interested in this, check out MT2002's tutorial:
http://www.mt2002.sitesled.com/OSDev7.html

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: C/C++ pointer to port Posted by basurapr on 25 Jun 2007 at 7:00 PM
: Well, I'm a bit more familair with NASM syntax, but it looks like
: it's getting a byte from an IO port (inb) (credit for me knowing
: this goes to MT2002, with his Bootloader tutorial ;) ).
: But this I take *just* from the fact that it says "inb". The rest of
: the syntax is like chinese to me. Did you copy it right?

The asm language syntax utilized on GCC is the AT&T style. I found a web
page that describes the syntax of the inline assembly on GCC using c++:
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
This url describes the differences on Intel assembly syntax and AT&T.

: If you're genuinely interested in this, check out MT2002's tutorial:
: http://www.mt2002.sitesled.com/OSDev7.html

Wow! I was searching on Google for this kind of information and nothing was
so detailed like this web page. Thanks a lot for the info. I'm fascinated
to learn how to write a bootloader and a small OS. Now I should be able
to understand how to communicate with ports and the devices on a system.

Again, Thanks a lot!



Report
Re: C/C++ pointer to port Posted by Lundin on 25 Jun 2007 at 11:49 PM
: asm ("movl $0,%eax");

Note that this isn't only correct syntax in gcc, but on every C++ compiler. In C++, unlike C, inline assembler is specified by the standard. If a compiler can't compile that line, it doesn't follow ANSI/ISO C++.
Report
Re: C/C++ pointer to port Posted by basurapr on 26 Jun 2007 at 5:49 AM
: : asm ("movl $0,%eax");
:
: Note that this isn't only correct syntax in gcc, but on every C++
: compiler. In C++, unlike C, inline assembler is specified by the
: standard. If a compiler can't compile that line, it doesn't follow
: ANSI/ISO C++.

Nice fact, so every C++ compiler use the AT&T Syntax? No Intel Syntax?


Report
Re: C/C++ pointer to port Posted by BitByBit_Thor on 26 Jun 2007 at 5:58 AM
: : : asm ("movl $0,%eax");
: :
: : Note that this isn't only correct syntax in gcc, but on every C++
: : compiler. In C++, unlike C, inline assembler is specified by the
: : standard. If a compiler can't compile that line, it doesn't follow
: : ANSI/ISO C++.
:
: Nice fact, so every C++ compiler use the AT&T Syntax? No Intel
: Syntax?
:
:

I think the exact syntax (AT&T or Intel) isn't specified. It's the asm("...") keyword syntax I think Lundin was talking about.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: C/C++ pointer to port Posted by Lundin on 26 Jun 2007 at 6:39 AM
: I think the exact syntax (AT&T or Intel) isn't specified. It's the
: asm("...") keyword syntax I think Lundin was talking about.


Exactly. Whatever goes between the " and " is naturally system-specific.
Report
Re: C/C++ pointer to port Posted by basurapr on 26 Jun 2007 at 10:14 AM
: : I think the exact syntax (AT&T or Intel) isn't specified. It's the
: : asm("...") keyword syntax I think Lundin was talking about.
:
:
: Exactly. Whatever goes between the " and " is naturally
: system-specific.

Thanks, so the Assembly language syntax is Compiler Specific. There could be compilers that only accept Intel Syntax.

Report
Re: C/C++ pointer to port Posted by BitByBit_Thor on 26 Jun 2007 at 10:16 AM
: Thanks, so the Assembly language syntax is Compiler Specific. There
: could be compilers that only accept Intel Syntax.
:

Yes, in a very hypothetically theoretical improbable situation... yeah (MS Visual Studio)


Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: C/C++ pointer to port Posted by basurapr on 26 Jun 2007 at 6:41 PM
: Yes, in a very hypothetically theoretical improbable situation...
: yeah (MS Visual Studio)

Well I'm Sticked to the GNU Compiler Collection and the Code::Blocks IDE.
I Think that with this tools everything can be done. Like for example ReactOS, this Operating System is a clone of Windows NT family of operating
systems. Is been developed using nasm and GCC. Very nice progress it have.

Not an easy task that one of reactos! :S

I have to search the op-codes that support the gnu compiler for the
assembler language, like the one from the examble above "inb".

Report
Re: C/C++ pointer to port Posted by BitByBit_Thor on 27 Jun 2007 at 11:56 AM
: I have to search the op-codes that support the gnu compiler for the
: assembler language, like the one from the examble above "inb".
:

Actually, the OP-codes are all Intel-format. They're processor specific and for PC's it's Intel's world.
You need to search the ASM syntax used ;)


Best Regards,
Richard

The way I see it... Well, it's all pretty blurry



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.