lboyel
lboyel

Reputation: 2228

How to add inline comments to generated assembly from arduino

I am trying to add inline comments to the generated assembly on my arduino. So for instance

/*
Testing
*/
#include <avr/io.h>
#include <iostream>


int ledPin = 13;

void setup()
{
 asm volatile("\n# comment 1");
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  asm volatile("\n# comment 2");
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

when the assembly is generated for the code i want to see "comment 1" and "comment 2" as a marker at that particular line in the assembly code.

Here is the assembly generated code

C:\Users\****\AppData\Local\Temp\build1888832469367065438.tmp\sketch_jul17a.cpp.o:     file format elf32-avr


Disassembly of section .text.loop:

00000000 <loop>:
loop():
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:19
   0:   80 91 00 00     lds r24, 0x0000
   4:   61 e0           ldi r22, 0x01   ; 1
   6:   0e 94 00 00     call    0   ; 0x0 <loop>
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:20
   a:   68 ee           ldi r22, 0xE8   ; 232
   c:   73 e0           ldi r23, 0x03   ; 3
   e:   80 e0           ldi r24, 0x00   ; 0
  10:   90 e0           ldi r25, 0x00   ; 0
  12:   0e 94 00 00     call    0   ; 0x0 <loop>
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:21
  16:   80 91 00 00     lds r24, 0x0000
  1a:   60 e0           ldi r22, 0x00   ; 0
  1c:   0e 94 00 00     call    0   ; 0x0 <loop>
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:22
  20:   68 ee           ldi r22, 0xE8   ; 232
  22:   73 e0           ldi r23, 0x03   ; 3
  24:   80 e0           ldi r24, 0x00   ; 0
  26:   90 e0           ldi r25, 0x00   ; 0
  28:   0e 94 00 00     call    0   ; 0x0 <loop>
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:23
  2c:   08 95           ret

Disassembly of section .text.setup:

00000000 <setup>:
setup():
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:13
   0:   80 91 00 00     lds r24, 0x0000
   4:   61 e0           ldi r22, 0x01   ; 1
   6:   0e 94 00 00     call    0   ; 0x0 <setup>
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:14
   a:   08 95           ret

The comments are not included in the assembly code, how can I do this

Upvotes: 2

Views: 1334

Answers (1)

Martin Rosenau
Martin Rosenau

Reputation: 18503

First of all comments in "regular" (not cpp pre-processed) assembler code begin with a hash sign, not with two slashes. So you might want that a comment named "# onesectimer" is in the assembler code.

This may be archieved the following way:

asm("\n# onesectimer");
void OneSecTimer()
{
  if(bags!=0){
    asm("\n# for counter 1");
    if(counter1 == 3)
...

--- Edit ---

Reading your comments and your edits I think you are mixing the words "assembly" and "disassembly":

When translating C code to binary code the C compiler generates "assembly" code. This code may contain comments:

# This is a comment
lds r24, 0
ldi r22, 1
call digitalWrite

The assembler then translates this "assembly" code to binary code. In binary code there is no information about comments any more but only the binary data to be written to the memory.

The "disassembly" translates the binary data back to assembly code but only information that is present in binary code may be disassembled - so you cannot have any comments in disassembly code!

What you may do is to insert a symbol into the object file at a point of interest:

digitalWrite(0,1);
asm volatile(".global Here_is_Delay\nHere_is_Delay:");
delay(1000);

The names of these symbols must be unique over the whole project and not be identical to any function or variable name used.

Depending on the disassembler (not sure about the AVR one) you'll see the symbols then:

loop():
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:19
   0:   80 91 00 00     lds r24, 0x0000
   4:   61 e0           ldi r22, 0x01   ; 1
   6:   0e 94 00 00     call    0   ; 0x0 <loop>

Here_is_Delay():
C:\Program Files (x86)\Arduino/sketch_jul17a.ino:20
   a:   68 ee           ldi r22, 0xE8   ; 232
   c:   73 e0           ldi r23, 0x03   ; 3
     ...

Upvotes: 4

Related Questions