avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Local data & stack pointer questions


From: Theodore A. Roth
Subject: Re: [avr-gcc-list] Local data & stack pointer questions
Date: Sun, 21 Apr 2002 16:24:52 -0600

On Sun, 21 Apr 2002 23:25:25 +0200
"Marko Panger" <address@hidden> wrote:

> Hello all !
> 
> I was wondering If GCC uses Y pointer alwasy as a stack frame pointer
> for data which is allocated locally in a function (with or without
> "naked" attribute) ?
> 
> 
> Example:
> 
> void Test(void) __attribute__ ((naked));
> 
> void Test(void)
> {
>     char a;
> 
>     a =  10;
> }

Looking at the output of:

  $ avr-objdump -h -S -D --disassemble-zeroes messy.elf > messy.lst

without "naked" gives:

  00000340 <Test>:

  /*  void Test(void) __attribute__ ((naked)); */

  void Test(void)
  {
   340:   cf 93           push    r28
   342:   df 93           push    r29
   344:   cd b7           in  r28, 0x3d   ; 61
   346:   de b7           in  r29, 0x3e   ; 62
   348:   21 97           sbiw    r28, 0x01   ; 1
   34a:   0f b6           in  r0, 0x3f    ; 63
   34c:   f8 94           cli
   34e:   de bf           out 0x3e, r29   ; 62
   350:   0f be           out 0x3f, r0    ; 63
   352:   cd bf           out 0x3d, r28   ; 61
      char a;

      a =  10;
   354:   8a e0           ldi r24, 0x0A   ; 10
   356:   89 83           std Y+1, r24    ; 0x01
  }
   358:   21 96           adiw    r28, 0x01   ; 1
   35a:   0f b6           in  r0, 0x3f    ; 63
   35c:   f8 94           cli
   35e:   de bf           out 0x3e, r29   ; 62
   360:   0f be           out 0x3f, r0    ; 63
   362:   cd bf           out 0x3d, r28   ; 61
   364:   df 91           pop r29
   366:   cf 91           pop r28
   368:   08 95           ret

And with "naked" gives:

  00000340 <Test>:
  void Test(void)
  {
      char a;

      a =  10;
   340:   8a e0           ldi r24, 0x0A   ; 10
   342:   89 83           std Y+1, r24    ; 0x01

  00000344 <main>:
  }

You can see that lines 340-346 in the without "naked" case are pushing the
Frame Pointer onto the stack and putting the new stack pointer in the
Frame Pointer (gcc uses Y for the Frame Pointer).

With "naked", there is no tracking of the Frame Pointer.

Also, notice that there is no 'ret' from your Test() function. That scares
me since I see this when I call Test() in main():

      Test();
   490:   0e 94 a0 01     call    0x340

Since your Test() does not have a 'ret' instruction, you will hose your
stack by calling Test(). The 'call' instruction will push PC onto the
stack and you need the 'ret' to pop it back off and resume execution after
the 'call'.

Ted Roth
avr-gcc-list at http://avr1.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]