⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cc65-9.html

📁 cc65 的编译器文档
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.20"> <TITLE>cc65 Users Guide: Inline assembler</TITLE> <LINK HREF="cc65-10.html" REL=next> <LINK HREF="cc65-8.html" REL=previous> <LINK HREF="cc65.html#toc9" REL=contents></HEAD><BODY><A HREF="cc65-10.html">Next</A><A HREF="cc65-8.html">Previous</A><A HREF="cc65.html#toc9">Contents</A><HR><H2><A NAME="inline-asm"></A> <A NAME="s9">9.</A> <A HREF="cc65.html#toc9">Inline assembler</A></H2><P>The compiler allows to insert assembler statements into the output file. Thesyntax is</P><P><BLOCKQUOTE><CODE><PRE>        asm (&lt;string literal&gt;[, optional parameters]) ;</PRE></CODE></BLOCKQUOTE>or<BLOCKQUOTE><CODE><PRE>        __asm__ (&lt;string literal&gt;[, optional parameters]) ;</PRE></CODE></BLOCKQUOTE></P><P>The first form is in the user namespace and is disabled by <CODE><A HREF="cc65-2.html#option-A">-A</A></CODE>.</P><P>The asm statement may be used inside a function and on global file level. Aninline assembler statement is a primary expression, so it may also be used aspart of an expression. Please note however that the result of an expressioncontaining just an inline assembler statement is always of type <CODE>void</CODE>.</P><P>The contents of the string literal are preparsed by the compiler and insertedinto the generated assembly output, so that the can be further processed bythe backend and especially the optimizer. For this reason, the compiler doesonly allow regular 6502 opcodes to be used with the inline assembler. Pseudoinstructions (like <CODE>.import</CODE>, <CODE>.byte</CODE> and so on) are <EM>not</EM> allowed,even if the ca65 assembler (which is used to translate the generated assemblercode) would accept them. The builtin inline assembler is not a replacement forthe full blown macro assembler which comes with the compiler.</P><P>Note: Inline assembler statements are subject to all optimizations done by thecompiler. There is currently no way to protect an inline assembler statementfrom being moved or removed completely by the optimizer. If in doubt, checkthe generated assembler output, or disable optimizations.</P><P>The string literal may contain format specifiers from the following list. Foreach format specifier, an argument is expected which is inserted instead ofthe format specifier before passing the assembly code line to the backend.</P><P><UL><LI><CODE>%b</CODE> - Numerical 8 bit value</LI><LI><CODE>%w</CODE> - Numerical 16 bit value</LI><LI><CODE>%l</CODE> - Numerical 32 bit value</LI><LI><CODE>%v</CODE> - Assembler name of a (global) variable or function</LI><LI><CODE>%o</CODE> - Stack offset of a (local) variable</LI><LI><CODE>%s</CODE> - The argument is converted to a string</LI><LI><CODE>%%</CODE> - The % sign itself</LI></UL></P><P>Using these format specifiers, you can access C <CODE>#defines</CODE>, variables orsimilar stuff from the inline assembler. For example, to load the value ofa C <CODE>#define</CODE> into the Y register, one would use</P><P><BLOCKQUOTE><CODE><PRE>        #define OFFS  23        __asm__ ("ldy #%b", OFFS);</PRE></CODE></BLOCKQUOTE></P><P>Or, to access a struct member of a static variable:</P><P><BLOCKQUOTE><CODE><PRE>        typedef struct {            unsigned char x;            unsigned char y;            unsigned char color;        } pixel_t;        static pixel_t pixel;        __asm__ ("ldy #%b", offsetof(pixel_t, color));        __asm__ ("lda %v,y", pixel);</PRE></CODE></BLOCKQUOTE></P><P>Note: Do not embedd the assembler labels that are used as names of globalvariables or functions into your asm statements. Code like this</P><P><BLOCKQUOTE><CODE><PRE>        int foo;        int bar () { return 1; }        __asm__ ("lda _foo");   /* DON'T DO THAT! */        ...        __asm__ ("jsr _bar");   /* DON'T DO THAT EITHER! */</PRE></CODE></BLOCKQUOTE></P><P>may stop working if the way, the compiler generates these names is changed ina future version. Instead use the format specifiers from the table above.</P><HR><A HREF="cc65-10.html">Next</A><A HREF="cc65-8.html">Previous</A><A HREF="cc65.html#toc9">Contents</A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -