Viitor_cc65/usr/share/doc/cc65/webdoc/ca65-14.html
kueller 223cc6685e Neue Version V963
git-svn-id: svn://svn.compuextreme.de/Viitor/V963/Viitor_cc65@5933 504e572c-2e33-0410-9681-be2bf7408885
2011-01-03 10:48:06 +00:00

134 lines
3.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.20">
<TITLE>ca65 Users Guide: Structs and unions</TITLE>
<LINK HREF="ca65-15.html" REL=next>
<LINK HREF="ca65-13.html" REL=previous>
<LINK HREF="ca65.html#toc14" REL=contents>
</HEAD>
<BODY>
<A HREF="ca65-15.html">Next</A>
<A HREF="ca65-13.html">Previous</A>
<A HREF="ca65.html#toc14">Contents</A>
<HR>
<H2><A NAME="structs"></A> <A NAME="s14">14.</A> <A HREF="ca65.html#toc14">Structs and unions</A></H2>
<H2><A NAME="ss14.1">14.1</A> <A HREF="ca65.html#toc14.1">Overview</A>
</H2>
<P>Structs and unions are special forms of
<A HREF="ca65-6.html#scopes">scopes</A>. They
are to some degree comparable to their C counterparts. Both have a list of
members. Each member allocates storage and may optionally have a name, which,
in case of a struct, is the offset from the beginning and, in case of a union,
is always zero.</P>
<H2><A NAME="ss14.2">14.2</A> <A HREF="ca65.html#toc14.2">Declaration</A>
</H2>
<P>Here is an example for a very simple struct with two members and a total size
of 4 bytes:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
.struct Point
xcoord .word
ycoord .word
.endstruct
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>A union shares the total space between all its members, its size is the same
as that of the largest member.</P>
<P>A struct or union must not necessarily have a name. If it is anonymous, no
local scope is opened, the identifiers used to name the members are placed
into the current scope instead.</P>
<P>A struct may contain unnamed members and definitions of local structs. The
storage allocators may contain a multiplier, as in the example below:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
.struct Circle
.struct Point
.word 2 ; Allocate two words
.endstruct
Radius .word
.endstruct
</PRE>
</CODE></BLOCKQUOTE>
</P>
<H2><A NAME="ss14.3">14.3</A> <A HREF="ca65.html#toc14.3">The <CODE>.TAG</CODE> keyword</A>
</H2>
<P>Using the
<A HREF="ca65-10.html#.TAG">.TAG</A> keyword, it is possible to reserve space
for an already defined struct or unions within another struct:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
.struct Point
xcoord .word
ycoord .word
.endstruct
.struct Circle
Origin .tag Point
Radius .byte
.endstruct
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Space for a struct or union may be allocated using the
<A HREF="ca65-10.html#.TAG">.TAG</A> directive.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
C: .tag Circle
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Currently, members are just offsets from the start of the struct or union. To
access a field of a struct, the member offset has to be added to the address
of the struct itself:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
lda C+Circle::Radius ; Load circle radius into A
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>This may change in a future version of the assembler.</P>
<H2><A NAME="ss14.4">14.4</A> <A HREF="ca65.html#toc14.4">Limitations</A>
</H2>
<P>Structs and unions are currently implemented as nested symbol tables (in fact,
they were a by-product of the improved scoping rules). Currently, the
assembler has no idea of types. This means that the
<A HREF="ca65-10.html#.TAG">.TAG</A> keyword will only allocate space. You won't be able to initialize
variables declared with
<A HREF="ca65-10.html#.TAG">.TAG</A>, and adding an embedded
structure to another structure with
<A HREF="ca65-10.html#.TAG">.TAG</A> will not make
this structure accessible by using the '::' operator.</P>
<HR>
<A HREF="ca65-15.html">Next</A>
<A HREF="ca65-13.html">Previous</A>
<A HREF="ca65.html#toc14">Contents</A>
</BODY>
</HTML>