15

I would like to be able to see an assembly language listing of my Arduino sketches. How can I achieve this?

Update: I am running the Arduino Software on a Windows machine.

5 Answers 5

14

One way to do this is to use avr-objdump on the .elf file created by the build. For example, on OS X I can do this:

$ cd ~/arduino-0015/examples/Digital/Blink/applet
$ avr-objdump -d Blink.elf

(Your path on Windows may be different, obviously.) This produces a disassembly of the code, part of which will look something like this:

0000013a <main>:
 13a:   0e 94 3e 01     call    0x27c <init>
 13e:   0e 94 97 00     call    0x12e <setup>
 142:   0e 94 80 00     call    0x100 <loop>
 146:   fd cf           rjmp    .-6             ; 0x142 <main+0x8>
1
6

If you are using Linux, you can follow this tutorial on how to compile for the Arduino without the IDE.

Once you do that, you can get an assembly listing by running gcc with the -s flag.

1
  • @Magnus I'm running on Windows, but the link may still be of use - I'll take a look. Thanks. Commented May 28, 2009 at 8:38
3

The following (hacky) steps will provide assembly language listings of Arduino sketches and associated libraries on Windows:

  1. Download (and rename) the Arduino Windows command line batch files into the directory containing your sketch (the .pde file)
  2. Set up the required environment variables as specified in the above linked page
  3. Add -S to the abuild_gcc_opts variable in abuild.bat (line 158)
  4. Run abuild -r -c <pde_filename>
  5. Expect to get the following warnings and errors, which you can ignore:

    ... warning: #warning "This file has been moved to <util/delay.h>."

    .\obj\<pde_filename>.cpp.o: file format not recognized: treating as linker script

    .\obj\<pde_filename>.cpp.o:1: syntax error

The assembly language listings can be found in the .o files in the created obj directory. For example the listing for the sketch itself is in obj\<pde_filename>.cpp.o

0

The -S (not s) flag show the c code as well.Also know as mixed listing:

linux: (.arduino/preferences.txt: delete_target_folder=false)

$ cd /tmp/buildxxxx.tmp
$ avr-objdump -dS Blink.cpp.elf

int main(void)
{
    init();
 2f4:   8a df           rcall   .-236       ; 0x20a <init>
...
0

Arduino compilation is set up with link-time optimization (LTO), and the assembly files created during C compilation don't contain any assembly code - just the intermediate representation used later in the LTO stage to generate actual assembly code. We want the latter.

It so happens, rather nicely, that the very last time assembler is invoked in the entire compilation process, is to take the output from the LTO stage, and turn it into a whole-application object file.

To get at this assembly file, do the following:

  1. Open %LOCALAPPDATA%\Arduino15\packages\arduino\hardware\avr\1.8.3\platform.local.txt (create it if it doesn't exist). Add the following line

    compiler.c.elf.extra_flags=-save-temps=obj -fverbose-asm
    

    If there already is such a line there, just add the above options (to the right of =).

    Note: If you can't find the folder, look for the following files: boards.txt, platform.txt, programmers.txt. If you find these files in multiple locations, the one you want should be inside your home directory (i.e. not in Program Files or /Applications or /usr), and should be correlated at least partially to the version of Arduino you're using (should you have multiple versions installed).

  2. Build the sketch again.

  3. Look for a file named %TEMP%\cc*.ltrans.s. Each build will create a new such file. If you sort them by timestamp, the newest one is for the most recent build.

Note: The environment variables, enclosed by % signs, are valid in the Windows dialog boxes. You can directly paste such a path into e.g. an Open or Save As... dialog box, and it'll work. Paths with globs (*) will act as filters (IIRC).

To know which exact cc*.s file was generated, add the verbose option to the final binary generator pass:

compiler.c.elf.extra_flags=-save-temps=obj -fverbose-asm -v

Rebuild, and copy-paste all the output from the compilation status window in Arduino to e.g. Notepad++, and search for ar.exe. The sole line that contains ar.exe ends with the name of this cc file:

 c:/users/[...omitted...]/avr/bin/as.exe -mmcu=avr6 -mno-skip-bug 
    -o C:\Users\[...]\AppData\Local\Temp\cc3XhU2F.ltrans0.ltrans.o
    C:\Users\[...]\AppData\Local\Temp\cc3XhU2F.ltrans0.ltrans.s

How does the file look? This is a short extract, just to show that both the C source and the assembly are intermingled - and this represents exactly the binary embedded in the .hex file and sent to the target.

.LBE33:
.LBE32:
.LBB34:
.LBB35:
 ;  C:\Users\[...]\Documents\Arduino\sketch_jul01a\sketch_jul01a.ino:29:   pinMode(ledPin, OUTPUT);
    .file 4 "C:\\Users\\[...]\\Documents\\Arduino\\sketch_jul01a\\sketch_jul01a.ino"
    .loc 4 29 0
    ldi r22,lo8(1)   ; ,
    ldi r24,lo8(13)  ; ,
    call pinMode     ; 
.LVL55:
 ;  C:\Users\[...]\Documents\Arduino\sketch_jul01a\sketch_jul01a.ino:30:   pinMode(relayPin, OUTPUT);
    .loc 4 30 0
    ldi r22,lo8(1)   ; ,
    ldi r24,lo8(12)  ; ,
    call pinMode     ; 
.LVL56:
 ;  C:\Users\[...]\Documents\Arduino\sketch_jul01a\sketch_jul01a.ino:31:   pinMode(ldrPin, INPUT);
    .loc 4 31 0
    ldi r22,0    ; 
    ldi r24,lo8(54)  ; ,
    call pinMode     ; 
.LVL57:
.LBE35:
.LBE34:
.LBB36:
.LBB37:

This approach also works on Linux and MacOS, except that the paths are slightly different, and path expansion uses different syntax. The two files of concern still are platform.local.txt - a file you must create, since it doesn't exist at first, and the ${TEMP}/cc*.ltrans.s files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.