C/C++ pointer to port

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.

[code]
#include
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;
}
[/code]

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?

[b]Compiler:[/b] GCC
[b]Operating System:[/b] Windows XP
[b]IDE:[/b] Code::Blocks
«1

Comments

  • : 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?
    :
    : [b]Compiler:[/b] GCC
    : [b]Operating System:[/b] Windows XP
    : [b]IDE:[/b] 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
  • : 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?
  • : : 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.
  • : 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.
  • : 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.
  • : 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:

    [code]
    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;
    [/code]

    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 :(
  • [code]
    int myCfunction([italic]parameters[/italic])
    {
    return 0;
    }

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

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : [code]
    : int myCfunction([italic]parameters[/italic])
    : {
    : return 0;
    : }
    :
    : //Asm equivalent:
    : int myASMfunction([italic]parameters[/italic])
    : {
    : __asm {
    : mov eax, 0
    : }
    : }
    : [/code]

    Nice example! Thanks!

    The correct syntax on G++ is:

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

    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:

    [code]
    inline static unsigned char inportb(int port)
    {
    register unsigned char r;

    asm volatile
    (
    "inb %%dx, %%al
    "
    : "=a" (r)
    : "d" (port)
    );

    return (r);
    }
    [/code]

    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?
  • 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:
    [link=null]http://www.mt2002.sitesled.com/OSDev7.html[/link]

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : 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++:
    [link=http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html]http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html[/link]
    This url describes the differences on Intel assembly syntax and AT&T.

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

    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!



  • : 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++.
  • : : 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?


  • : : : 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
  • : 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.
  • : : 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.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories