Arduino and assembly code

Is there a tool some where that lets you paste in a simple C/C++ code snippet and have it spit out the appropriate optimised assembly code?

I did assembler years ago with the 8086 processor.

And I'd rather learn assembler with Arduino/ESP32 via reverse engineering with a tool.

     asm volatile(
          "headB:"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 6"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 5"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 4"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 3"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 2"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 1"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 0"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "sbiw %[count], 1"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "ld   %[byte] , %a[ptr]+"
          "\n\t"
          "sbrc %[byte] , 7"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "brne headB"
          "\n"
          : [byte] "+r"(b), [n1] "+r"(n1), [n2] "+r"(n2), [count] "+w"(i)
          : [port] "I"(_SFR_IO_ADDR(PORTB)), [ptr] "e"(ptr), [hi] "r"(hi),
            [lo] "r"(lo));

Not tested , but might or might not work.

https://sourceforge.net/projects/arduino-to-assembly-converter/

Hi @gregaryb. Here is a nice website you might find useful:

1 Like

looks like gcc -c -Sfilename

You can use Wokwi.com to simulate an Arduino running code and also view the assembler code. The F1 function exposes the option.

This was the code running on an Arduino Uno (AVR architecture):