Thursday, December 6, 2012

Fun with Arduino - how to get into assembler programming.

I am having fun with an Arduino Uno. I am old school; I started with a TRS-80 in the late 1970's, did a lot of Z80 assembly language. My computer had a massive memory (for the time) of 16KB, so in order to wring out a bit more, assembler was the way to go.

Gotta say, this Arduino brings back a lot of memories.

I bought a few tutorial kits (e.g. an Arduino Uno), and by the looks of it, the Arduino has a lot of similarities with my first computer, but it's a bit more powerful.

Its clock is 16MHz (my TRS-80 was 1.7MHz).
Its memory is 32KB Flash + 2KB SRAM + 1KB EEPROM (my TRS-80 had 16KB SRAM + 12KB ROM. And cassette tapes for storage)
It uses an 8-bit CPU (so did my TRS-80, which used a Z80).
Turns out that the Arduino IDE uses C++ as its 'main language' (that was Level 2 Basic on my TRS-80).

(And yes, these are KB - Kilobytes. Not Tera, not Giga, not Mega. Kilo. 1KB being 1024 Bytes. I still find it strange how, in 1978, I thought 16KB was unimaginably large, whereas nowadays 16KB in unimaginably small. My MacBook Pro has 8GB SRAM, more than 500,000 times what my TRS-80 had. Wow. That still flabbergasts me.)

The Arduino IDE is cool and all that, but feels kind of wasteful, so I set out on a quest to do some assembler programming on the Arduino.

Turns out that a lot of effort seems to have gone into discouraging doing this. There is a lot of 'magic' going on, and the Arduino IDE hides a lot of complexity from view. With good reason; there is often no need to look beyond the magic. One can opt to just accept it, and it works perfectly fine.

But I like to understand how it all works; dig into the magic, take it apart, and figure out how it works. I've always found that if you understand the magic, you can make better use of it.

I am pretty sure that most of my future Arduino (or Pinguino or Uno32; I bought one each of those too!) exploits will be using mostly, if not exclusively C/C++, but my first experiments are aimed at understanding how to do assembler programming.

I had to do a lot of fishing around to get enough info: lots and lots of little tidbits here and there, but no real 'how to get started' cookbook. So I decided to put the tidbits together in a wee dokuwiki page, and make it as complete as possible, in the hopes that someone else finds it useful.

While searching, often all I am looking for are a few good, complete examples, and that gets me going.

The dokuwiki page below is an attempt to help other people get going, by offering complete, working examples.

I tried to cover all the necessary steps, from patching up your IDE to accept assembly source code down to some increasingly advanced sample assembler programs.

There are some things I've skimmed over (e.g. I assume you know how to set up a Java development environment on your computer).

So, here goes. In case you're looking to get started with Arduino and assembler, here are my 'useful tidbits', for what they're worth:

If you want more detail for particular things, let me know. If I can find the time, I'll write additional instructions if needed.




Anonymous said...

Hey, this is awesome, thanks!


Anonymous said...

My first introduction to programming was FORTRAN on an IBM 360. Then I went on to the KIM1-hand assembly and and manual entry of the opcodes. Sometimes I think all that lovely memory we have today leads to sloppy and inefficient programming.