Computer Science 237 |
Lecture 19: More Assembly Programming: Saving Registers, Building Memory
Date: October 26, 2005
Recall our array-based swap example:
void swap(int a[], int i, int j)
{ register int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
Our assembly translation used a0, d0, d1, d2, d3, so we could save those on the stack before we use the registers, pop off and restore after:
a = 8
i = 12
j = 14
swap: link %a6,#0
move.l %d0,-(%sp)
move.l %d1,-(%sp)
move.l %d2,-(%sp)
move.l %d3,-(%sp)
move.l %a0,-(%sp)
....code for swap from before...
move.l (%sp)+,%a0
move.l (%sp)+,%d3
move.l (%sp)+,%d2
move.l (%sp)+,%d1
move.l (%sp)+,%d0
unlk %a6
rts
Note that any order for pushing the registers works, as long as they are popped off in reverse order
swap: link %a6,#0 movem.l #0x010f,-(%sp) ... movem.l (%sp)+,#0xf080 unlk %a6 rts
#0x7fff
. To pop
all registers, less a7, you would use: #0xfffe
| win.s: safely use d1 and highlight.
move.w #0,%d1
loop: cmp.w #4,%d1
bge endloop
| highlight could destroy d1, so
| we save it on the stack:
move.w %d1,-(%sp)
| push parameters (3-i,i)
move.w %d1,-(%sp)
move.w #3,-(%sp)
sub.w %d1,(%sp)
bsr highlight
| stack contains remnants of params
| and saved d1. d1 is junk.
| pop params, and restore d1
add.l #4,%sp
move.w (%sp)+,%d1