-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdo-switch.s
More file actions
63 lines (52 loc) · 1.62 KB
/
do-switch.s
File metadata and controls
63 lines (52 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* file: do-switch.s
* description: assembly code to switch stack
* class: CS 7600, Spring 2013
*/
/*
* do_switch - save stack pointer to *location_for_old_sp, set
* stack pointer to 'new_value', and return.
* Note that the return takes place on the new stack.
*
* do_switch(int **location_for_old_sp, int *new_value)
* location_for_old_sp = *(SP+4) or *(FP+8)
* new_value = *(SP+8) or *(FP+12)
*
* For more details, see:
* http://pdos.csail.mit.edu/6.828/2004/lec/l2.html, GCC calling conventions
*/
do_switch:
/* frame pointer - makes life easier in assembler, too
*/
push %ebp
mov %esp,%ebp
/* debugging support - the last value pushed before switching
* is a flag; check for that here and halt *before* switching
* so you have a chance to debug
*/
mov 12(%ebp),%eax /* new_value */
cmpl $0xA5A5A5A5,(%eax) /* flag value there? */
je ok /* yes - skip */
mov $0,%eax
mov 0(%eax),%eax /* no - simple assert */
ok:
/* C calling conventions require that we preserve %ebp (already
* saved above), %ebx, %esi, and %edi - push them onto the stack
*/
push %ebx
push %esi
push %edi
push $0xA5A5A5A5 /* push the flag value */
cmpl $0,8(%ebp) /* is 'location_for_old' null? */
je skip
mov 8(%ebp),%eax /* no - save current stack pointer */
mov %esp,(%eax) /* into 'location_for_old' */
skip:
mov 12(%ebp),%esp /* switch */
pop %eax /* pop flag and ignore it */
pop %edi /* pop callee-save registers */
pop %esi
pop %ebx
pop %ebp /* and the frame pointer */
ret /* and return */
.global do_switch