Blog Entry
Beginners AVR Assembler Language Programming 1
November 21, 2008 by rwb, under Microcontroller.
I will present this tutorial using this following imaginary conversation between David and Susan; hope you enjoy
David:
Why should I learn coding in assembler language? It’s a machine language and for sure it’s very hard to learn!
Susan:
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.
David:
So what is actually this assembler language?
Susan:
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
- “Go to the kitchen and bring me a cup of hot tee”
or I could say like this
- “Go to the kitchen”
- “Get a cup”
- “Get a bag of tee”
- “Put it in the cup”
- “Poor hot water in it”
- “Bring it to me”
David:
What’s your point Susan?
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.
David:
Ok… I understand that; but what is the benefit of learning this kind of low level language or I should say a caveman language!
Susan:
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:
- Go to the kitchen
- if there is a door open it first
- if we could not open, than send an error message
- Get a cup
- if it broken than take another cup
- if we could not find one, than send an error message
- Get a bag of tee
- if we could not find it, than send an error message
- Put it in the cup
- Poor a hot water in it
- if is not hot than find a hot one
- if we could not find a hot water, than send an error message
- Bring it to me
Therefore this is what the code look likes when we disassemble the higher level language compiler generated machine code.
David:
Why they must instruct the machine to check the door first; what if there is no door on the way to the kitchen?
Susan:
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.
David:
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?
Susan:
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.
David:
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?
Susan:
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.
David:
Wow… hold on; you said what?… bank?
Susan:
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.
David:
Ok. Now I understand, I would like to start with AVR first
Susan:
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.
David:
Ok… I will…see you tomorow and thanks Susan.
(Beginners AVR Assembler Lanuage Programming 2)