I have the following test code:
Code: Select all
int printint(int i);
struct test{
int i;
};
void f(struct test *t)
{
printint(t->i);
}
void f2(struct test t)
{
printint(t.i);
}
struct test t1;
int main()
{
struct test t2;
t1.i = 9;
t2.i = 5;
f(&t1);
f(&t2);
f2(t1);
printint(t2.i);
__asm__("jmp .");
}
My os is using 32 bit protected mode. The code snippet which is changing to protected mode:
Code: Select all
.code16
.text
.globl _start
_start:
...
/*some code to enable a20 line and to detect the memory*/
...
xor %ax,%ax
movw %ax,%es
movw %ax,%si
mov %ax,%ss
mov %ax,%ds
mov %ax,%gs
jmp switsching_to_protected
/* base address of code and data segment is 0x0
limit for code and data segment is 0xfffff
code segment access byte: 0b10011010
data segment access byte: 0b10010010
stack base: 0xAC00
stack limit: 0x05
stack segment access byte: 0b10010110
the granuality and size flag is set for all the segments*/
.equ STACK_BASE, 0xAC00
.equ STACK_LIMIT, 0x05
StartOfGDT:
zerodescriptor:
.quad 0
OScode:
.quad 0x00cf9a000000ffff
OSdata:
.quad 0x00cf92000000ffff
OSstack:
.quad 0x00C09600AC000005
GDTend:
GDTdescriptor:
.word 0x1f
.int StartOfGDT
switsching_to_protected:
cli
lgdt (GDTdescriptor)
mov %cr0,%eax
or $0x1,%ax
mov %eax,%cr0
jmp $8,$init_protected
ret
.code32
init_protected:
mov $0x10,%ax
mov %ax,%ds
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
mov $0x18,%ax
mov %ax,%ss
mov $STACK_BASE,%ebx
movl $STACK_LIMIT,%ecx
movl $4096, %eax
mull %ecx
addl %ebx,%eax
sub $16,%eax
mov %eax,%esp
call main
jmp .
Andrej