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; Would trying puts() be simpler?...

  1. #16
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Would trying puts() be simpler?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sly View Post
    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.
    gcc spits out assembler with the option "-S". Other compilers use other options. Look in the documentation for the compiler itself.

    To call printf from assembler, in x86, you would push the arguments in backwards order...
    Code:
         .code
         push   4711
         push   offset hellow_world
         push   offset format
         call      _printf
         add    esp, 12      // remove 3 args of 4 bytes. 
    
         .data
    format:  db "%s the number is %d\n",0
    hello_world: db "Hello, World"
    The syntax of this may not be correct for nasm, but I think you get the idea.

    --
    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. #18
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thanks for the -s switch, but the asm is not what I meant. I wanted to code my own simple puts function in C. And learn how to jump to a C executable.
    I don't have the standard library. Where does gcc put the asm file?
    Last edited by Sly; 11-29-2008 at 05:25 PM.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and I think I've given you the basic ingredients:
    1. how to write a function that writes to the screen from C.
    2. How to parse a printf-style format string.
    3. How to deal with the arguments.
    4. How to call such a function.

    So here's the final installment, and I'm off to be:
    Code:
    // assuming we have the code from a couple of posts ago to set up screen. 
    int x = 0, y = 0;
    char attr = 0x07;   // standard attribute. 
    void puts(const char *str)
    {
        char c;
        while((c = *str++))   // Extra brackets to prevent gcc from moaning about assignment in condition. 
        {
           if (y >= rows-1) 
           {
                  int i; 
                  for(i = rows-1; i > 1; --i)
                  {
                      memcpy(screen[i-1], screen[i], sizeof(screen[0]));
                   }
                   memset(screen[rows-1], 0, sizeof(screen[0]));
            }
            if (c == '\n')
            {
                 x = 0; 
                 y++;
            }
            else
            {
                screen[y][x].ch = c;
                screen[y][x].attr = attr;
                x++;
                if (x == cols)
                   y++;
            }
       }
    }
    If you actually want a moving cursor as well, then you need to mess about with the video controller (VGA) - I think this page describes it sufficiently to be able to do something with it: http://www.stanford.edu/class/cs140/...ga/textcur.htm

    --
    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. #20
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thank you. I know how to move the cursor from asm. One last thing. When you use the -s switch, where does gcc put the asm file?

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sly View Post
    Thanks for the -s switch, but the asm is not what I meant. I wanted to code my own simple puts function in C. And learn how to jump to a C executable.
    I don't have the standard library. Where does gcc put the asm file?
    Note that it's an -S not -s (-s means something different - can't remember what, but it's "nothing useful"). It puts the file in your working directory, I believe - but it may be the directory of the source (or where you point it with -o, I expect). Note that you have to choose between -S and -c, so you can't generate object file and assembler at the same time.

    You can also use objdump -S when disassembling object files, and it will use debug (-g in gcc) info to determine what source code belongs to which line(s) of assembler code.

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

  7. #22
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thanks tons. It's some strange asm, but it's asm.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sly View Post
    Thanks tons. It's some strange asm, but it's asm.
    Use -masm=intel, and gcc should output something much closer to nasm's syntax - it may not be exactly right, but at least registers are in the same order.

    --
    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. #24
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thanks

Page 2 of 2 FirstFirst 12
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