I/O functions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
p0s1x
Posts: 18
Joined: Thu Mar 21, 2013 8:37 am

I/O functions

Post 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 :)
MDenham
Member
Member
Posts: 62
Joined: Sat Nov 10, 2012 1:16 pm

Re: I/O functions

Post by MDenham »

Are you sure you're operating in real mode when this function is called?
User avatar
p0s1x
Posts: 18
Joined: Thu Mar 21, 2013 8:37 am

Re: I/O functions

Post 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.
User avatar
p0s1x
Posts: 18
Joined: Thu Mar 21, 2013 8:37 am

Re: I/O functions

Post 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?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: I/O functions

Post 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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: I/O functions

Post 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.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: I/O functions

Post 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.
User avatar
p0s1x
Posts: 18
Joined: Thu Mar 21, 2013 8:37 am

Re: I/O functions

Post by p0s1x »

How to move CPU in Real Time Mode?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: I/O functions

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
p0s1x
Posts: 18
Joined: Thu Mar 21, 2013 8:37 am

Re: I/O functions

Post by p0s1x »

Bare Bones switch CPU to Real or Protected mode?
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: I/O functions

Post 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?
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
p0s1x
Posts: 18
Joined: Thu Mar 21, 2013 8:37 am

Re: I/O functions

Post by p0s1x »

I want to switch CPU to protected mode for access video memory
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: I/O functions

Post 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:
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
p0s1x
Posts: 18
Joined: Thu Mar 21, 2013 8:37 am

Re: I/O functions

Post by p0s1x »

My friend send me code:

Code: Select all

sysenter
"That code enter system to pmode", really?
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: I/O functions

Post by Nessphoro »

p0s1x wrote:My friend send me code:

Code: Select all

sysenter
"That code enter system to pmode", really?
Attachments
1302143491780.jpg
1302143491780.jpg (6.77 KiB) Viewed 2863 times
Post Reply