Blog Entry

Beginners AVR Assembler Language Programming 1

November 21, 2008 by , under Microcontroller.

I will present this tutorial using this following imaginary conversation between David and Susan; hope you enjoy

Why should I learn coding in assembler language? It’s a machine language and for sure it’s very hard to learn!

The truth is no one actually can really understand the machine language as it only contains “0” and “1”; or what we know as a binary, event the most experience programmer could not understand this kind of language.

So what is actually this assembler language?

The assembler language is the same like all other programming language such as C, BASIC or PASCAL; every programming language has their own dialect and syntax. But all the programming languages have the same goal that we as a human can read and write the code using our own language.

For example if I want to instruct someone to make me a cup of tee, I could say like this

  1. “Go to the kitchen and bring me a cup of hot tee”

or I could say like this

  1. “Go to the kitchen”
  2. “Get a cup”
  3. “Get a bag of tee”
  4. “Put it in the cup”
  5. “Poor hot water in it”
  6. “Bring it to me”

What’s your point Susan?

The first instruction is just like all other high level programming language such as BASIC and PASCAL and the second one is much more like assembler language, while C language is in the middle of them. The more higher the level of the programming language the more easier to write the command because they have a rich syntax which is close to the human language, on the other hand the assembler language is only recognize the basic microcontroller instruction syntax which mean we have to assemble it to make one useful command.

Ok… I understand that; but what is the benefit of learning this kind of low level language or I should say a caveman language!

All the programming language before they can really run by the microcontroller must go to what is called the compiling phase; in this phase all of the human language like code will be translated into the machine binary code.

Now the interesting part begin; as you’ve seen from my first example the “make a cup of tee” command; this kind of command is a much more human’s like command and there is no machine in this world can understand it, so while in the compiling phase the compiler designer put a lot of assuming on what the machine should do:

  1. Go to the kitchen
  2. if there is a door open it first
  3. if we could not open, than send an error message
  4. Get a cup
  5. if it broken than take another cup
  6. if we could not find one, than send an error message
  7. Get a bag of tee
  8. if we could not find it, than send an error message
  9. Put it in the cup
  10. Poor a hot water in it
  11. if is not hot than find a hot one
  12. if we could not find a hot water, than send an error message
  13. Bring it to me

Therefore this is what the code look likes when we disassemble the higher level language compiler generated machine code.

Why they must instruct the machine to check the door first; what if there is no door on the way to the kitchen?

That’s the point! In the higher level language this assumption should be taken into consideration when compiling the program, because what if there is a real door or there is no hot water; but in assembler language we as programmer that make this decision whether to put this into consideration or not. That’s why the assembler code can be made smaller and efficient comparing to the higher level language and of course this will affect the speed of the program.

Ok Susan, this is sound good, but if this assembler language it’s so good; small, efficient and fast why not many people learn and use it?

Good question; the disadvantage of using the assembler language is; it’s a machine dependent language; this mean the code in assembler from one microcontroller type like Atmel AVR and Microchip PIC is a totally different. But in higher level language this gaps are much more closer; all the differences issues is taking care by the compiler.

Now, I’m become more confuse; this mean if I code in the assembler language for the Atmel AVR, I could not just recompile it to the Microchip PIC; means I have to recode it to the Microchip PIC assembler language?

Yes… you are right; because it’s a machine dependent language and it’s depend on the microcontroller you use. For example the microcontroller architecture is completely different like the AVR microcontroller use continues memory for it’s registers while the PIC microcontroller use bank system memory for it’s registers, even the I/O port name is totally different.

Wow… hold on; you said what?… bank?

Sorry… I just want to explain the differences; but to my experiences once you understand one type of microcontroller assembler language; it’s easier to learn another type of microcontroller assembler language; the learning time it’s lesser comparing to when you learn the assembler language for the first time. Beside you could choose just to use one type of microcontroller; today most of popular microcontrollers almost have similar features.

In the real world most experience embedded programmers combine the C code with the assembler code; when they need a time critical code; they use assembler language instead of C. Therefore basically the best of this two world can be achieved.

Ok. Now I understand, I would like to start with AVR first

No problem! Just bring your notebook tomorrow and don’t forget to download the latest Atmel AVR Studio 4 and the Atmel AVR Tiny2313 datasheet from their site.

Ok… I will…see you tomorow and thanks Susan.

(Beginners AVR Assembler Lanuage Programming 2)

Bookmarks and Share

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark