User`s manual

180 digi.com Using Assembly Language
11.4.2 Embedded Assembly Example
The purpose of the following sample program, asm1.c, is to show the different ways to access stack-
based variables from assembly code.
void func(char ch, int i, long lg);
main(){
char ch;
int i;
long lg;
ch = 0x11;
i = 0x2233;
lg = 0x44556677L;
func(ch,i,lg);
}
void func(char ch, int i, long lg){
auto int x;
auto int z;
x = 0x8888;
z = 0x9999;
#asm
// This is equivalent to the C statement: x = 0x8888
ld hl, 0x8888
ld (sp+@SP+x), hl
// This is equivalent to the C statement: z = 0x9999
ld hl, 0x9999
ld (sp+@SP+z), hl
// @SP+i gives the offset of i from the stack frame on entry.
// On the Rabbit, this is how HL is loaded with the value in i.
ld hl,(sp+@SP+i)
// This works if func() is useix; however, if the IX register
// has been changed by the user code, this code will fail.
ld hl,(ix+i)
// This method works in either case because the assembler adjusts the
// constant @SP, so changing the function to nouseix with the keyword
// nouseix, or the compiler directive #nouseix will not break the code.
// But, if SP has been changed by user code, (e.g., a push) it won't work.
ld hl,(sp+@SP+lg+2)
ld b,h
ld c,L
ld hl,(sp+@SP+lg)
ex de,hl
#endasm
}