Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Assembly / C?

Status
Not open for further replies.

chancsy

New Member
Hi everyone I'm new here. Currently studying Telecommunications.

And my question is,
What is the disadvantage of using C language and a compiler when writing my codes rather than writing it in assembly? Are there some restrictions when using a higher level language? I learned not much about PIC assembly and prefer C because of its simplicity. Can anyone please explain to me?:confused:

I only have experience using a PIC16F84 and PIC16F877A (with bootloader).


Thanks in advance.
 
Sorry looks like most of my questions in my head were answered in the Newcomers thread, sorry all.
If resources (ROM/RAM) are not a problem, and I'm developing my own codes, still, which is better?
 
chancsy said:
Sorry looks like most of my questions in my head were answered in the Newcomers thread, sorry all.
If resources (ROM/RAM) are not a problem, and I'm developing my own codes, still, which is better?
There is no better, really. I love assembler, but lately I've been writing everything in C. Tends to get things done quicker with less detail work necessary.

On the other hand, the code is a bit slower (usually doesn't matter) and somewhat larger (again, usually doesn't matter). And you don't have fine control over timing on some things like in assembler. But inline assembly can take care of that.

Use whatever suits you, and the project.
 
When you write in C you are using other peoples macros/obj/code (their ASM). It is a lot simpler that way. Saves you time.

If there is a bug in their compiler it will be in your code. But today, if it is not on a large scale project (Windows, etc) that is not a problem. C is fine. A little bloat for the time savings.

But ASM is all you and you do not have to rebuild, update and all when they send you the update to the C compiler.

Now when I use assembler, I expect some bugs in my code. That is the fun part, finding them :)

It is all about speed and size of the program (that means nothing today on a desktop computer), however ASM requires you to map it all out. And the interrupts are all yours for the detailed work.

Guess the question might be what are you planning to do? If PICs then C is fine.
 
The primary difference is that assembly written code will usually take up less space and be faster. This assumes, of course, you have a competent person writing the assembly program.

C compilers make code development much easier and faster, but the code they produce may not be optimized for program size or execution time.

The decision on which to use depends on your needs.

If you are a small business making prototypes and very low volume products, C is by far the preferred language. With such small volumes the cost of a larger, faster processor is of no real consequence - it will be dwarfed by labor costs.

If you are going to make a million units annually of a product, you want to minimize the cost of every part in it down to the tenth of a cent level. The cost of a larger, faster processor is absolutely not acceptable. All this programming will be done in assembly to maximize performance so you can use the cheapest hardware possible.

It should also be noted that the above, while true, overstates the difference between the code produced by a good C compiler and a good assembly programmer. In fact, some of the best code is written in C, compiled, and then optimized at the assembly level. Coding in C and in assembly level are not mutually exclusive. A good embedded systems programmer will know when typing a statement in C what assembly will be produced when using his or her chosen compiler.

Easy example: What's the difference in the code produced when using a switch() statement vs. nested if/else statements? What's the big-Oh performance? If you don't know this, you should probably study a little more before comfortably becoming a C-only programmer.
 
Last edited:
Oh thank you all especially speakerguy79.
I do really don't know what's the difference in assembly for switch and if/else.
Looks like what I really need is more experience with programming so I can answer the question myself =)

Thank you everyone for your time and patience.
 
Assembly relies on your skill to optimize speed and program size. C utilizes the skill of a compiler writing team.

It's usually quicker to design in C, but I always inspect the ASM to make sure I'm getting fast, small code (if it matters). You can inadvertently write C which tricks some compilers into generating slow or large code.
 
speakerguy79 said:
A good embedded systems programmer will know when typing a statement in C what assembly will be produced when using his or her chosen compiler.
Agree wholeheartedly here... it's why I think compilers should be taught as a part of a good college/university course. I have seen many people chasing bugs in their code for weeks because they are unable to find a compiler bug. All good programmers should be prepared to suspect their tools and know how they work.
Easy example: What's the difference in the code produced when using a switch() statement vs. nested if/else statements? What's the big-Oh performance? If you don't know this, you should probably study a little more before comfortably becoming a C-only programmer.
...and this is completely compiler and target CPU/uC-dependent so can either be done by inspection of the code or reading the FAQs/commentaries on forums such as this. For instance the Microchip MC18 compiler generates better code for nested if's than it does for if's with long boolean expressions... and I thank whoever posted that tidbit :)

Paul
 
So let's say I am skilled in ASM and that means I know what the compiler is doing *hours in debug*?

Why would I just not do it in ASM?

I am with mneary. You want speed and small size use ASM. But you must know ASM. I would learn that first.

Then get lazy and do one-off projects in a simpler format.

I use compilers because I am old, slow and doing one project at a time.

