How do you program without the standard library? For example, how would you display something to the screen?
How do you program without the standard library? For example, how would you display something to the screen?
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.
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 03:58 PM.
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.
I know where video memory is; 0xB8000. What I am wondering is how you would write to it.
Can't you do like:
assuming you don't have an OS not to allow you to write to 0xB8000Code:correctType* ptr = 0xB8000; *ptr = data2send;
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 05:19 PM.
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.
Thank you, but connecting asm to C is something I don't know how to do.
Well, you can google a bit. An example is:
so you put some aseembly code in your C codeCode:__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 */
Like:
Code:int myFun() { int x, y; y = 0; asm("movl %ecx %eax"); return 123; }
So that asm will call the function myFun?
That will fill the screen with a pattern of characters and attributes.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; }
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.
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 05:58 PM.
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.
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.