The stack and local variables in C function (MIPS)

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
Passworder
Posts: 4
Joined: Wed Apr 14, 2010 3:37 am

The stack and local variables in C function (MIPS)

Post by Passworder »

Hi
I've been messing around with some bugs for weeks now and I think I found out what is causing it. However I need some help in solving it, I have no idea how to...

Right now all my processes are represented by a function (which is the program it runs). Most things seems to work, hard to say when a big flaw is in the program... but I found out that the local variables of the program functions aren't saved when context switching. I'm fairly sure that it has something to do with the stack, everything else seems ok. It's just that the local variables aren't preserved.

Here's an example of a process:

Code: Select all

Process A(char *arg_str) {
    char str[5];
    int i;
    for (i=0;i<5;i++) str[i] = arg_str[i];

    while (1) {
        // ...
        printString(str);
        // ...
    }
}
Right now, it works if I print for example the argument string 'arg_str'. But when I use the local string variable 'str', it only works the first time. When it switches back from another process its value is gone though.

So how can I save all local variables so I can use them like normal the next time it switches to this process? The interrupt handler is ok, I save all hardware registers and restore them. The thing i'm a little unsure about is when creating a process. This is how I initialize the regs for a PCB when creating a PCB.

...
PCB.REGS.EPC = Function pointer;
PCB.REGS.SP = Stack;

Where Stack is a char[0x9000]. I'm using MIPS by the way.
Thankful for help.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: The stack and local variables in C function (MIPS)

Post by Combuster »

Use stack switching - don't let programs share the same stack. That way you can just store all registers, and when you return all other context hasn't changed.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply