Program without standard library?

This is a discussion on Program without standard library? within the C Programming forums, part of the General Programming Boards category; How do you program without the standard library? For example, how would you display something to the screen?...

  1. #1
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65

    Program without standard library?

    How do you program without the standard library? For example, how would you display something to the screen?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you don't have a standard library, you would have to write your own display functions. How you do that depends on the OS (or BIOS/monitor/runtime environment - which is various names for some type of base-code that isn't really an OS, but provides interfaces between the software and actual hardware).

    For example, in Windows, you could use WriteFile() with STDOUT_HANDLE to write to the display, and not use the C runtime library. It's obviously a whole heap harder than using printf, especially if you want to format numeric data into nice columns.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I mean, without standard library or an OS. Like, without BIOS interrupts in pmode.
    No BIOS interrupts, and no base code. Just C and some inline asm.
    Last edited by Sly; 11-29-2008 at 02:58 PM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sly View Post
    I mean, without standard library or an OS. Like, without BIOS interrupts in pmode.
    No BIOS interrupts, and no base code. Just C and some inline asm.
    Well, WriteFile in Windows does not use BIOS or Standard library functions, but I guess that's just escaping the actual answer, which is more along the lines of "It depends".

    You would have to find out where the display memory is, and then poke the correct values into the memory. Or if your output is on a serial port, you'd have to know how to send data onto the serial port.

    There is no one way to do this, because it's specific depending on what exactly what hardware the system consists of.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I know where video memory is; 0xB8000. What I am wondering is how you would write to it.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Can't you do like:
    Code:
    correctType* ptr = 0xB8000;
    *ptr = data2send;
    assuming you don't have an OS not to allow you to write to 0xB8000

  7. #7
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Maybe, but how would I print to an x/y location on screen?

    In NASM, I would use this to print to the top left corner of the screen...


    Code:
    mov edi, 0xB8000                            ;pointer to video memory
    mov [edi], 'A'                                    ;Letter to print
    mov [edi+1],0x7                               ;Character attribute
    Last edited by Sly; 11-29-2008 at 04:19 PM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sly View Post
    Maybe, but how would I print to an x/y location on screen?

    In NASM, I would use this to print to the top left corner of the screen...


    Code:
    mov edi, 0xB8000                            ;pointer to video memory
    mov [edi], 'A'                                    ;Letter to print
    mov [edi+1],0x7                               ;Character attribute
    Since the screen is linear and contiguous (that is, the stride [bytes from position x on line y to position x on line y+1] is the same as the width), you just multiply y by 160 (2 * 80) and add x * 2.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thank you, but connecting asm to C is something I don't know how to do.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, you can google a bit. An example is:
    Code:
     __asm__ ("movl %eax, %ebx\n\t"
                     "movl $56, %esi\n\t"
                      "movl %ecx, $label(%edx,%ebx,$4)\n\t"
                      "movb %ah, (%ebx)");
    
    or 
    
    asm("movl %ecx %eax"); /* moves the contents of ecx to eax */
    so you put some aseembly code in your C code

    Like:
    Code:
    int myFun()
    {
       int x, y;
       y = 0;
       asm("movl %ecx %eax");
       return 123;
    }

  11. #11
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    So that asm will call the function myFun?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sly View Post
    Thank you, but connecting asm to C is something I don't know how to do.
    Code:
    struct screenchar
    {
        char ch;
        char attr;
    };
    
    const int cols = 80;
    const int rows = 25;
    
    
    typedef struct screenchar[rows][cols] Screen;
    
    Screen *screenPtr = (Screen *)0xB8000;
    
    
    int main()
    {
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                  screen[i][j].ch = ' ' + j;
                  screeb[i][j].attr = 0xF0 ^ j;
             }
         }
         reurn 0;
    }
    That will fill the screen with a pattern of characters and attributes.

    Just be happy that you are not on an ABC80, that had some pretty interesting calculations to get to particular screen coordinates - it involed XOR of 128, that's all I really remember.

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    But how do I write a printf funtion, then call this function from asm? For example, load the file?
    Last edited by Sly; 11-29-2008 at 04:58 PM.

  14. #14
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I am actually trying to write a function that will work at bootup. I have a loader already, and what I want to do is create a function like printf, and then also be able to jump from asm to C code. Oh, and I don't know how to make a compiler spit out asm.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    printf() works on a relatively simple principle if we ignore some of the obscure uses (e.g. %*.*f might be more than you need to support?)

    The formatting of "%s", "%c", etc is done by iterating through characters of the format string. If the character is a "%", then you take the next character and figure out what it means. If it's a number, it's part of the width. If it's a minus, it means "adjust to the left", if it's a 'c' or 's', then it's what you are supposed to print, so pick the corresponding argument and output it according to how the format specifies.

    The argument part is dealing with variable arguments, but it's not that difficult to deal with. There is actually an example that resembles printf in the man-pages for va_list, so if you google for "man va_list", it will show you that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C library in C++ program?
    By Kernel Sanders in forum C++ Programming
    Replies: 7
    Last Post: 09-10-2008, 01:12 PM
  2. Which of the C 'Standard Library headers' are worth rembering?
    By Ravens'sWrath in forum C Programming
    Replies: 24
    Last Post: 05-17-2007, 02:12 AM
  3. Difference between GLIBC and Standard C library
    By RoshanX in forum Tech Board
    Replies: 5
    Last Post: 04-15-2007, 02:22 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Links to learn standard C library functions...
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-31-2002, 11:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21