Computing.Net > Forums > Programming > Local variable issues

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Local variable issues

Reply to Message Icon

Name: Birdman
Date: August 2, 2004 at 09:31:58 Pacific
OS: My own
CPU/Ram: 80x86
Comment:

So I've been working on my own operating system for some time now, and I'm just starting to be able to load some kernel modules. I have no trouble loading a program from the floppy drive and tossing it into memory, setting up an LDT..etc etc and starting the task. But the problem comes when the task wants to play with some local variables. Hmm, it'd be simpler if I'd just show you:

int main()
{
int i;
i=0;
if(i==0) return 1;
else return 0;
}

Okay. Now because Bochs emulator doesn't have instruction tracing the way I test if this assignment worked is to have the start code stub (That starts the C code) halt the system after main returns, then I just check out bochsout.txt and see what the value of EAX is (since the return loads 1 or 0 into EAX). The example above works. EAX is set to 1 when the system hangs. No problem.

But:
int main()
{
int i=0;
i++;
if(i==1) return 1;
else return 0;
}

This stuffs a 0 into EAX! Why? Now here's the rub:

int i=0;
int main()
{
i++;
if(i==1) return 1;
else return 0;
}

This works, and EAX==1!! So it only has trouble doing SOME assignments to LOCAL variables.

Next I set gcc to give me the assembly output. It seems that when doing direct assigments, gcc mov's the address of the local variable into -4(ESP), which is the offset from the stack where the local variable is stored. But when doing other assignments, such as i++, gcc does:
mov -4(ESP),EAX
incl (EAX)

It is this last kind of assingment that fails to do anything.

So, are there any ideas as to what the problem could be? The stack seems set up just fine, and other code loaded earlier in the bootloading process from the floppy runs just fine.

I'm compiling with:
gcc -c -Wall -no-builtin-functions -o file.c.o file.c

Then I assemble the stub code with:
nasm -f aout -o start.o start.asm

Then I link them with:
ld --oformat binary -{some command to put the .text in the front) -{some command to set the entry point to _start, a function in start.asm that calls main()} start.o file.c.o

Any help is appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: Birdman
Date: August 2, 2004 at 17:24:53 Pacific
Reply:

Well, I figured it out, so I guess I'll post in case anybody's interested. The problem actually was this: the C code expected DS to be loaded with SS! I realized that the asm code was doing assignments to (%eax) and also to -4(%esp). This meant that because it wasn't doing (ss:%eax), its default segment selector for eax was assumed to be set to SS. I found out through process of elimination that DS was the register being used. Then I set my start stub code to init DS to SS, and all was good.

Well, I hope that will help someone some day :)


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Local variable issues

Local Variable life span www.computing.net/answers/programming/local-variable-life-span/2272.html

Global Variable vs. Local Variable www.computing.net/answers/programming/global-variable-vs-local-variable-/3215.html

local variables in vc++ 9.0 disasm www.computing.net/answers/programming/local-variables-in-vc-90-disasm/16467.html