Go Down

Topic: Support of Arduino 1.8.5 IDE for assembly language program (Read 4522 times) previous topic - next topic

tbanerje

Hello everyone!

I'm a newbie in this field, just bought ATmega 2560 development board, installed Arduino IDE 1.8.5.
But I'm looking for assembly language coding, does this IDE support that?
I have gone through a video on
https://www.youtube.com/watch?v=h8T3PWauYF4
 This seem to have been recorded quite a few years back. These options that has been said on this video or as a matter of fact the .java file which he mentioned in the video, is also not present in the current source code of the IDE.
I can use some older version of IDE, but before doing that I was wondering if there is some other way out to use assembly language coding that is running .s files on Arduino IDE.
If someone has gone through the same issue then please let me know
Thanks a lot

pert

There should be no need to mess with a .java file. The recent versions of the Arduino IDE supports .S files (note capital S). This example might be useful:
https://forum.arduino.cc/index.php?topic=413151.msg2844634#msg2844634

You can also use inline assembly in your sketch or other C++ code by putting it inside the asm() declaration.

tbanerje

Thank you so much posting the link. That's exactly what I was trying to do. But unfortunately I'm getting a lot of error.
So currently I have a folder called blink with the two files blink.ino and blink.S in it.
When I try compiling them I get these errors :
Is it due to some header file that I should have included?




Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\pc-admin\Documents\Arduino\codes\blink\blink\blink.S.ino:1:0: warning: "__SFR_OFFSET" redefined

 #define __SFR_OFFSET 0

 ^

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,

                 from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\pgmspace.h:90,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28,

                 from sketch\blink.ino.cpp:1:

c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\sfr_defs.h:172:0: note: this is the location of the previous definition

 #    define __SFR_OFFSET 0x20

 ^

blink.S:4: error: expected unqualified-id before '.' token

 .global start

 ^

blink.S:9: error: 'ret' does not name a type

   ret

   ^

blink.S:13: error: 'call' does not name a type

   call  delay_n_ms

   ^

blink.S:15: error: 'ldi' does not name a type

   ldi   r20,250

   ^

blink.S:18: error: 'ret' does not name a type

   ret

   ^

blink.S:21: error: expected constructor, destructor, or type conversion before '(' token

   ldi   31, 3000>>8 ; high(3000)

                           ^

blink.S:22: error: expected constructor, destructor, or type conversion before '(' token

   ldi   30, 3000&255  ; low(3000)

                            ^

exit status 1
expected unqualified-id before '.' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

pert

The problem is that you somehow ended up with the file named blink.S.ino, rather than blink.S so it's being compiled as C++ code. I'm not sure how that actually happened. Try this:
  • Click on your blink.S tab.
  • Click the button with the downward pointing triangle on the right side of the tab bar
  • Click "Delete"
  • Click "OK"
  • Click the button with the downward pointing triangle on the right side of the tab bar
  • Click "New Tab"
  • Type blink.S
  • Click "OK"
  • Paste the assembly code from that other forum thread into the blink.S tab.

tbanerje

Thank  you so much Sir.
I could compile now.
I would just like to ge
t clarified about how the delay part is working, I understood something like this :
(Please correct me if I'm wrong)


When we write
ldi   31, 3000>>8 ; high(3000)
  ldi   30, 3000&255  ; low(3000)

that means 3000 is binary is 0b0000101110111000, so register r31 gets higher part that is  0b00001011 which is equivalent to 11 in decimal and r30 gets 0b10111000 which is 184 in decimal? Is it so ?

Then in delaylp :
we subtract 1 from 184 everytime till it drops to 0, and also this happens till r20 which has 250 drops to 0 , so 184*250 times they run together?

Thats how its produced?  But 184*250 is 46000 , but its mentioned about 16000 cycles, and then 5 cycles, 3000 loops etc, what are they about?
Thank you so much for your patience



pert

To be honest I don't know anything about assembly. That code is the only time I've ever used it and I mostly just copied it from the forum thread mentioned in the comment without fully understanding it. My contribution was to demonstrate how the assembly code westfw wrote could be used in an Arduino sketch, rather than being compiled outside the IDE as was done in the original thread.

I'm sure some other forum members have more knowledge to help you understand this.

tbanerje

Sure. I would wait for someone to explain  then. Actually I think the delay might be very less or something, because in my case the led stays lit for the entire time, I dont see it blink.
So I wasn't sure what exactly is happening.

Btw thanks for all the help


pert

The blink looks pretty close to the expected 2 Hz when I run the code.

The code is written to blink pin PB5. That pin is pin 13 on the Uno/Nano/Pro Mini Arduino boards but on the Mega 2560 PB5 is pin 11. Pin 13 on the Mega 2560 is PB7. If you want to blink pin 13 on your Mega 2560 you need to make a few changes:

Change line 12 of blink.S from:
Code: [Select]
sbi   DDRB,5
to:
Code: [Select]
sbi   DDRB,7

Change line 18 of blink.S from:
Code: [Select]
sbi   PORTB,5
to:
Code: [Select]
sbi   PORTB,7

Change line 21 of blink.S from:
Code: [Select]
cbi   PORTB,5
to:
Code: [Select]
cbi   PORTB,7

You can see the pin mapping of the Mega 2560 here:

tbanerje

Oh my bad, made this silly mistake.
But thank you so much for pointing it out. Now its working!

Even I'm not very familiar with this assembly language code used over here.

So one last question ,by any chance do you have any idea if there is some website/link that has any tutorial on assembly language that can be used in Arduino with ATmega2650 ?
Thanks again!

pert

The keywords you want to search for are avr assembly. There's nothing special that needs to be done to use it in the Arduino IDE.

This looks like it would be a fairly definitive reference:
https://www.microchip.com/webdoc/avrassembler/index.html

Coding Badly

So one last question ,by any chance do you have any idea if there is some website/link that has any tutorial on assembly language that can be used in Arduino with ATmega2650 ?
I found perusing disassembled C / C++ to be a good way to learn assembly.


pert

In that spirit maybe Compiler Explorer would be useful:
https://godbolt.org/g/MjD4Tm
That's the code with compiler optimization disabled (-O0), which might produce more understandable assembly.

Compare that output to the same program compiled with the optimization setting used by Arduino (-Os):
https://godbolt.org/g/UTq8rT

Of course you can use avr-objdump to do the same thing with the .elf file produced by the Arduino IDE but it's kind of neat how that website makes it so easy. It has lots of other languages and compilers to play with.

westfw

Be aware that the assembler you get from arduino is avr-gas, which is not quite the same as the official Atmel assembler syntax, and that most of the demo and training examples you may find may use the atmel syntax.   Little did uup uuutilrhthe nforgotall

tbanerje

Thank you so much everyone for all you solutions. I'll try to go through all of them and play with these codes.
I think this would give me the basic knowledge I require.
In case I find some difficulty, I'll again post over here.
Thanks again!

Go Up