Pages: [1] 2   Go Down
Author Topic: Arduino assembler programming  (Read 1063 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I recently got an Arduino UNO as an alternative to using plain atmel microchips with troublesome circuitry. I've been able to easily program the Arduino without using the IDE by following this post: http://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/

However, I would like to find a way to program the Arduino with assembler. I've only seen posts about AVR-Studio assembler programming in Windows, using in-line assembly, and modifying the Arduino IDE, none of which I want to do. Is there a simple way of merely creating a HEX file with assembler and downloading it into an Arduino UNO via AVR-dude? If there is, how is it achieved and what tools would I require?
I'm currently using Linux(Ubuntu 12.04).

Thanks in advance!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Any particular reason why? Or do you just want to learn assembler?

You can drop into assembler from C, although it's fiddly IMHO. I don't know of a stand-alone assembler but there almost certainly is one.

If you are just doing it for speed, I wouldn't bother personally. If you want to learn more about the architecture, well have fun!
Logged

Offline Offline
Full Member
***
Karma: 11
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The avr-gcc toolchain will assemble assembly language source files. After all when avr-gcc compiles a c++ file it first generates an assembly language file. Which it then passes to avr-as to assemble into an object file. Which is then passed to avr-ld to produce an executable. The toolchain comes with the Arduino IDE, or you can get it from the Ubuntu repositories.

terry
Logged

0
Offline Offline
Edison Member
*
Karma: 17
Posts: 1413
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any particular reason why? Or do you just want to learn assembler?

You can drop into assembler from C, although it's fiddly IMHO. I don't know of a stand-alone assembler but there almost certainly is one.

If you are just doing it for speed, I wouldn't bother personally. If you want to learn more about the architecture, well have fun!


hehe it looks like you are not very keen on assembler?
I have never attempted assembler, is it still used now a days? You talk about speed, when would someone code in assembler as a general practice?
Logged

* Si preguntas, pon el código de tu programa, hace mucho mas fácil ayudarte. Y me ahorro un mensaje pidiendo que lo hagas.
* Si consigues solucionar tu problema, dedica unos minutos a explicar en tu post como lo conseguiste para beneficio de todos.
* Cambia el 'Subject' de tu hilo y añade 'SOLUCIONADO' cuando hayas llegado a una solución al problema que planteaste.
* Utiliza un 'Subject' para tu hilo que explique de que va el hilo.
Si estas empezando:
* Comienza a usar Arduino
* Guías de iniciación a Arduino
* Ejemplos
* Referencia del Lenguaje
* Conceptos básicos
Guia de usuario de arduino
Tutoriales en Ingles
Si necesitas que alguien te escriba el código: http://www.freelancer.com/  o esta  http://www.guru.com/

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written more assembler than I care to remember, for example a Pascal compiler written in assembler for the Apple 2:

http://www.gammon.com.au/forum/?id=11203

However these days it is only really required for low-level system programming, where you either need to do something not supported by other languages (eg. C) or something very timing-specific.

Modern C (and C++) compilers optimize very well. You will probably write slower code if you try to do it yourself in assembler, unless you are a real expert. The compiler can keep track of register usage, use "tricks" you may not have thought of, move instructions around, and generally produce fast, tight code.

It's fine to learn how to read it, knowledge is always useful. But I wouldn't be writing a big project in assembler these days, personally.
Logged

0
Offline Offline
Edison Member
*
Karma: 17
Posts: 1413
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written more assembler than I care to remember, for example a Pascal compiler written in assembler for the Apple 2:

http://www.gammon.com.au/forum/?id=11203

However these days it is only really required for low-level system programming, where you either need to do something not supported by other languages (eg. C) or something very timing-specific.

Modern C (and C++) compilers optimize very well. You will probably write slower code if you try to do it yourself in assembler, unless you are a real expert. The compiler can keep track of register usage, use "tricks" you may not have thought of, move instructions around, and generally produce fast, tight code.

It's fine to learn how to read it, knowledge is always useful. But I wouldn't be writing a big project in assembler these days, personally.

Thanks Nick, that was a very usefull answer to my question. I had always want to have this key points clear. As you say, knowledge is always useful, and I will indeed try to learn a bit about reading the code, but now I have things much more clear about how useful or not it can be for me.

Once again, thanks for answering.
Logged

* Si preguntas, pon el código de tu programa, hace mucho mas fácil ayudarte. Y me ahorro un mensaje pidiendo que lo hagas.
* Si consigues solucionar tu problema, dedica unos minutos a explicar en tu post como lo conseguiste para beneficio de todos.
* Cambia el 'Subject' de tu hilo y añade 'SOLUCIONADO' cuando hayas llegado a una solución al problema que planteaste.
* Utiliza un 'Subject' para tu hilo que explique de que va el hilo.
Si estas empezando:
* Comienza a usar Arduino
* Guías de iniciación a Arduino
* Ejemplos
* Referencia del Lenguaje
* Conceptos básicos
Guia de usuario de arduino
Tutoriales en Ingles
Si necesitas que alguien te escriba el código: http://www.freelancer.com/  o esta  http://www.guru.com/

Cincinnati, OH
Offline Offline
God Member
*****
Karma: 40
Posts: 664
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick: My company produced a C compiler back in the old MSDOS days. Byte Magazine was always running benchmark tests to see which compiler was the "best". One test that got most of the attention was the Sieve of Eratosthense. I remember one company consistently won that test, but was almost dead last in all of the other benchmarks. It was later discovered that the company actually built into their parser a detection for the Sieve source code and dumped out an EXE that was hand-tweaked assembler! Subsequent test revealed that most compiler vendors supplied libraries that were also tweaked assembler. While there's nothing wrong with this, the Byte benchmarks were a poor test of a compiler's code generation. Later, Computer Language published the Dhampstone Benchmark that was designed to test a compiler's code generation, rather than its library routines.

Personally, I wish I hadn't let my assembler abilities get rusty as it can still be useful. However, I would not want to code in assembler on a day-to-day basis.
Logged

USA
Offline Offline
Jr. Member
**
Karma: 2
Posts: 87
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Inline Assembly is a good method to get your feet wet. However it has somewhat odd and confusing syntax.

Mixing C and assembly in AVR Studio is another method.

Check out the book, "Programming Microcontrollers using Assembly Language" for an excellent primer.

Goodluck...
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick: My company produced a C compiler back in the old MSDOS days. Byte Magazine was always running benchmark tests to see which compiler was the "best". One test that got most of the attention was the Sieve of Eratosthense. I remember one company consistently won that test, but was almost dead last in all of the other benchmarks. It was later discovered that the company actually built into their parser a detection for the Sieve source code and dumped out an EXE that was hand-tweaked assembler!

That's funny! In the day when I produced that compiler I remember that Sieve test, and recall that I did tweak the compiler slightly to improve its performance. Not cheating, but subtle things, like the order in which keywords were tested for in the lexer. For example, if you use "for" more frequently than "return" then if you put "for" higher up the list of keywords to be found, it will parse it slightly faster. Of course that affected compile speed, not run-time speed, but I think you could do similar things for the run-time part. A trade-off to make some things slightly faster at the expense of others. My changes however would improve compile and run-time speed for all "typical" programs, not just the Sieve one.
Logged

Offline Offline
Sr. Member
****
Karma: 25
Posts: 498
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use tavrasm.  Here is the syntax I use in my build shell script:

Code:
/home/tanhadron/Programs/tavrasm/bin/tavrasm -i PumpController.asm -o PumpController.hex -e PumpController.lst

Then I use avrdude to send the hex file to my ATtiny44.  Or 13.  Or 85, or whatever.
Code:
/home/tanhadron/Programs/arduino-1.0.3/hardware/tools/avrdude -C/home/tanhadron/Programs/arduino-1.0.3/hardware/tools/avrdude.conf -pattiny44 -cstk500v1 -P/dev/ttyACM0 -b19200 -Uflash:w:/home/tanhadron/Projects/PumpController/PumpController.hex:i

There is a little bit of house keeping in the actual asm file.  Set up the interrupt vectors, initialize the stack, disable the watchdog timer... but once you have done it once, the template is there and using it for other programs is simply a cut and paste.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for the quick and informative replies!

Any particular reason why? Or do you just want to learn assembler?

You can drop into assembler from C, although it's fiddly IMHO. I don't know of a stand-alone assembler but there almost certainly is one.

If you are just doing it for speed, I wouldn't bother personally. If you want to learn more about the architecture, well have fun!


I'm doing it for educational purposes. I don't get much satisfaction out of seeing my C programs work, let alone see an Arduino Sketch work.  smiley-razz

The avr-gcc toolchain will assemble assembly language source files. After all when avr-gcc compiles a c++ file it first generates an assembly language file. Which it then passes to avr-as to assemble into an object file. Which is then passed to avr-ld to produce an executable. The toolchain comes with the Arduino IDE, or you can get it from the Ubuntu repositories.

terry

I searched for a way to just skip to the assembler part of the compilation, turns out avr-gcc can accept .S assembler files directly. Seems like quite a simple solution for the small amount of support available in the web. I found this out in this site: http://www.nongnu.org/avr-libc/user-manual/assembler.html
I haven't gotten the Arduino Uno to respond though...

I use tavrasm.  Here is the syntax I use in my build shell script:

Code:
/home/tanhadron/Programs/tavrasm/bin/tavrasm -i PumpController.asm -o PumpController.hex -e PumpController.lst

Then I use avrdude to send the hex file to my ATtiny44.  Or 13.  Or 85, or whatever.
Code:
/home/tanhadron/Programs/arduino-1.0.3/hardware/tools/avrdude -C/home/tanhadron/Programs/arduino-1.0.3/hardware/tools/avrdude.conf -pattiny44 -cstk500v1 -P/dev/ttyACM0 -b19200 -Uflash:w:/home/tanhadron/Projects/PumpController/PumpController.hex:i

There is a little bit of house keeping in the actual asm file.  Set up the interrupt vectors, initialize the stack, disable the watchdog timer... but once you have done it once, the template is there and using it for other programs is simply a cut and paste.

How do I know the type of "housekeeping" I need to get it up and running?
I assume that it's either the fact that I haven't done any "housekeeping" in my code, or the shell script that's holding my code back. I'm using the same terminal commands used in http://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/ , maybe they were just made for C?

I would go further as to post my assembler code here to troubleshoot, but I'm afraid I may be going a bit off-topic now for an "arduino programming questions" sub-forums primarily focused on Sketches.
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Personally, I wish I hadn't let my assembler abilities get rusty as it can still be useful. However, I would not want to code in assembler on a day-to-day basis.
Agreed. I wrote a program in assembler for the Sinclair Spectrum. It was the only way to do what I wanted. If I made an error...CRASH...with no error codes or reports and the time taken to write a simple routine could be quite long. I'm happier, these days, writing in a higher level language, such as C++, where the code can be written and checked fairly quickly. I'm sure that too much assembler 'does your brain in'.  smiley

That's not to say that I regret learning assembler, as that knowledge has made higher level languages easier to understand.
Logged

Offline Offline
Sr. Member
****
Karma: 25
Posts: 498
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you want to use the Arduino core?  If so, it does all the house keeping for you.  If you want it to be in control and call your assembly code, all you have to do is put your .S file into the build directory, and it will link it in.  Then you call your assembly routines from setup() or loop().

If you want to bypass all the Arduino core and take control from the beginning, you have to deal with all the house keeping tasks.  You also don't have access to standard Arduino routines.  If you want timers, you have to configure them and handle the interrupts yourself.  You can't do such things as analogWrite() or Serial.write().  And you don't have setup() and loop().

I'm not really sure how you would program the microcontroller while it's in the Arduino to do that, though.  I always use a separate microcontroller and bypass the boot loader when I do straight assembly programs.  If I didn't have another microcontroller, I wouldn't trust my assembly skillz not to brick my Arduino.  And since I have another microcontroller, why not wire it up to run standalone?  So I use my Arduino as an ISP and use it to program my standalone microcontroller.  For test programs like blinky LEDs and such, I don't use any other external circuitry except power and ground.  I turn on the internal pull-up on the reset pin, and use the internal clock so I don't need a crystal, and I disable the watchdog unless I'm using it as a sleep waker.
Logged

Offline Offline
God Member
*****
Karma: 7
Posts: 647
"In this house, we obey the Laws of Thermodynamics" Homer J. Simpson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In my early days of programming I was told to write some modules and to do it assembler as they needed to be very efficient. Not knowing the assembler I wrote some high level code to see what the compiler would produce with the intent of then tweeking it by hand. What I found was that what the compiler generated was hard to beat. Furthemore by restructuring my high level code I found I could get the compiler to produce output that was either faster or used less memory.

The end result was that the modules were written in the high level language much simplifying support issues.
Whilst assembler can be interesting there are not a lot of cases where it is really needed.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ironically, I use my knowledge of assembler these days largely to prove that you don't need to use it. Most times when someone says they will use assembler to "make it faster" I can prove that the code generated by the C compiler is superior, or at least, could not be beaten.

For example if someone wants to set a bit "quickly" and proposes to use assembler, I do it in C and show that the generated code does it in one or two clock cycles, which cannot be improved on.
Logged

Pages: [1] 2   Go Up
Jump to: