Page 1 of 3

printing a char in C

Posted: Thu Aug 08, 2002 7:34 am
by frank
Hi,

How do I print a char in C (32bit pmode)?
Accessing 0B800h doesn't work anymore...
and since I'm in C, I don't have an gdt table with an linear adress....

Re:printing a char in C

Posted: Thu Aug 08, 2002 9:48 am
by John
Do it from the kernel part able to do it (this with enough permissions). Eg. console device driver.

May be a system call? Probably you'll need kinda wraper coded in asm to make an int. Check any open source system for intel 386 to see how to do it.

I've not coded user/kernel rings, so i cannot put here a clear example about how to do it.

regards,

John

Re:printing a char in C

Posted: Thu Aug 08, 2002 10:59 am
by frank
hmmm, it doesn't work...

-----
int main()
{
char *vidmem = (char *) 0xb8000;

vidmem[1]=0x7;
vidmem[2]='f';


while(1)
{


}
}
---


Any ideas why?

Re:printing a char in C

Posted: Thu Aug 08, 2002 11:08 am
by crazybuddha
1) maybe you're not really executing this code ... that is, are you sure you've even gotten to this point

2) vidmem[1] isn't the beginning byte. It's actually the byte for a char, not the attribute.

without the rest of your code, it's hard to know what else may be wrong.

Re:printing a char in C

Posted: Thu Aug 08, 2002 11:14 am
by frank
Got this code now:

int main()
{
unsigned short *vidmem = (unsigned short*)0xb8000;
int i=0;

vidmem = 0x0941;

while(1)
{


}
}

I'm gonna make sure wether its loaded or not...

Re:printing a char in C

Posted: Thu Aug 08, 2002 11:19 am
by frank
jep, it does load...
(tried this simple asm file to make sure:
times 512-($-$$) db 0, otherwise an empty file..

Still, I have no idea why printing a character does not work...

Re:printing a char in C

Posted: Thu Aug 08, 2002 12:11 pm
by frank
nope, i'm stlil jumping to 0x0,
I'm gonna change that now...

Re:printing a char in C

Posted: Thu Aug 08, 2002 12:22 pm
by frank
Changed it, but it doesn't work yet...

Here are the codes:
-------------------------------
org 07c00h
mov [drive],dl
jmp readkern

readkern:
bits 16
mov ah,0Eh
mov al,'Q'
int 0x10

mov ah,0x02
mov al,2
mov ch,00000000b
mov cl,00000010b
mov dh,00000000b
mov dl,[drive]
mov bx,0x1 ; this memory place,please :]
int 13h

cli
lgdt[gdtr]
mov eax,cr0
or al,1
mov cr0,eax
jmp dword codesel:0x1

gdtr
dw gdt_end-1
dd gdt
gdt
nullsel equ $-gdt
gdt0
dd 0
dd 0
codesel equ $-gdt
dw 0ffffh
dw 0h
db 0h
db 09ah
db 0cfh
db 0h
datasel equ $-gdt
dw 0ffffh
dw 0h
db 0h
db 092h
db 0cfh
db 0h

gdt_end

drive db 0
times 510-($-$$) db 0
sign dw 0xAA55
--------------------------------------

Here's the C code:
----------------------------------
int main()
{
unsigned short *vidmem = (unsigned short*)0xb8000;
int i=0;

vidmem = 0x0941;

for ( ; ; )
{


}

}
-------------------------------------

It does hang, but does not print a character....
The character should be on the top-left, right?

Re:printing a char in C

Posted: Thu Aug 08, 2002 12:44 pm
by frank
btw this is the C compile line:

gcc -c kern.c
ld -o kern -Ttext 0x0 -e main kern.o
objcopy -R .note -R .comment -S -O binary kern kern.bin
dd if=kern.bin seek=1 of=/dev/ad2

Re:printing a char in C

Posted: Thu Aug 08, 2002 1:11 pm
by frank
gotta make more sure I get there...
tried to turn on the keyboard lets like this:

mov dx,0x60
mov al,1
out dx,al
mov al,2
out dx,al
mov al,3
out dx,al
But that doesn't work.. (tried this in boot.asm, and I don't have a floppydrive (its easier to turn that on:)) )

Re:printing a char in C

Posted: Thu Aug 08, 2002 1:31 pm
by frank
hmmm it does not :(
(Tried it at 0x0 and 0x1)
I think the jump is wrong... does anybody know what's wrong with it?

And em, I read on the internet that linux and windows are v86 (is that realmode?) os-es, is that right?

Re:printing a char in C

Posted: Thu Aug 08, 2002 1:35 pm
by frank
hmm linux is a protected mode os...

anyway, gotta find out whats wrong with the source...
any ideas/help?

Re:printing a char in C

Posted: Thu Aug 08, 2002 1:52 pm
by frank
good it works :)
didn't change much trough..

this little asm code:
---------------------------
bits 32
jmp main
main: int 0x10
times 512-($-$$) db 0
----------------------
It reboots (since pmode does not understand int 0x10 :), and it does.. (before it hanged)

Re:printing a char in C

Posted: Thu Aug 08, 2002 2:10 pm
by frank
hmm I can't let the C code reboot...

tried this:
asm("int $0x10");
and nothing without a forloop, both doesn't work...
it keeps hanging...

and I still can't output a char...

btw it's loaded at 0x0:0x0 (in realmode segment)

Re:printing a char in C

Posted: Thu Aug 08, 2002 2:34 pm
by frank
hmm its kinda strange...

btw what that c code in assembly? (just for testing)
(anyway, I'm no sure it booted my assembly kernel)

mov bx,0B800h
mov es,bx
mov byte[es:0],'f'
That does not work....

(it reboots)