• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Coding Gurus of GAF please help [Context Switching]

Status
Not open for further replies.

rastex

Banned
For school we're writing a simple OS for Motorola Coldfire 68k boards, and I'm responsible for context switching. My question concerns how to access c variables inside of my inline assembly. I'm also still figuring out what I should store as the Process' Program Counter when its switched out, and where it'll lie on the stack.

Anyway onto the assembly. So I have a global pointer variable to a ProcessControlBlock (PCB) struct. Now I want to assign certain members of that PCB pointer for saving when that process gets switched back in... so what's the inline assembly for that?

Do I use the member as an address or as referable data itself or as immediate data?

So I guess here are my three options
Member as address

Code:
asm("movea.l gCurrenPCB->fPC, %a0");
asm("move.l %pc, (%a0)");

Memory location directly
Code:
asm("move.l %pc, gCurrentPCB->fPC");

immediate address
Code:
asm("move.l #gCurrentPCB->fPC, %a0");
asm("move.l %pc, (%a0)");

And I guess there's one more option which is using gCurrentPCB pointer as an address and then figuring out what offset is the correct member field and setting that, and so.

Code:
asm("move.l gCurrentPCB, %a0")
asm("move.l %pc, 8(%a0)")


I'm fully aware that Motorola Coldfire boards are from the stone ages, but if anybody could help me out at all I'd be extremely grateful. I've been looking at the course documentation and trying to find information online but thus far I've been totally unlucky. Thanks in advance!
 

rastex

Banned
Ya, it is pretty cool. Except this term is absolutely INSANE in the amount of work that I have to do. This monday my group just finished up a 130 page Software Requirements and Specifications document and it's not even finished yet, we still have 1 more phase to go. Trust me that 20 pages of UML sequence diagrams are *not* fun.
 

rastex

Banned
hot damn! Just had an epiphany! (I've been waiting a couple of days for this) I don't need to store the Program Counter inside the process control block struct directly. As long as I follow a protocol I can save the context of the process on the process' stack, and just restore the context when I switch that process back in! This dramatically reduces the number of members I have in my struct and makes my code much simpler as well. BUT I really gotta make sure I follow the protocol, and I still have my inline assembly problem because I still have to store the Process' stack pointer
 

gblues

Banned
Wow, it's been ages since I've done assembly.. it was for a 6502 and we used hex numbers, not ASM (because we had to key the programs in manually on an archaic Heathkit gizmo using a glorified 10-key, not because we were l33t).

Here's how you can determine the answer to your question. You'll probably have to consult the M68K docs for the exact ASM commands, but this will give you a direction to research:

1. What command is used to set the PC?
2. What type of data does that command need (immediate data, absolute address, relative address)?
3. What type of data is gCurrentPCB->fPC? Is it a float? a short int? a long int? a pointer to one of the above?

This is going to determine what needs to be stored, which is also going to determine how you get the data back. It's like math. If you change with addition, you undo with subtraction. If you change with multiplication, you undo with division. If you store the PC by copying the value, put it back by copying it back.

I hope I'm making sense and not telling you what you already know. :(

Nathan
 

rastex

Banned
jmp or jsr are used to set the PC. Branch and stuff can be used as well, but the jump commands should be more practical I believe.

I think I have the first 2 questions covered, it's the 3rd question that is what I'm trying to figure out. What is struct->member exactly when it's converted to assembly? Right now the value is not a pointer but the value itself.

I mean, I can totally visualize what's happening inside the computer, it's something like
Code:
Memory:
|  xxxxx   |
| xxxxx+1|
|    ...       | 
| member |  <- struct pointer value
| member |
| ...          |
|              |

so if I just had PCB* pcb then [pcb] would be the address to the starting of the struct. and pcb->stackPointer would then be what?

And I'm having doubts about even putting a variable name inside of the inline assembly directly. Since the GCC Just copies the stuff in the asm command directly, I don't think it replaces the variable symbols with their respective memory addresses or values, or maybe it does. Does the assembler have access to the compiler's symbol table??
 

IJoel

Member
rastex said:
so if I just had PCB* pcb then [pcb] would be the address to the starting of the struct. and pcb->stackPointer would then be what?

pcb->stackPointer accesses the value stored in stackPointer member of the structure given the address of the structure.

It's too late... this might not even make sense... or I'm not understanding you. :p
 

rastex

Banned
From a high-level language view that's true, but looking at the low-level it's a bit more complicated.

something like
int i = pcb->stackPointer;

will get compiled to a few lines of assembly code because everything is accessed by a memory address. Getting the actual value is just another assembly instruction to access the value pointed at by the memory location.

Anyway, I finally got hold of someone who took this course a couple of years ago and she said that their group used functions as a hack. Turns out that GCC places parameters on the stack and returns values in register d0. Soooo... if I place the memory address of the member as a return value for a helper function then I'll have access to that specific memory address with register d0. It's ugly, but it should work.
 

IJoel

Member
rastex said:
From a high-level language view that's true, but looking at the low-level it's a bit more complicated.

something like
int i = pcb->stackPointer;

will get compiled to a few lines of assembly code because everything is accessed by a memory address. Getting the actual value is just another assembly instruction to access the value pointed at by the memory location.

Anyway, I finally got hold of someone who took this course a couple of years ago and she said that their group used functions as a hack. Turns out that GCC places parameters on the stack and returns values in register d0. Soooo... if I place the memory address of the member as a return value for a helper function then I'll have access to that specific memory address with register d0. It's ugly, but it should work.

Arrr... I see what you mean...

Anyway, you can use extended ASM to specify the registers that you want to contain the specific data you need for the inline code. Here's a reference, though it seems you found a solution. It's good knowledge anyway:

http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
 
Status
Not open for further replies.
Top Bottom