# Context switch
#
#   void swtch(struct context **old, struct context *new);
# 
# Save the current registers on the stack, creating
# a struct context, and save its address in *old.
# Switch stacks to new and pop previously-saved registers.

.globl swtch
swtch:
  movl 4(%esp), %eax		# mov	eax, [esp+4]	; eax = &old
  movl 8(%esp), %edx		# mov	edx, [esp+8]	; edx = new

  # Save old callee-saved registers
  pushl %ebp
  pushl %ebx
  pushl %esi
  pushl %edi

  # Switch stacks
  movl %esp, (%eax)		# mov DWORD [ eax ], esp; save old stack pointer
  movl %edx, %esp		# mov esp, edx		; set esp to new stack

  # Load new callee-saved registers
  popl %edi
  popl %esi
  popl %ebx
  popl %ebp
  ret
