Pages: [1] 2   Go Down
Author Topic: How can one view the assembly code output from the Arduino IDE?  (Read 5038 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, the subject line tells it all, but I will add that I have searched the forum and cannot find a way to view the assembly code output from the Arduino IDE.  In fact, I cannot find any output files from running Arduino.  It looks as though there are no "tracks" left behind from uploading a sketch to a board.
Logged

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you want it? It is completely unreadable and in HEX.

for this blinking file:
Code:
/*
    5-10-07
    Copyright Spark Fun Electronics© 2007
    Nathan Seidle
    nathan at sparkfun.com
   
    ATmega168

Example Blink
Toggles all IO pins at 1Hz
*/

#include <avr/io.h>

//Define functions
//======================
void ioinit(void);      //Initializes IO
void delay_ms(uint16_t x); //General purpose delay
//======================

int main (void)
{
    ioinit(); //Setup IO pins and defaults

    while(1)
    {
PORTC = 0xFF;
PORTB = 0xFF;
PORTD = 0xFF;
delay_ms(500);

PORTC = 0x00;
PORTB = 0x00;
PORTD = 0x00;
delay_ms(500);
    }
   
    return(0);
}

void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0b11111111; //All outputs
    DDRC = 0b11111111; //All outputs
    DDRD = 0b11111110; //PORTD (RX on PD0)
}

//General short delays
void delay_ms(uint16_t x)
{
  uint8_t y, z;
  for ( ; x > 0 ; x--){
    for ( y = 0 ; y < 90 ; y++){
      for ( z = 0 ; z < 6 ; z++){
        asm volatile ("nop");
      }
    }
  }
}

You get
Code:
:100000000C9434000C944F000C944F000C944F004F
:100010000C944F000C944F000C944F000C944F0024
:100020000C944F000C944F000C944F000C944F0014
:100030000C944F000C944F000C944F000C944F0004
:100040000C944F000C944F000C944F000C944F00F4
:100050000C944F000C944F000C944F000C944F00E4
:100060000C944F000C944F0011241FBECFEFD4E02E
:10007000DEBFCDBF11E0A0E0B1E0E8EFF0E002C0EC
:1000800005900D92A030B107D9F711E0A0E0B1E0E2
:1000900001C01D92A030B107E1F70C9467000C94E9
:1000A00000008FEF84B987B98EEF8AB9089501C037
:1000B0000197009759F020E00000000000000000C8
:1000C000000000002F5F2A3599F3F6CF08958FEFD7
:1000D00084B987B98EEF8AB98FEF88B985B98BB9A2
:1000E00084EF91E00E94570018B815B81BB884EF50
:0800F00091E00E945700F0CFDF
:00000001FF


Check this out though:
http://sourceforge.net/projects/winavr/

It looks like it can compile C/C++ into HEX.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't consider the hex output to be assembly code --- however, I have seen reference to a utility that can produce assembly code from hex, although I expect that a great deal of readability would be lost.

As to why? --- I just like to peek under the skirts to see how things are done.  And sometimes for performance reasons I might feel a need to depart momentarily from the Arduino library calls and it can be useful to see how the C code (that works) does things; this can be used as a guide to in-line assembly coding.

Thanks for the response.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I just like to peek under the skirts
Pervert  smiley-twist
Logged

0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bob Anderson

I agree with the points you make.  When writing for a controller, I am always pleased to have an easy and quick to program system create the user and communication interfaces and then revert to assembly for the interrupt routines that actually do time sensitive controlling.  If you had access to the assembly code that Arduino system emits, you could see when and what resources it is using and stay clear of interference, or change the Arduino program to avoid problems.

John Carroll

Interesting how the spell checker hates Arduino
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hold SHIFT down whilst pressing the verify or upload button to see what the IDE is doing behind the scenes. You will see that it uses a temporary directory, where you will find source and binary files.

You can either use objdump -d or -D to disassemble the object files, or copy the gcc command and add -S to get it to output assembler files.
Logged


