AlexKuz wrote:
This code doesn't work:
Code:
unsigned short* vmem = (unsigned short*)0xb8000;
void lmain()
{
for (unsigned int i = 0; i < 80 * 24; i++)
vmem[i] = 0;
while (1);
}
But this code works perfectly:
Code:
unsigned short* vmem;
void lmain()
{
vmem = (unsigned short*)0xb8000;
for (unsigned int i = 0; i < 80 * 24; i++)
vmem[i] = 0;
while (1);
}
I disagree with the term "it works perfectly". The first set, specifically:
Code:
unsigned short* vmem = (unsigned short*)0xb8000;
Places the value of 0xB8000 at offset [vmem] within your binary file.
Then when you do:
Code:
vmem[i] = 0;
The processor does:
Code:
mov DS:[vmem+i*2], 0
However, DS may not be where you think it is.
Now, the reason why you think
Code:
vmem = (unsigned short*)0xb8000;
works perfectly is because the code is actually using a word sized memory position, somewhere in DS:, probably not where you think vmem is, even though the code looks correct.
Without looking any further, I would almost guaranty that you do not have your segments/selectors set up correctly. i.e.: DS doesn't pointer where you think it does at the start of your C code.
Compile it as is, but place
Code:
_asm xchg bx,bx // or what ever your compiler needs
at the beginning. Then load it into the Bochs Debugger and hit 'c'ontinue. The code will run until it hits that instruction. Then you can see what the DS, ES, CS, etc. segment/selectors are.
If you don't use Bochs, make your emulator of choice crash or have an infinite loop, then stop the emulator. The emulator should print out the segment register values at end of emulation.
Anyway, some how find out what these values are and you will find out they are not what you think they should be.
Ben