Programs required to program ATmega328p on Arduino Uno using Assembly Language

Hello, and thanks for your interest.

After doing some research, I seem to believe that in order to program the ATmega328p on my Arduino Uno using Assembly, I just need to use Atmel Studio 7 (to produce the hex file), and AVRdude to upload it to the MC.

My question is: Is this really true?

And if so, do I need to take any precautions?

If you must know (and I'm sure you want to), I want to program using Assembly Language because I'm going to develop a couple of projects that:

a) Are time-intensive.
b) Really could use the ADC Noise Reduction mode.

With Assembly I can know for sure how much time each instruction is going to take in order to be executed, and I also believe that when the Arduino Uno uses the ADC for measuring, it doesn't activate the ADC Noise Reduction mode (I'm not 100% sure about this, and if this is not the case, please correct me).

I know more or less how to use Assembly, but I learned with the PIC 16f628a, so I'm not just quite prepared yet for programming the ATmega328p with Assembly, but I'll get there (after I finish reading the 600+ pages of the datasheet). What I'm trying to say here, is that I'm positive that I want to use Assembly for these couple of projects.

I believe that you perhaps could recommend me to get an ATmega328p and a programmer (like a USBasp) separately, instead of trying to use the Arduino Uno for programming with Assembly. Sadly, I live in a wonderful land called Venezuela, where getting electronic parts is really difficult, being that either things cost 3 or 4 times more than, say, in the US; or simply I can't find them here. And trying to bring an ATmega328p and USBasp from the US, is currently not an option.

Please, be as detailed as you can with your answer.

As a side note, I'm not in any way, shape or form saying that programming the ATmega328p with Assembly is better. I believe that Arduino's languange is wonderful in many ways, specially in ways that Assembly isn't (starting from the fact that Assembly is way harder to learn). I actually believe that the best thing to do is to learn both languages, and to be able to recognize when a language suits better a certain project.

Thanks in advance for your time, help and knowledge.

Yeah, that's all you need.

You can mix and match assembler and C, so you get the ease-of-programming of C when you don't need every bit of speed possible, and assembly when you do. You can even use the Arduino IDE when doing it this way, and upload your code through the USB port in the normal way, without even having to manually call AVRdude.

No particular precautions.

It's worth noting that the compiler's optimizer is pretty damned good; there often isn't much to gain from using assembler, because the compiler generates code very nearly as good from C. (On the third hand, some of the Arduino helper functions, such as digitalRead/digitalWrite, are shockingly slow because of the overhead of translating arduino pin numbers - direct port writes are more than 10x faster than using those functions, but you can do that in C, you don't need assembler)

You also don't need assembler to use the ADC noise reduction mode, just set the appropriate registers to enable it (you may even be able to just set the registers and continue to use analogRead!)

I would advise to, for the purposes of ease-of-programming as well as maintenance, to only use assembly when you need to; assembly is much harder to maintain (for yourself after you've forgotten exactly how you did everything, or for the next person maintaining or modifying your project). Over the past 40 years, it has been discovered just how important maintainability is - a coworker of mine who was involved in some US govt stuff, where there is legacy code, written when RAM cost more than $1/byte in the really early days, which they are now stuck dealing with. The savings they made at the time by using less of the $1/byte ram, they have paid for more than ten times over in costs of maintaining, interfacing with, and modernizing the old code.

Re: Your being in Venezuela - good luck man. I'm amazed you can get any electronics there, honestly.

+1 to what Dr Azzy said. Adding my perspective:

In all seriousness, you've been conditioned to think that way by the PIC pea brained 8 bit processors. The PIC was never designed with a compiler in mind with its one W register. One working register. No matter how good the compiler is, it would be severely handicapped by the hardware.

This is not the case with the 8 bit AVR family used in most Arduinos, they were designed for compiled code with its 20 primary registers. This reason is why you will find very little assembly code for the AVR family of processors with the vast majority of code in C and some C++ that used in the library extension model of the Arduino eco-system.

Assembly is perhaps good to know but not needed and certainly optional. The time required to develop does not justify the benefit which is marginal at best. With AVR's, the code size is directly related to execution speed with a 1:1 clock/instruction execution ratio. The PIC's are 4:1. Yes, there are a few exceptions to these rules but the rule is there with the AVR's being significantly faster for an equal clock rate.

Oh, one last point. You'll find you can learn all you need/want to know about AVR assembly without the pain by using the simulator within AS7. It's much easier to debug than the build/load/crash/repeat model of development without an in-circuit debugger.

Have fun.

As an alternative to the Atmel Studio software to produce the HEX file, you might look at a very small, fast assembler called the Naken Assembler written by Michael Kohn:

I've never used it for AVR, but it's what I use to program the TI MSP430 controllers. It's a very small command line program, has no dependencies, and produces output pretty much instantaneously. And it's free.

Naken now supports a number of controller/processor lines including AVR8. The required code format may differ slightly from Atmel's standard.

I just need to use Atmel Studio 7

I suppose. Note that "Just AS7" consumes about 5G of disk space, and only runs on Windows.

You could also write in assembler using the Arduino IDE, using a short "startup" .ino file, and immediately calling a function written in a .S (assembly language) file.

westfw:
Note that "Just AS7" consumes about 5G of disk space, and only runs on Windows.

You could also write in assembler using the Arduino IDE, using a short "startup" .ino file, and immediately calling a function written in a .S (assembly language) file.

I'm intimidated by the big IDEs. In addition their size, there's a significant learning curve that comes with all that functionality and complexity. You can't just do something simple. As demonstrated repeatedly here, even the Arduino IDE requires a little getting used to, but that's pretty much necessary for beginners because it's still easier to deal with than dealing with GCC directly. But installing Atmel Studio just to get assembler - that's paying a pretty high price. If a .S file works in Arduino, then that has to be the way to go. Or something small and simple like the Naken Assembler (about 2MB, for Windows, Mac and Linux) if you have a way to flash the hex file.

But I would also agree with the earlier suggestions to avoid using assembler if you can. I've used it a lot, and love coding in it, but the fact is that your code isn't of much use to anyone else. Or to yourself two years later if you haven't commented it thoroughly.