Page 1 of 1

Global Kernel Vars

Posted: Tue Aug 04, 2009 10:21 pm
by brodeur235
Okay, another problem arose while I was porting my kernel from 1 module, to multiple modules. The issue:

When I declare a variables in my kernel, such as these (for example):
dd cursor_x
dd cursor_y

The locations of these variables when the kernel is run will not be at "cursor_x" ( or the location that nasm will change that to), because that is their location in the disk image. Their location in memory will be:

( ( cursor_x - k_main ) + kernel_offset )

Where kernel_offset is where the kernel was loaded into memory and k_main is the location of the first kernel byte. I'm not sure If I've explained this well enough already, or I've not been specific enough so I'll provide an example, just to make sure...

Say my kernel is loaded to 0x000B0000, and this is my kernel:

Code: Select all

[BITS 32]
k_main:
cursor_x dd 0x00000000
cursor_y dd 0x00000004
Granted, this will accomplish nothing, but it's for example's sake. The location of the global variables cursor_x and cursor_y are 0 and 4 respectively, on disk. However, in memory, they will be 0x000B0000 and 0x000B0004, respectively.

Now to address either of these variables, I have to say:
DWORD [ ( ( cursor_x - k_main ) + 0x000B0000 ) ]
and
DWORD [ ( ( cursor_y - k_main ) + 0x000B0000 ) ]

Nasm had no problem assembling addresses this way, if it's in the same module as the k_main label. However, if I try to address a variable this way from another module, in which k_main is defined as external, I get the following error:

Code: Select all

./../sources/kernel/k_screen/k_screen.asm:30: error: beroset-p-650-invalid effective address
And with thousands of lines of code... I get many of these errors... About 50. How does everybody else solve this problem? It must be a common issue...

Help appreciated,

Brodeur235

Re: Global Kernel Vars

Posted: Wed Aug 05, 2009 5:00 am
by -m32
With [ORG <offset>] perhaps?

Re: Global Kernel Vars

Posted: Wed Aug 05, 2009 7:51 am
by NickJohnson
You shouldn't be loading your kernel around that address, especially if it has thousands of lines of code. The EBDA can potentially be anywhere between 0x80000 and 0xF0000, and if you overwrite it, the BIOS will not be happy. I'd recommend loading at 0x100000 (or at least more like 0xFC00), above lower memory - you have a guarantee of at least 3MB of extended memory on anything above a 386. This won't fix your current problem, but could definitely cause you trouble in the future.

What sort of executable format are you using?

Re: Global Kernel Vars

Posted: Wed Aug 05, 2009 1:41 pm
by brodeur235
I'm assembling to elf.

Brodeur235