# Arduino and assembly code

**URL:** <https://forum.arduino.cc/t/arduino-and-assembly-code/1323208>\
**Category:** Programming\
**Created:** [November 18, 2024, 1:00pm UTC](https://forum.arduino.cc/t/arduino-and-assembly-code/1323208 "2024-11-18T13:00:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregaryb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gregaryb/32/710076_2.png) [@gregaryb](https://forum.arduino.cc/u/gregaryb)\
**Post date:** [November 18, 2024, 1:00pm UTC](https://forum.arduino.cc/t/arduino-and-assembly-code/1323208/1 "2024-11-18T13:00:34Z")

</div>

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.

```arduino
     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));

```

---

<div class="post-metadata">

**Author:** ![mikedb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikedb/32/1098613_2.png) [@mikedb](https://forum.arduino.cc/u/mikedb)\
**Post date:** [November 18, 2024, 1:49pm UTC](https://forum.arduino.cc/t/arduino-and-assembly-code/1323208/2 "2024-11-18T13:49:10Z")

</div>

Not tested , but might or might not work.

[https://sourceforge.net/projects/arduino-to-assembly-converter/](https://sourceforge.net/projects/arduino-to-assembly-converter/)

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [November 18, 2024, 2:04pm UTC](https://forum.arduino.cc/t/arduino-and-assembly-code/1323208/3 "2024-11-18T14:04:26Z")

</div>

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

> **[Compiler Explorer](https://godbolt.org/)**
>
> Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C++, Rust, Go (and many more) code.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [November 18, 2024, 2:15pm UTC](https://forum.arduino.cc/t/arduino-and-assembly-code/1323208/4 "2024-11-18T14:15:48Z")

</div>

looks like [gcc -c -Sfilename](https://www.google.com/search?q=generating+assembler+listing+from+gcc&rlz=1C1CHBF_enUS802US802&oq=generating+assembler+listing+from+gcc&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIHCAEQIRigATIHCAIQIRigATIHCAMQIRigATIHCAQQIRigAdIBCDc1MTZqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8)

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [November 18, 2024, 3:35pm UTC](https://forum.arduino.cc/t/arduino-and-assembly-code/1323208/5 "2024-11-18T15:35:19Z")

</div>

You can use [Wokwi.com](http://Wokwi.com) to simulate an Arduino running code and also view the assembler code. The F1 function exposes the option.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/8/8/788458c22a8ed31c200350f0574f3283f7d74f77.png)

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

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/2/c/0/2c03afb988b53e8277bac267ca6a23266475702d.png)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 17, 2025, 3:35pm UTC](https://forum.arduino.cc/t/arduino-and-assembly-code/1323208/6 "2025-05-17T15:35:20Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
