Yahoo Groups archive

AVR-Chat

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

Thread

RE: [AVR-Chat] Re: Mixing C and assembly with special requirements.

RE: [AVR-Chat] Re: Mixing C and assembly with special requirements.

2012-03-29 by Tim Mitchell

----Original Message----
Show quoted textHide quoted text
From: AVR-Chat@yahoogroups.com
[mailto:AVR-Chat@yahoogroups.com] On Behalf Of Don Kinzer
Sent: 28 March 2012 19:17 To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Re: Mixing C and assembly with special
requirements. 

> --- In AVR-Chat@yahoogroups.com, "Steve Hodge"
> <steve@...> wrote: 
> > what do you do to pass C arrays and C structs?
> The assembly knows nothing about C structures or even
> simple C types.  Consequently, you have to write the code
> manually to index arrays and access structure elements. 
> I've posted a simple example available at the link below.
>

You can get into trouble quickly here as you have to know exactly how the compiler has stored the structs in RAM. The map file can be very helpful in working out what it has done. 

 
> If you're going to write C-callable subroutines in
> assembly language, you'll need to read the avr-libc FAQ
> entry describing which registers may be modified and
> which must be preserved.   
> 
> http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage


That's a good page... it's (to my mind) slightly vague on how function parameters are passed - if passing chars it uses r24, r22, r20 down to r8. If passing 16 bit parameters it uses r25:r24 (r25 is high byte), r23:r22 etc. For 32 bit it uses r25:24:23:22
Obviously for asm interrupt routines you have to save and restore everything.


-- 
Tim Mitchell

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

RE: [AVR-Chat] Re: Mixing C and assembly with special requirements.

2012-03-29 by Steve Hodge

Thanks for the help, tips and pitfalls to watch for.  I've looked at the
links and I think I have it more or less figured out.   Steve
Show quoted textHide quoted text
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of Tim Mitchell
Sent: Thursday, March 29, 2012 1:29 AM
To: AVR-Chat@yahoogroups.com
Subject: RE: [AVR-Chat] Re: Mixing C and assembly with special requirements.

 

  

----Original Message----
From: AVR-Chat@yahoogroups.com <mailto:AVR-Chat%40yahoogroups.com> 
[mailto:AVR-Chat@yahoogroups.com <mailto:AVR-Chat%40yahoogroups.com> ] On
Behalf Of Don Kinzer
Sent: 28 March 2012 19:17 To: AVR-Chat@yahoogroups.com
<mailto:AVR-Chat%40yahoogroups.com> 
Subject: [AVR-Chat] Re: Mixing C and assembly with special
requirements. 

> --- In AVR-Chat@yahoogroups.com <mailto:AVR-Chat%40yahoogroups.com> ,
"Steve Hodge"
> <steve@...> wrote: 
> > what do you do to pass C arrays and C structs?
> The assembly knows nothing about C structures or even
> simple C types. Consequently, you have to write the code
> manually to index arrays and access structure elements. 
> I've posted a simple example available at the link below.
>

You can get into trouble quickly here as you have to know exactly how the
compiler has stored the structs in RAM. The map file can be very helpful in
working out what it has done. 

> If you're going to write C-callable subroutines in
> assembly language, you'll need to read the avr-libc FAQ
> entry describing which registers may be modified and
> which must be preserved. 
> 
> http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage

That's a good page... it's (to my mind) slightly vague on how function
parameters are passed - if passing chars it uses r24, r22, r20 down to r8.
If passing 16 bit parameters it uses r25:r24 (r25 is high byte), r23:r22
etc. For 32 bit it uses r25:24:23:22
Obviously for asm interrupt routines you have to save and restore
everything.

-- 
Tim Mitchell





[Non-text portions of this message have been removed]

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.