<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <!--Converted with LaTeX2HTML 2008 (1.71) original version by: Nikos Drakos, CBLU, University of Leeds * revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan * with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --> <HTML> <HEAD> <TITLE>3.6 Absolute Addressing</TITLE> <META NAME="description" CONTENT="3.6 Absolute Addressing"> <META NAME="keywords" CONTENT="sdccman"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <META NAME="Generator" CONTENT="LaTeX2HTML v2008"> <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> <LINK REL="STYLESHEET" HREF="sdccman.css"> <LINK REL="next" HREF="node65.html"> <LINK REL="previous" HREF="node62.html"> <LINK REL="up" HREF="node37.html"> <LINK REL="next" HREF="node65.html"> </HEAD> <BODY > <!--Navigation Panel--> <A NAME="tex2html1640" HREF="node65.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1634" HREF="node37.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1628" HREF="node63.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1636" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1638" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1641" HREF="node65.html">3.7 Parameters & Local</A> <B> Up:</B> <A NAME="tex2html1635" HREF="node37.html">3. Using SDCC</A> <B> Previous:</B> <A NAME="tex2html1629" HREF="node63.html">3.5.1 Binary constants</A> <B> <A NAME="tex2html1637" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1639" HREF="node191.html">Index</A></B> <BR> <BR> <!--End of Navigation Panel--> <H1><A NAME="SECTION00460000000000000000"></A><A NAME="1527"></A> <BR> 3.6 Absolute Addressing </H1> <P> Data items can be assigned an absolute address with the <I>at<A NAME="1528"></A><A NAME="1529"></A> <address></I> keyword, in addition to a storage class, e.g.: <BLOCKQUOTE> <TT>__xdata __at (0x7ffe) unsigned int chksum;</TT> </BLOCKQUOTE> In the above example the variable chksum will be located at 0x7ffe and 0x7fff of the external ram. The compiler does <I>not</I> reserve any space for variables declared in this way (they are implemented with an equate in the assembler). Thus it is left to the programmer to make sure there are no overlaps with other variables that are declared without the absolute address. The assembler listing file (.lst<A NAME="1535"></A>) and the linker output files (.rst<A NAME="1536"></A>) and (.map<A NAME="1537"></A>) are good places to look for such overlaps. <P> If however you provide an initializer<A NAME="1538"></A> actual memory allocation will take place and overlaps will be detected by the linker. E.g.: <BLOCKQUOTE> <TT>__code __at (0x7ff0) char Id[5] = ''SDCC'';</TT> </BLOCKQUOTE> In the above example the variable Id will be located from 0x7ff0 to 0x7ff4 in code memory. <P> In case of memory mapped I/O devices the keyword <I>volatile</I> has to be used to tell the compiler that accesses might not be removed: <BLOCKQUOTE> <TT>volatile<A NAME="1545"></A> __xdata<A NAME="1546"></A> __at<A NAME="1547"></A> (0x8000) unsigned char PORTA_8255;</TT> </BLOCKQUOTE> For some architectures (mcs51) array accesses are more efficient if an (xdata/far) array<TT><FONT SIZE="-1"><A NAME="1549"></A></FONT></TT> starts at a block (256 byte) boundary<A NAME="1550"></A> (section <A HREF="node82.html#sub:A-Step-by_Assembler_Introduction">3.13.1</A> has an example). <BR> Absolute addresses can be specified for variables in all storage classes, e.g.: <BLOCKQUOTE> <TT>__bit<A NAME="1553"></A> __at<A NAME="1554"></A> (0x02) bvar;</TT> </BLOCKQUOTE> The above example will allocate the variable at offset 0x02 in the bit-addressable space. There is no real advantage to assigning absolute addresses to variables in this manner, unless you want strict control over all the variables allocated. One possible use would be to write hardware portable code. For example, if you have a routine that uses one or more of the microcontroller I/O pins, and such pins are different for two different hardwares, you can declare the I/O pins in your routine using: <BLOCKQUOTE> <TT>extern volatile<A NAME="1557"></A> __bit MOSI; /* master out, slave in */</TT> <BR><TT>extern volatile __bit MISO; /* master in, slave out */</TT> <BR><TT>extern volatile __bit MCLK; /* master clock */</TT> <BR> <BR><TT>/* Input and Output of a byte on a 3-wire serial bus.</TT> <BR><TT> If needed adapt polarity of clock, polarity of data and bit order</TT> <BR><TT> */</TT> <BR><TT>unsigned char spi_io(unsigned char out_byte) </TT> <BR><TT>{ </TT> <BR><TT> unsigned char i=8;</TT> <BR><TT> do { </TT> <BR><TT> MOSI = out_byte & 0x80; </TT> <BR><TT> out_byte <<= 1;</TT> <BR><TT> MCLK = 1; </TT> <BR><TT> /* __asm nop __endasm; */ /* for slow peripherals */</TT> <BR><TT> if(MISO) </TT> <BR><TT> out_byte += 1; </TT> <BR><TT> MCLK = 0; </TT> <BR><TT> } while(-i);</TT> <BR><TT> return out_byte; </TT> <BR><TT>}</TT> </BLOCKQUOTE> Then, someplace in the code for the first hardware you would use <BLOCKQUOTE> <TT>__bit __at<A NAME="1587"></A><A NAME="1588"></A> (0x80) MOSI; /* I/O port 0, bit 0 */</TT> <BR><TT>__bit __at (0x81) MISO; /* I/O port 0, bit 1 */</TT> <BR><TT>__bit __at (0x82) MCLK; /* I/O port 0, bit 2 */</TT> </BLOCKQUOTE> Similarly, for the second hardware you would use <BLOCKQUOTE> <TT>__bit __at (0x83) MOSI; /* I/O port 0, bit 3 */</TT> <BR><TT>__bit __at (0x91) MISO; /* I/O port 1, bit 1 */</TT> <BR><TT>__bit<A NAME="1601"></A> __at (0x92) MCLK; /* I/O port 1, bit 2 */</TT> </BLOCKQUOTE> and you can use the same hardware dependent routine without changes, as for example in a library. This is somehow similar to sbit, but only one absolute address has to be specified in the whole project. <P><P> <BR> <P> <HR> <!--Navigation Panel--> <A NAME="tex2html1640" HREF="node65.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1634" HREF="node37.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1628" HREF="node63.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1636" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1638" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1641" HREF="node65.html">3.7 Parameters & Local</A> <B> Up:</B> <A NAME="tex2html1635" HREF="node37.html">3. Using SDCC</A> <B> Previous:</B> <A NAME="tex2html1629" HREF="node63.html">3.5.1 Binary constants</A> <B> <A NAME="tex2html1637" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1639" HREF="node191.html">Index</A></B> <!--End of Navigation Panel--> <ADDRESS> 2011-03-20 </ADDRESS> </BODY> </HTML>