Global Kernel Vars

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.
Post Reply
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Global Kernel Vars

Post 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
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: Global Kernel Vars

Post by -m32 »

With [ORG <offset>] perhaps?
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Global Kernel Vars

Post 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?
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Global Kernel Vars

Post by brodeur235 »

I'm assembling to elf.

Brodeur235
Post Reply