Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Mixing C and assembly with special requirements.

2012-03-29 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Tim Mitchell" <tim@...> wrote:
>[Y]ou have to know exactly how the compiler has
>stored the structs in RAM.
On a host like the AVR where there is no penalty for accessing a byte-aligned data item one generally uses the compiler option -fpack-struct to ensure that no padding is added to structure members.  With that option, computing structure member offsets is a simple matter of calculating a running total of the number of bytes used by each member - not particularly difficult.

>[The description is] slightly vague on how function
>parameters are passed [...].
Compiling an example written in C and examining the generated code is generally sufficient to clear up any remaining questions.  The FAQ doesn't describe the case of a function that returns a large (>64 bit) data item.  I filed a bug report on that:
http://savannah.nongnu.org/bugs/?35240

If you have suggestions on improving any parts of the avr-libc documentation, the Savannah site is the place to submit them.

> Obviously for asm interrupt routines you have to save
> and restore everything.
To be more precise, you need only save/restore the registers modified in the ISR (and any functions it calls).

My example showed how to index an array at run time.  If the index is known at compile time you can get the assembler to compute the address at compile/link time.  For example,

#define ELEM_IDX 5
...
ldi r31, hi8(ms + (ELEM_IDX * MYSTRUCT_SIZE)) 

You can add in the structure member offset, too if you want the address of a particular member of an array of structures.  Along the same lines, you can directly access a structure member, whether part of an array or not.

lds r24, ms + (ELEM_IDX * MYSTRUCT_SIZE) + MYSTRUCT_B_OFST

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.