[BITS 32] extern currentProcess extern systemtss ; right now stack looks like ; processPtr ; esp-->returnAddress ; ; align 16 ; void switchTo(process_t *p) global switchTo switchTo: pop eax ; save return address mov ebx, [currentProcess] cmp dword [ebx+8], 1 ; are we switching from a user process? jne notUser push ss push esp notUser: pushfd ; push eflags register push cs ; push cs register push eax ; push return address back for iret ; stack frame (eip, cs, eflags) push es push ds push fs push gs pushad mov eax, [currentProcess] ; get pointer to current process mov [eax+0], esp ; save currents processe's stack pointer ;**if you push/pop more stuff before this, update the offset from ESP*** cmp dword [ebx+8], 1 ; are we switching from a user process? jne notUser2 mov eax, [esp+68] ; get pointer from user setup stack jmp isUser notUser2: mov eax, [esp+60] ; get pointer to process we're switching to isUser: mov [currentProcess], eax mov ebx, [systemtss] ; ebx = &systemtss mov word [ebx+8], 0x20 ; systemtss->ss0 = KernelDataSel mov ecx, [eax+12] ; ecx = process->kstackBase add ecx, 4096 mov dword [ebx+4], ecx ; systemtss->esp0 = (process->kstackBase + 4096) mov esp, [eax+0] ; restore stack pointer of new process popad pop gs pop fs pop ds pop es iret