1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-23 19:29:37 +00:00

Added more sample for the inline assembler. Contributed by Steffen Görzig.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5308 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2011-12-07 21:08:14 +00:00
parent 5449f21d9f
commit 494b0619d7

View File

@ -1200,8 +1200,8 @@ the format specifier before passing the assembly code line to the backend.
<item><tt/%b/ - Numerical 8-bit value <item><tt/%b/ - Numerical 8-bit value
<item><tt/%w/ - Numerical 16-bit value <item><tt/%w/ - Numerical 16-bit value
<item><tt/%l/ - Numerical 32-bit value <item><tt/%l/ - Numerical 32-bit value
<item><tt/%v/ - Assembler name of a (global) variable or function <item><tt/%v/ - Assembler name of a global variable or function
<item><tt/%o/ - Stack offset of a (local) variable <item><tt/%o/ - Stack offset of a local variable
<item><tt/%g/ - Assembler name of a C label <item><tt/%g/ - Assembler name of a C label
<item><tt/%s/ - The argument is converted to a string <item><tt/%s/ - The argument is converted to a string
<item><tt/%%/ - The % sign itself <item><tt/%%/ - The % sign itself
@ -1229,6 +1229,52 @@ Or, to access a struct member of a static variable:
__asm__ ("lda %v,y", pixel); __asm__ ("lda %v,y", pixel);
</verb></tscreen> </verb></tscreen>
<p> <p>
The next example shows how to use global variables to exchange data between C
an assembler and how to handle assembler jumps:
<tscreen><verb>
unsigned char globalSubA, globalSubB, globalSubResult;
/* return a-b, return 255 if b>a */
unsigned char sub (unsigned char a, unsigned char b)
{
globalSubA = a;
globalSubB = b;
__asm__ ("sec");
__asm__ ("lda %v", globalSubA);
__asm__ ("sbc %v", globalSubB);
__asm__ ("bcs %g", jumpSubNoError);
__asm__ ("lda #$FF");
jumpSubNoError:
__asm__ ("sta %v", globalSubResult);
return globalSubResult;
}
</verb></tscreen>
<p>
Arrays can also be accessed:
<tscreen><verb>
unsigned char globalSquareTable[] = {
0, 1, 4, 9, 16, 25, 36, 49, 64, 81,
100, 121, 144, 169, 196, 225
};
unsigned char globalSquareA, globalSquareResult;
/* return a*a for a<16, else 255 */
unsigned char square (unsigned char a)
{
if (a>15){
return 255;
}
globalSquareA = a;
__asm__ ("ldx %v", globalSquareA);
__asm__ ("lda %v,x", globalSquareTable);
__asm__ ("sta %v", globalSquareResult);
return globalSquareResult;
}
</verb></tscreen>
<p>
Note: Do not embed the assembler labels that are used as names of global Note: Do not embed the assembler labels that are used as names of global
variables or functions into your asm statements. Code like this variables or functions into your asm statements. Code like this