0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stimmer,

Thank you.

John Carroll
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stimmer:

Is there a way to tell Arduino IDE to use the -S option?

It looks rather burdensome (and error prone) to copy and modify the rather long list of commands that are executed to compile a program.

Thanks for your help in this matter.  It was useful to see what Arduino is doing.

Bob
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 137
Posts: 6805
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
avr-objdump -S
sketch.elf
will give you a mixed source/assembly listing; the elf file still has symbol info so it is pretty readable.
Logged

Washington
Offline Offline
God Member
*****
Karma: 39
Posts: 808
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I just like to peek under the skirts
Pervert  smiley-twist
LOL
Logged

Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

0
Offline Offline
Newbie
*
Karma: 1
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

westfw:

Where would one place the  "avr-objdump  -S" line?

I am used to imbedding things like 

      #pragma  stuffForCompiler

in the code.  Can this be done with Arduino to induce an assembly listing?  I'd really like to see an assembly listing interspersed with the C code.  I guess since the avr toolset is being used that I should investigate on the avr site as well.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You run it at a command prompt or terminal shell depending on your OS. Which OS are you using?

You can copy and paste the command from the window, if you are using the avr-gcc -S method you only need the one line which compiles your sketch (the other ones are compiling the Arduino library)

Unfortunately it is rather a chore, that's just the way it is... the IDE is designed to make the simplest things that most people do (ie edit a simple sketch and upload it) as trouble free as possible. Doing anything more complicated than that and very often you are on your own.

Two things to watch out for in the disassembly once you get it. Instructions are in the form  Instruction Destination Source, not Instruction Source Destination (think Intel style rather than AT&T). And watch out for an instruction called MOVW which the compiler emits quite often but the disassembler seems to disassemble incorrectly. So instead of saying movw r17:r16,r1:r0 it just says movw r16,r0. That had me confused for hours.
Logged


SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 137
Posts: 6805
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You run it at a command prompt or terminal shell
Right.  There's no way I know of to get the IDE to produce the listing for you; you have to let the IDE produce the binaries, and then generate the listing manually from the binaries.

As Stimmer points out, you wind up with "gnu assembler" syntax, which will not match Atmel assembler syntax in a number of annoying ways.  (on the bright side, gnu assembler syntax remains more consistent across multiple architectures than the individual manufacturer's syntaxes...)
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stimmer and westfw:

Thanks again for your useful comments.

I am using Snow Leopard on a MacBook Air. 

For some reason, I can find no vestiges of compiling a sketch anywhere on my harddrive.  Arduino seems to sweep up all crumbs, that makes it impossible to apply any of the disassembly tools.

I will try invoking avr-gcc -S in a Terminal window to see what I get.

Actually, this is not very important as I can find most of what I want by examining the core code.

I appreciate the Arduino IDE philosophy as outlined by stimmer, but I still wish that were a possibility to get a little deeper into things without having to change to a different environment completely.  There isn't much of a path for the developing programmer to spread his wings as he gains confidence.  On the other hand, any such options could easily become a source of confusion for the beginner.  On the whole, I feel that Arduino has it right.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 137
Posts: 6805
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am using Snow Leopard on a MacBook Air.

For some reason, I can find no vestiges of compiling a sketch anywhere on my harddrive.  Arduino seems to sweep up all crumbs, that makes it impossible to apply any of the disassembly tools.

yes, apparently the IDE asks Java for a temporary directory, and gets something deeply buried as a result.  (I think this is something copied from "Processing", where the compiled code is less interesting the IDE originates with processing, and this behavior changed after a relatively recent upgrade that involved syncing with the Processing code.))  Holding SHIFT while clicking the verify command will show the verbose output during compile, and you can find the directory there.  Or you can modify your preferences.txt file (~/Library/Arduino/preferences.txt; a simple text file) to include a "build.path=/tmp/arduino" or similar.
Logged

Pages: [1] 2   Go Up
Jump to: