NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Assembly Language & C ++

November 22, 2009
by MC_Newb
MC_Newb's Avatar

Is it possible to load straight assembly language code to the MicroController (I have a tool to convert C ++ to Assembly Language code)

Is there a list of method limitations/functions that can be performed in C ++? For example, do certain sensors or electronic components have problems implementing C ++ correctly? (this is the reason I am asking if you can load Assembly Language code directly).

BEWARE :O)-- I am a high-level language programmer, and the closest language I code in is C#. As a result, the questions above my not even apply to what we are trying to do. Thanks NerdKits & NK Users for your assistance!


November 22, 2009
by mikedoug
mikedoug's Avatar

All processors (Atmel MPUs included) have a machine language. Assembly language is often used in place of machine language for terminology because it is often a 1-to-1 correlation to the actual machine code. So yes, at some point EVERYTHING we program for the MPU is converted into assembly before it is uploaded to the MPU. In order to use C++ you would need a compiler that knows how to compile C++ to the machine language for the MPU (or as you call it "a tool to convert C++ to assembly language).

The caveat here is that not all machine languages are alike. The Atmel MPUs use a vastly different machine language than your x86 desktop/laptop. The question then becomes what platform does your tool target?

Now, from within a C program (or a C++ program for that matter) you can definitely have blocks of code that are assembly. I'd have to hunt around for the exact syntax. But a far more important question is in order here -- why do you want to program in C++? These MPUs are very tiny in terms of program and executing memory space. C++ is a language of bloat, and I seriously doubt that there is anything you need/want to do on the MPU that will be made easier by having some of the extra constructs of C++ over the C that you already have in AVR-GCC. A benefit of using C++ or C# is some of the ease with which you can work with the String classes -- you probably couldn't even FIT the C++ or C# standard String class in the memory of most Atmel MPUs.

If you are familiar with C#, I would advise that you take a deep dive into C -- it's not so terribly different that you should be able to make the transition with relative ease. With these MPUs you've got to think SMALL. Very small.

Good luck whatever you are attempting! Michael Douglass

November 22, 2009
by MC_Newb
MC_Newb's Avatar

Great info Mike- I greatly appreciate you taking the time to reply on a Sunday afternoon! The reason I was using C++ is b/c I am used to the IDE in Visual Studio, and Visual Studio supports C++.

You brought up a point which completely resolves my uncertainty- Emphasize-the assmebly language used by the Atmel MPU is going to be different than that which my XP operating system would use. Hahaha! Forgive my ignorance!! That makes perfect sense :O).

FYI, when installing Visual Studio, there is an optional addin, "conversion tool", you can install and it will cause VS to "spit out" assmebly language code for you in a seperate window.

I did not realize that C ++ would greatly bloat my code as you informed me. I will have to get some of my old college books out and revisit the C language! I am very fluent in VS and therefore I was just trying to stay with what I know. Also, I am excited about the possiblities of MicroControllers and really want to get some of your projects working... time to re-visit C!!!

November 22, 2009
by mikedoug
mikedoug's Avatar

Let us know if you get stuck and need some help. A good place to start would be the nerdkit guide that you get when you buy the nerdkit -- it tells you what to download, and has some starter projects that walk you through the setup and whatnot to actually program the processor. ALL of my projects have used those projects as a starting point, and then modify it to do what I want.

Good Luck!


November 22, 2009
by tech20
tech20's Avatar

Just to note, most compilers for C and C++ convert the C to assembly, then that to hex to bin to upload or run. With asm it just coverts that straight to either of those and can then be uploaded.

November 23, 2009
by BobaMosfet
BobaMosfet's Avatar

Here's another note, since it will help you (John) orient:

Visual Basic is an interpreted language-- that means it's essentially a text file that another program reads, line by line, and then converts to symbols, and then executes real low-level machine code based on what your VB script tells it to do-- that is why VB is slow. It's easy to right in- but it is extremely inefficient.

C and C++ are compiled languages. It means that if you write in either, you create a text file. When you compile it, the compiler (in 2 or nowadays just 1 pass) converts the code to symbols, builds a symbol table, a jump or link table, calculates relative addresses and then if the syntax check passes, it then converts it into machine language via the assembler. Machine language and assembly language are synonymous. And in fact, machine language is a direct translation of hardware, in software form. It represents gate logic.

Because a compiler "compiles" (eg, tokenizes and then assembles) code to a processor's native format- it runs extremely fast.

Note that some processors, for example, Motorola, .v. Intel, have bytes in reverse order when describing numbers. Normally, your compiler will glaze over this for you, so you don't have to think about it, unless you are writing inline assembly code.

Instructions in assembly language (old hands just say 'assembler') are called 'mnemonics' (pronounced "new mon ix") The arguments that are passed with each mnemonic are called operands, and it keeps track of things with hard-wired memory locations called 'registers'.

Assembly is fun and easy. One of the quickest ways to learn assembly, is to a) get a reference for your particular processor, so you see the syntax you are to use, and then b) turn your compiler's option on that makes it create a .asm (or assembly) file. It's just another text file, but you can open it and look at it, and it will have all the assembly language code with all the symbols (variable names, function names, register names, etc) so it's easier to read and understand. You can then sit down with a printout of your C/C++ code and your .asm file and compare each to see what the compiler turned your code into-- and you will be able to see how it makes 'IFs' 'WHILEs', 'SWITCH:CASEs', etc.

The fundamental difference between C and C++ is this: C expects YOU to be responsible and have an organized mind with how you make things- it is strict. C++ lets you code in (what some could call a 'sloppy') less restrictive manner, because it offloads the responsibility from you to the compiler. As such, depending on the quality of the compiler, it may or may not be able to optimize C++ as effeciently as C, because with C, it assumes you know what you are doing- it makes no assumptions and optimizes accordingly, whereas with C++ it has to assume some things because it's trying to figure out what you're doing, so it can optimize effeciently.

Unless you have a pressing need, I wouldn't worry about C .v. C++. Use the one you are comfortable with until you get your wheels spinning.

Best of luck to you.

Post a Reply

Please log in to post a reply.

Did you know that you can use a transistor to interface between different voltage levels of digital logic? Learn more...