How can one view the assembly code output from the Arduino IDE?

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.

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

for this blinking file:

/*
    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

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

Check this out though:

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

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.

I just like to peek under the skirts

Pervert ]:D

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

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.

stimmer,

Thank you.

John Carroll

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

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.

1 Like

PaulS:

I just like to peek under the skirts

Pervert ]:D

LOL

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.

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.

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...)

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.

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.

WARNING WARNING

I tried the suggestion to put "build.path = " in my preferences.txt file for Arduino. I set the path to my desktop (I’m running Snow Leopard) — big mistake !!!

What happened is that Arduino did (apparently) use my desktop as a place to store its intermediate files, but when it was done, it performed a delete . in that directory and erased my desktop and everything that was laying on it.

Fortunately I was using Carbonite and was able to recover the desktop contents (the Arduino delete did not move the deleted files to Trash) without a great deal of effort: just time.

Ah. Sorry. Do give it its OWN directory for the build.

westfw:

I will try that, but I am not optimistic --- the delete . behavior is likely to still be in effect, so I will have no output files to work with in any case.

westfw: You said...

Ah. Sorry. Do give it its OWN directory for the build.

Yes, that does work. But I was worried that Arduino will delete all the files after a verify or upload function.

What I have found is that Arduino does delete all the files in the build directory, but only on exit, so if I keep the IDE open, the output files can be examined. In particular, it is possible to apply objdump -S against the .elf file. And the output from that is exactly what I was looking for.

Thanks for all the help.

Bob Anderson, I feel for you. I see this post may be redundant after we all tied the build path

Thank you for the build.path= line for the preferences.txt file. For me there is an important change of action when the build path is changed. Without the build the commentary at the bottom of the screen ends with something like:

C:\DOCUME~1\Owner\LOCALS~1\Temp\build6058277634917607854.tmp\meg09.cpp.hex

indicating that a build directory was generated. On my XP machine the build directory is persistent. I assume, without testing, this is the result of leaving the delete previous applet box in the preferences tap unchecked. Whatever, it is okay with me.

When the build.path parameter was added the bottom line became something like this:

c:___a_output\meg09.cpp.hex

Far more handy to look AND feel the output. Easier to build batch files for objdump.exe. EXCEPT, everything in the directory is erased when the Arduino application is closed. This led to a near heart stopper when, on the first try, I discovered I had mistakenly typed in the directory name of my project files instead of the intended location of the Build files. I poked around a bit and decided that I had to erase the build files and fix the parameter before it got me into trouble. I shut down Arduino in order to edit the preferences.txt file. The result was the complete erasure of the entire hierarchy of project files, and not to the recycle bin.

Although somewhat mentally challenged,I am capable of slowly learning, particularly if blood and pain are involved. Just before trying this I had mashed the icon that incrementally backs up the arduino files to a compter "somewhere else".