How to print a message in protected mode

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

How to print a message in protected mode

Post by beyondsociety »

I originaly posted this same topic a long time ago, but anyways I back. I have entered protected mode and am finally ready to print a message in protected mode.

I don't know where to start. Could I use the original print code but change it to 32 bit code?

IF anybody could help me out I would appreciate it, either some links or some examples in assembly would really help.
dronkit

Re:How to print a message in protected mode

Post by dronkit »

You have several options.
1) Switch from pmode to rmode and use your old *16 BIT* routine that calls BIOS.
2) Implement your own console driver, wrting to 0xB800
3) use V86 or unreal mode and call BIOS.

either way, try to stay out of mixing 16/32 bit codes unless you know what you're doing.

some C example code (i think i took it from some other post here):

#define MSG "Hello, world"

int main(void)
{
char *str = MSG, *ch;
unsigned short *vidmem = (unsigned short*) 0xb8000;
unsigned i; for (ch = str, i = 0; *ch; ch++, i++)
vidmem = (unsigned char) *ch | 0x0700;
for (;;)
;

}
beyondsociety

Re:How to print a message in protected mode

Post by beyondsociety »

Examples should be in assembly and not c, c+ or c++.

Could you re-write your high-level language code in assembly.
beyondsociety

Re:How to print a message in protected mode

Post by beyondsociety »

How would I implement my own console driver for the video?

I am not ready to use v86 mode and I could use unreal mode but it would be like switching to real mode and back everytime I wanted to print a message?

Is there another way?
dronkit

Re:How to print a message in protected mode

Post by dronkit »

You didn't say anything specific about it, so I couldn't know what were you looking for. I guess any example is better than no examples at all, right?

anyway, the example is pretty easy to understand for yourself.

If you know how addressing modes work in assembly + how to make a simply loop, it is easy.

otherwise just grab gdb ;)

what you need to know is that the video buffer for a 80x25 console is at 0xB8000 and you can poke characters directly there (one byte for the character and one byte as an attribute character), there are plenty of examples on the web.

good luck
dronkit

Re:How to print a message in protected mode

Post by dronkit »

and you WILL need to change to real mode unless you "roll-your-own" video/ide/all-32-bits drivers
beyondsociety

Re:How to print a message in protected mode

Post by beyondsociety »

By the way, what type of addressing mode are you using in your example code?
dronkit

Re:How to print a message in protected mode

Post by dronkit »

You can do this using indirect mode if you INC your counter starting from 0xB800 and then MOVe the content there.

Or, the most probable if you compile this with gcc, a base-offset displacement, like:
/*
* dl = the byte you want to write
* bx = your last cursor location
*/
movw $0xB800, %ax
pushw %ax
popw %es
movw %dl, %es:(%bx)
inc %bx
movw %dl, %es:(%bx)

you could write something like 'movw %dl, %esN(%bx)
where 'N' is a displacement withing that base-offset

see:
http://www.gnu.org/manual/gas/html_chapter/as_16.html

anyway, the syntax depends on your asm compiler. at&t is better (for me) because you can play with parameters like base index and scale easier.
beyondsociety

Re:How to print a message in protected mode

Post by beyondsociety »

I using the Intel syntax and nasm as my assember. You are using AT&t syntax so I can't really use your examples. Thanks for the help anyways.

I have re-written it in intel syntax, heres what it looks like in nasm:

mov 0xB800,ax
push ax
pop es
mov dl,es:bx
inc bx
mov dl,es:bx
dronkit

Re:How to print a message in protected mode

Post by dronkit »

one of the major differences between at&t and intel syntax is the order
of the operands, so:

mov $0x0, %ax

becomes

mov ax, 0

now for indirect mode addressing in intel syntax:

mov byte [es:bx], dl

versus the at&t syntax

movb %dl, %es:(%bx)

remember that this code is 16 bits!! also remember that you should write 2-byte words instead of bytes because of the additional attribute
byte.

regards
Whatever5k

Re:How to print a message in protected mode

Post by Whatever5k »

beyondsociety

Re:How to print a message in protected mode

Post by beyondsociety »

[attachment deleted by admin]
Slasher

Re:How to print a message in protected mode

Post by Slasher »

hi,
first your video pointer should be
video ptr=0xb8000 - segment base
segment baze = 0 video ptr = 0xb8000
segment baze = X video ptr = 0xb8000 - X

so if your sements are not zero based then you have to do this
also change

mov eax,[esi]      ; String char to AL
   lea esi,[esi+1]    ; Advance one byte into string
   cmp al,0
   jne dochar      ; Else, we're done
   add byte [ypos],1   ; Down one row
   mov byte [xpos],0   ; Back to left
   ret
to
mov eax,[esi]      ; String char to AL
inc esi    ; Advance one byte into string
mov al,[esi]   
cmp al,0
jne dochar      ; Else, we're done
add byte [ypos],1   ; Down one row
mov byte [xpos],0   ; Back to left
ret
...
...
...
see if that helps :)
beyondsociety

Re:How to print a message in protected mode

Post by beyondsociety »

[attachment deleted by admin]
Post Reply