Page 1 of 3
I/O functions
Posted: Thu Mar 21, 2013 8:44 am
by p0s1x
Hello world, I have a question, how to make basic printf or scanf functions on OS?
I use this:
Code: Select all
void write_string( int colour, const char *string )
{
volatile char *video = (volatile char*)0xB8000;
while( *string != 0 )
{
*video++ = *string++;
*video++ = colour;
}
};
but this can't print any string or character. Console pointer goes crazy. Can you help? Thanks.
//Sorry for bad English
Re: I/O functions
Posted: Thu Mar 21, 2013 10:20 am
by MDenham
Are you sure you're operating in real mode when this function is called?
Re: I/O functions
Posted: Thu Mar 21, 2013 10:23 am
by p0s1x
MDenham wrote:Are you sure you're operating in real mode when this function is called?
I use bare bones from
here. I not sure.
Re: I/O functions
Posted: Thu Mar 21, 2013 10:45 am
by p0s1x
I use this (My code :3):
Code: Select all
void printf(char *string )
{
asm(
"mov %si, string \n"
"call DisplayMessage \n"
"DisplayMessage: \n"
"lodsb \n"
"or %al, %al \n"
"jz .DONE \n"
"mov %ah, 0x0E \n"
"mov %bh, 0x00 \n"
"mov %bl, 0x07 \n"
"int $0x10 \n"
"jmp DisplayMessage \n"
".DONE: \n"
"ret \n");
};
And have linker error:
Code: Select all
kernel.o:kernel.c:(.text+0x40): undefined reference to `string'
How to use C variables in inline assembler?
Re: I/O functions
Posted: Thu Mar 21, 2013 11:36 am
by bluemoon
If you follow the bare bone tutorial, I assume you're on gcc.
inline assembly with gcc uses input/output/clobber lists instead of putting variable names directly in assembly instruction (aka old visual c++ style), I suggest you read the gcc inline assembly manual.
By the way, string is a really bad variable name.
Re: I/O functions
Posted: Thu Mar 21, 2013 11:58 am
by Gigasoft
If your GCC program is running, then the processor must not be in real mode. Otherwise, you would not be able to run your program. Therefore, you can not call BIOS functions.
Re: I/O functions
Posted: Thu Mar 21, 2013 4:15 pm
by iansjack
In your original program you are assuming that
char *video = (volatile char*)0xB8000;
points to video memory. I'd say that is quite a large assumption; it may depend upon what value you have placed in DS.
Re: I/O functions
Posted: Fri Mar 22, 2013 5:14 am
by p0s1x
How to move CPU in Real Time Mode?
Re: I/O functions
Posted: Fri Mar 22, 2013 7:48 am
by Brendan
Hi,
p0s1x wrote:How to move CPU in Real Time Mode?
Do you mean: How do you move data (using the CPU) while in Real Mode?
If this is what you're asking, then my favourite is "rep movsd".
Cheers,
Brendan
Re: I/O functions
Posted: Fri Mar 22, 2013 9:55 am
by p0s1x
Bare Bones switch CPU to Real or Protected mode?
Re: I/O functions
Posted: Fri Mar 22, 2013 11:27 am
by Mikemk
p0s1x wrote:Bare Bones switch CPU to Real or Protected mode?
You want to switch from real mode to pmode, then immediately back to real mode?
Re: I/O functions
Posted: Fri Mar 22, 2013 11:49 am
by p0s1x
I want to switch CPU to protected mode for access video memory
Re: I/O functions
Posted: Fri Mar 22, 2013 1:05 pm
by Mikemk
Here is a code sample. Let's see if you can implement it.
BTW, I don't know c++, so you or somebody will have to translate.
From real mode (assuming string index passed in ds:si, and color in al):
Code: Select all
print:
mov bx, 0xb800
mov es, bx
xor bx, bx
.loop:
mov ah, [si]
cmp ah, 0
je .endi
mov [es:bx], ax
add bx, 2
inc si
jmp .loop
.endi:
pmode:
Code: Select all
print:
mov bx, [i]selector[/i]
mov es, bx
mov ebx, 0xb8000
.loop:
mov ah, [esi]
cmp ah, 0
je .endi
mov [es:ebx], ax
add ebx, 2
inc esi
jmp .loop
.endi:
Re: I/O functions
Posted: Fri Mar 22, 2013 11:34 pm
by p0s1x
My friend send me code:
"That code enter system to pmode", really?
Re: I/O functions
Posted: Fri Mar 22, 2013 11:40 pm
by Nessphoro
p0s1x wrote:My friend send me code:
"That code enter system to pmode", really?