Download 4 Assembly Language Interface

Transcript
Assembly Language Interface 4
As a negative example, the following code is not recommended
because an interrupt could corrupt the stack.
modify(i7,1); /* NOT recommended */
r3=dm(0,i7); /* NOT recommended */
If an interrupt occurred after the modify but before the read the stack
would be pointing at valid data. The interrupt service routine would
write over this data.
4. At the beginning of an assembly language function, all compiler
registers that are used in the function must be saved. At the end of an
assembly language function, all those that were saved must be
restored.
5. Before a function call, all scratch registers that contain “live” data must
be saved. After a function call, all those scratch registers that were
saved must be restored.
4.2.1.4 Macros For Stack Access
The header file asm_sprt.h includes many macros useful for
interfacing C to assembly language. For example, the puts() macro
does a push:
puts=r7;
This instruction pushes r7 onto the stack. Similarly the gets()
macro reads off the stack. For example, to read the most recently
pushed value into register r9 , use this code:
r9=gets(1);
You can use the restore_reg macro in conjunction with the
save_reg macro to save and restore all register file registers (R0R15). The macros are found in asm_sprt.h . Use them as a templates
for constructing code for performing register saves and restores.
4–5