Real programmers use ASM. If not, that is probably why my car is not running right.
 
In general employed programmers do not do all their programming in assembly because it takes too long. Most compilers are a lot better in terms of bugs then they were in the past. If there is a bug chances are the programmer is to blame. Most of the time he will be able to debug it at the C level.

There are cases where looking at and understanding the machine code is a must. How often that happens depends on how good you are, how good your compiler is, and what you are writing. If you spend a lot of time trying to second guess the compiler there is a problem with you or the compiler.

I am not pushing C first over ASM.



mramos1 said:
So let's say I am skilled in ASM and that means I know what the compiler is doing *hours in debug*?

Why would I just not do it in ASM?

I am with mneary. You want speed and small size use ASM. But you must know ASM. I would learn that first.

Then get lazy and do one-off projects in a simpler format.

I use compilers because I am old, slow and doing one project at a time.

Real programmers use ASM. If not, that is probably why my car is not running right.
 
I have never programmed in assembly for a job. I've only done it while studying for my engineering degree. They beat assembly and machine code into us for about two semesters, then let us use C compilers for our upper division electives. All the courses were 68HC12 based.
 
REAL programmers are skilled in ASM and a variety of high level languages and know exactly which to use and when.

Many programmers trained in the past 10 years were not taught ASM or compiler theory or operating systems... thanks to the commoditisation of the IT field. Many of those same young programmers won't learn any technology for which there is no obvious "career path". I was just talking to someone bemoaning this... he can't convince young programmers out of college to learn Perl because it's not Java, C# etc. etc. so they believe it won't add to their resume in a meaningful way.

This kind of thinking has fed back into colleges who know they need those same buzz-words to attract students. Few students will be excited by "they're going to teach me ASM!" vs "they're going to teach me Java!" And so ASM is dropped...

REAL programmers are good at abstracting problems into any language... much better programmers can do so within very tight constraints such as those in a uC (RAM/ROM/speed etc.)

Paul
 
mramos1 said:
Now when I use assembler, I expect some bugs in my code. That is the fun part, finding them :)

interesting quote, but sometimes it will kill me when there's too many of them..

anyway, I believe knowing both ASM and C/BASIC is a good thing.. it is really straight forward to write in C than ASM... the later just takes too much time to build everything from scratch. :D
 
mramos1 said:
Why would I just not do it in ASM?
...
I use compilers because I am old, slow and doing one project at a time.

There is a lot of every day "benefits" of using high level language over the ASM. The "only" benefits of ASM are speed/size/"you know what is going on" .. but in everyday work .. changing the simple thing as frequency of the oscillator will require code rewrite, not to mention changing MCU .. with high level language, you just need to recompile most of the times ...

so, imho, high level languages (c for example) offer more "portability", it is easier to create library of "useful routines" that are "universal" meaning, they will work on almost all MCU's etc..

generally if you work on 2-3 MCU's (as most embedded dev companies do ) only then there is a good reason for using ASM, but if you use variety of MCU's (like RND companies do) then some high level lang is more suitable ..

As someone already said, if you decide to use high level language, you have to know your compiler ... I used 4 different C compilers and still trying to stay with only one. I use 2 attm, one produce slightly better code but all libraries are "close source" and that is a big minus imo, the other one have all lib's came with source, but, it produces bigger/slower code ..
 
mramos1 said:
So let's say I am skilled in ASM and that means I know what the compiler is doing *hours in debug*?

Why would I just not do it in ASM?

I am with mneary. You want speed and small size use ASM. But you must know ASM. I would learn that first.

Then get lazy and do one-off projects in a simpler format.

I use compilers because I am old, slow and doing one project at a time.

Real programmers use ASM. If not, that is probably why my car is not running right.


Real programmers do what is necessary to get the job done, and don't waste unnecessary time programming something that can be done 10 times faster in a higher level language. Assembly has it's places, it's just another language (which can be considered highly inefficient in terms of programmer productivity for many applications).
 
Salgat said:
Real programmers do what is necessary to get the job done, and don't waste unnecessary time programming something that can be done 10 times faster in a higher level language. Assembly has it's places, it's just another language (which can be considered highly inefficient in terms of programmer productivity for many applications).

Unfortunately though this produced massively blotted slow running code, such as Windows! :p
 
WOW-Good opinions

I love sitting back and reading these opinions.

You have the ASM side

You have the C side.

You have the "sitting on the fence" guys.

All trying to convince the other side, of it's own merits.

Learn both, enrich yourself, try both, then, go with what's best for you after you walk in another mans shoes.
 
A smart compiler might, in some instances, be better than someone coding assembly.

Let's say you wanted to divide a number by 4. Think of one way on a PIC18 device that a compiler might make this faster than someone coding in assembly.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top