Thread: getting a variable's segment and offset address using gcc(kernel mode programming)

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    getting a variable's segment and offset address using gcc(kernel mode programming)

    Does anyone knows how to get a variable's segment and offset address using c... I need the information for a VESA driver that I am writing.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Is this Linux kernel? If so, the segment is irrelevant, because all segments are based at physical address 0. I assume you have a variable and you want to obtain its physical address. If the variable has been allocated by kmalloc(), or is a static variable, you can use the __pa() macro.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    No this is a home brewed kernel....

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jacob Dahlen View Post
    Does anyone knows how to get a variable's segment and offset address using c... I need the information for a VESA driver that I am writing.
    WOW, this must be our day for ancient and long abandoned stuff that's never going to work on a modern computer. A few minutes ago it was TSR programming.... Now it's VESA graphics that died along with 320 x 200 ega resolutions and 8 bit processors.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    No this is a home brewed kernel....
    Is this an allocated black of memory, something setting on the stack, a static global, or something else?

    You aren't going to get any answers if you don't give more information.

    Now it's VESA graphics that died along with 320 x 200 ega resolutions and 8 bit processors.
    O_o

    Swing and a miss.

    Soma

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Well, then if you would like to write drivers of all the 100s upon 100s of video cards for me, by all means go for it....

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Not sure, but what about....
    Code:
    #include <stdio.h>
    
    /*
     *  Assuming 32-bit (adjust as necessary for 64-bit)
     *    $ gcc -m32 main.c
     *    $ ./a.out 
     *    Address of aVariable: 0xbffff2ac 
     *    Segment of aVariable: bfff 
     *    Offset  of aVariable: f2ac
     */
    int main( void ) 
    {
        unsigned aVariable = 1;
    
        printf( "Address of aVariable: %p\n", &aVariable );
        printf( "Segment of aVariable: %x\n", ( ( unsigned )&aVariable & 0xFFFF0000 ) >> 16 );
        printf( "Offset  of aVariable: %x\n", ( unsigned )&aVariable & 0x0000FFFF );
    
        return 0;
    }
    Last edited by kmess; 04-20-2011 at 04:56 PM. Reason: Correct code formatting

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by phantomotap View Post
    Is this an allocated black of memory, something setting on the stack, a static global, or something else?

    You aren't going to get any answers if you don't give more information.



    O_o

    Swing and a miss.

    Soma
    This is 25 year old technology, hearalding back before the 386 chip. Way back then Chips only had 20 bits of address buss and memory was addressed in segments and offsets... the segment register set up a 64k block of memory and the 16 bit address register gave you an address inside the segment... These registers don't even exist any more and since the indtroduction of the 386 processor memory is addressed as a continuous block starting at 0.

    Even the desire for this speaks to technological understanding 25 years behind the times.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Er, strike that. I doubt that will work. I'm guessing you might need to do some inline assembly...

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jacob Dahlen View Post
    Well, then if you would like to write drivers of all the 100s upon 100s of video cards for me, by all means go for it....
    Every video card has (at least) 3 default modes built in... 720x400 text mode, 640x480 ega graphics and 800 x 600 vga graphics. If you look at documentation *from this millenium* you will discover the configuration codes for setting these three modes and the memory addresses of the buffers... no segments, no offsets... just a 32 or 64 bit memory address.

    And, oh boy are you gonna have a cow when you discover Plug and Play...
    Last edited by CommonTater; 04-20-2011 at 05:08 PM.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by kmess View Post
    Not sure, but what about....
    Code:
    #include <stdio.h>
    
    /*
     *  Assuming 32-bit (adjust as necessary for 64-bit)
     *    $ gcc -m32 main.c
     *    $ ./a.out 
     *    Address of aVariable: 0xbffff2ac 
     *    Segment of aVariable: bfff 
     *    Offset  of aVariable: f2ac
     */
    int main( void ) 
    {
        unsigned aVariable = 1;
    
        printf( "Address of aVariable: %p\n", &aVariable );
        printf( "Segment of aVariable: %x\n", ( ( unsigned )&aVariable & 0xFFFF0000 ) >> 16 );
        printf( "Offset  of aVariable: %x\n", ( unsigned )&aVariable & 0x0000FFFF );
    
        return 0;
    }
    In file included from /usr/include/features.h:387,
    from /usr/include/stdio.h:28,
    from main.c:1:
    /usr/include/gnu/stubs.h:7: fatal error: gnu/stubs-32.h: No such file or directory
    compilation terminated.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by CommonTater View Post
    Every video card has (at least) 3 default modes built in... 720x400 text mode, 640x480 ega graphics and 800 x 600 vga graphics. If you look at documentation *from this millenium* you will discover the configuration codes for setting these three modes and the memory addresses of the buffers... no segments, no offsets... just a 32 or 64 bit memory address.

    And, oh boy are you gonna have a cow when you discover Plug and Play...
    Well then where do I find this mythical documentation of yours?

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Bah, don't listen to those guys.

    Are you writing a real-mode or protected-mode operating system?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    One source is the Microsoft Driver SDK which is freely dowloadable.
    Google is your friend.

    What I'm mostly interested in is where you're getting this grossly outdated information from...
    There was another guy on here today asking about TSR programming which hasn't existed since MS-DOS was abandoned.
    What you're talking about is the functional equivalent of HIMEM.SYS, also part of MS-DOS and well and truly dead along with t.We are also beseiged at times by people using 16 bit compilers (usually Borland's TurboC) and wondering why their stuff isn't working correctly.

    Taking this along with several other messages over the past while --one wanting help with 8080 processors-- I'm starting to think there's some school someplace with a teacher that is disastrously behind the times and still teaching with tools and currucula from at least 20 years ago. That anyone is still teaching this stuff is a very scary idea, leaves me wonding how many perfectly good programmers chances this teacher has ruined.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Well then where do I find this mythical documentation of yours?
    Look for PCI interface manuals, or whatever the bus specifications are, that your video adapter is connected. You probably will not have access to the stdio routines if you are working in kernel mode since until the kernel is initialized you may not have any input or output ports initialized.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-04-2008, 01:27 PM
  2. User mode and Kernel Mode
    By learner01 in forum Linux Programming
    Replies: 9
    Last Post: 08-10-2006, 08:20 PM
  3. User mode and Kernel mode
    By learner01 in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2006, 02:38 PM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. User-mode and Kernel-mode
    By Eibro in forum Tech Board
    Replies: 1
    Last Post: 06-30-2003, 09:27 PM