Advice for a Newbie Programming PICs: Assemble or C?

  • Thread starter freddyfish
  • Start date
  • Tags
    Programming
In summary: C++, but it is not easy. I would not want to work in C++ unless I had a very good IDE. So I recommend C++ and Visual Studio, but it is not easy. In the end it is worth it.In summary, the conversation discusses the pros and cons of learning assembly code or C for programming PICs. Some suggest learning both, while others recommend focusing on C for its easier learning curve and support for structured programming.
  • #1
freddyfish
57
0
Hi

I am asking the question here since I am hoping there are some robotic engineers (or anyone else) that may have some good tips. I am pretty new to the area of programming PICs and the "lessons" I have found is about assembly code. I have read that assembly code is better when it comes to performance, but C is "easier to handle" and work with. I am not asking about a way to minimize the effort needed to learn programming the PICs, rather I am asking for some advice from experienced programmers about whether I should learn assemble, if I should skip ahead to C or maybe learn both.

If you strongly recommend C, then I would welcome any suggestions on literature etc.

Thanks :-p
 
Engineering news on Phys.org
  • #2
If you want to understand how computers work, learn assembly. The statements like "C is closer to machine language than other high level languages" is true but VERY misleading. If you take higher level languages as being 100 to 200 miles away from machine language, the yeah, C is maybe only 85 miles away, but it still isn't anywhere close to being machine language or requiring you to learn how registers work and things like that.
 
  • #3
When I started learning C, I failed to grasp
"The C Programming Language"
by K&R.

After some efforts spent on improving on two assembly dialects, working again with that book still was not piece of cake, but it had become a manageable task.

BEWARE!
Underlying that is my, inherently subjective, way of perception.
That's not guaranteed to hold for you, but we cannot do better than relaying personal experience, or do we.
 
  • #4
Learn both. If you can write a working program in Assembly, then C would be a like drinking a glass of water just if you know how to write a code in C. Writing in C would be easier and less time consuming, but most of the time you will not be able to see what really happens at the machine language level and how the whole read/write process on the file registers happens in detail.
 
  • #5
Learn assembly. It'll teach you a LOT and it'll make you appreciate high level.

When you get good you'll directly read the machine code in hex.

Watch out, programming can be addictive.
 
  • #6
My2c

At the PIC controller level knowing some assembly is very important to understand what a higher level language like C is doing when debugging programs. But understanding an ASM language is not the same as designing programs in that language. Every interface chip or storage device has it's own unique set of commands, feature set and registers so just knowing C or a general ASM language for CPU would be incomplete without libraries of interface code to talk to the outside world. Most chip hardware vendors today have very extensive libraries of C code but most only give the the tools to write your own or link to the C ones in assembly.

While it's possible to program large complex projects in assembly I would say once you get to the point in a software design where the manipulation and structure of data is as or more important than pure algorithmic performance, C or most languages designed for systems programming will allow you to design data structures efficiently and naturally at the bit level because you can abstract the problem in a human way of thinking instead of thinking like a machine. Most of the really hard bugs in large programs are data driven so the ability to control access to data structures and limit data interaction at the source code level eases the ability to write code that not only works but can fail in a (mainly) predictable way.

When coding for PIC type micro-controllers, writing OS drivers or bit-banging low level hardware in a kernel knowing ASM is important but a 'C' OS like Linux currently has very little ASM code except for what is needed to abstract a usable CPU model for the rest of the OS written in C to build on.

So it's not really assembly vs C, it's what level of direct hardware interaction you need in both to be successful at your project.
 
  • #7
freddyfish said:
Hi

I am asking the question here since I am hoping there are some robotic engineers (or anyone else) that may have some good tips. I am pretty new to the area of programming PICs and the "lessons" I have found is about assembly code. I have read that assembly code is better when it comes to performance, but C is "easier to handle" and work with. I am not asking about a way to minimize the effort needed to learn programming the PICs, rather I am asking for some advice from experienced programmers about whether I should learn assemble, if I should skip ahead to C or maybe learn both.

If you strongly recommend C, then I would welcome any suggestions on literature etc.

Thanks :-p

Hmmm. It depends.

I learned assembly language first, and it was a nightmare. Make one tiny mistake and you're screwed. It is very verbose and demanding. It was a terrible experience, and I never did it again.

BUT if assembly language is what you need, then there you go. So let's assume that this is what you need. The question is, how best to learn?

The basic question is whether you have solid structure and design in your programs. In my opinion most programmers hack in an undisciplined way. They do not plan ahead and their programs never work reliably. I used to be amazed by the abysmal quality of very popular programs. Recently I have been using Google groups and it just keeps getting worse. Bad programming is so prevalent that many programmers think that this is simply the way it is, and nothing can be done about it.

What I ended up doing was programming in C++, which has a lot of support for strong structure. It is possible to write highly modifiable and reliable programs with this. Look inside a modern computer and you will see strong structure. You will see independent units with very well-defined interfaces. It's like that.

In unstructured languages you tend to get unstructured programs, and that is like the inside of a radio from the 1930's. Wires going all over the place.

If I were ever to write assembly language again I would write it in a highly disciplined and structured manner. There is nothing that is going to stop you from doing this, it is just that the language gives you no support at all. If you start out writing in assembly language it will not help you to learn structure and discipline. In fact it will be a big impediment.

So should you start with C++? The trouble with that is that most programmers never learn to use it. They hack at it and the result is worse than a pure hack. Think of that computer with the circuit boards, then add a bunch of random wires connecting all the stuff together. The worst of both worlds. So if that is the result, you would be wasting your time. It takes years to get good at it, too.

C was developed as a replacement for assembly language. It's two huge advantages is that it can be ported from one computer to the other, and it isn't so verbose. It is a lot more structured than assembly language and much easier to learn and work with. So I would start with that and learn to write modifiable and reliable programs. They didn't teach me that in school -- I went to Harvard, but programming teaching was terrible, a nightmare -- I had to learn it from books and years of on-the-job training. Then I would write assembly language in that style.

Programmers will tell you how inefficient C is, and that is true, but it is also true that 90% of the time goes into 10% of the code. Over and over I have seen "optimizations" that have no significant effect other than to introduce errors and make the system very difficult to work with. I have fallen into this trap myself. Get it to work reliably first, then find out where the slow parts are and do something about it. If you need to write those in assembler, fine. It is quite possible to mix together C and assembly language.

Programmers tend to focus in minutia and miss the big picture. Don't listen to them.
 
  • #8
ImaLooser said:
...

Programmers tend to focus in minutia and miss the big picture. Don't listen to them.

:smile:

We should graph the age of all the responders vs their response.

Assembly language was the second language I learned, back in around 1982. My computer had an instruction set of 59. It took me less than a month to learn. I subsequently wrote thousands of Assembly language programs. Mostly for fun.

I then learned somewhere between 3 and 5 versions of C over the next decade. I didn't write very many C programs, but the ones I did, were monsters in comparison to my ASM programs. In 1991, my first term CS project was a 20 page long data base manager. Everyone else wrote a "Hello World" program. I improved it over the years, and still use it at work to this day. This is one of the nicest things about programming. You get to decide how the program functions. And if it doesn't do what you want, you don't have to wait on the IT department to fix it. I hate programmers. It's impossible to explain to them what you want things to do.

I've never heard of a "PIC" before today, but assembly sounds right for your needs:

Instruction set
A PIC's instructions vary from about 35 instructions for the low-end PICs to over 80 instructions for the high-end PICs.

ps. Whatever you do, don't listen to me.
 
  • #9
ImaLooser said:
Programmers tend to focus in minutia and miss the big picture.

The rumor mill has:
"Even the biggest pictures do not translate themselves into good code."

Pls attend e.g. Death by UML fever.

To disprove these, totally absurd and heretic, hypotheses by counterexample we plan to invest more money in more and fancier tools to paint more and bigger pictures soon.

Yeah! That's the sprit...
:biggrin:
 
  • #10
Solkar said:
The rumor mill has:
"Even the biggest pictures do not translate themselves into good code."

Pls attend e.g. Death by UML fever.

To disprove these, totally absurd and heretic, hypotheses by counterexample we plan to invest more money in more and fancier tools to paint more and bigger pictures soon.

Yeah! That's the sprit...
:biggrin:

It took me years to write the programs I was working on. They were big. If you don't have a plan for a project like that, then you're a dead duck.

Little one-day or one-week hacks are totally different. You can get away with a lot.
 
  • #11
Thank you guys very much! I really appreciate your time and effort. I seem to have quite a bit to go, but soon I may have my own robot army. :biggrin:
 
  • #12
One quick note:
There is now such thing as "Assembly Language". The Assembly language of a specific CPU exists, but there is not a universal assembly language. Of course, most processors use most of the same mnemonics, so it is not very difficult to go from one MicroController to the other.

However, learning assembly language requires one to learn how the processor functions in detail and the instruction set architecture that it has.
 
  • #13
ImaLooser said:
It took me years to write the programs I was working on.
So the available manpower was simply insufficient.
One normally does not have a time-to-market frame of several years.
 
  • #14
For me learning Assembly taught me a lot. You'll see how abstract high-level languages get (including C), since at the root level there are only a few instructions you can use.

For what you want to do, since you say that there are programs written in Assembly, you may need both. You can't really just learn Assembly since you'll need to know a high-level language at some point, and C/C++ is the best you can learn since it's so widely used and efficient relatively to other high-level languages. In whatever programming area you are, it's very hard to not find a program written in C.
 

FAQ: Advice for a Newbie Programming PICs: Assemble or C?

What is the difference between assembling and programming PICs?

The term "assembling" refers to the process of converting human-readable assembly code into machine-readable object code, which can then be programmed onto a PIC microcontroller. Programming, on the other hand, refers to the act of loading the object code onto the PIC's memory so that it can execute the instructions.

Which method is better for a newbie programmer?

It ultimately depends on the individual's preferences and goals. Assembling requires a strong understanding of assembly language, while programming may be easier for those with prior experience in high-level programming languages. It's recommended for newbies to start with programming, as it allows for a faster learning curve and a better understanding of the PIC's capabilities.

What are the advantages and disadvantages of assembling?

The main advantage of assembling is that it allows for more efficient use of the PIC's resources, as assembly code can be fine-tuned for specific tasks. However, it is a more time-consuming and complex process, requiring a deep understanding of assembly language and the PIC architecture. Additionally, debugging assembly code can be more challenging compared to high-level languages.

Can I use both methods for the same project?

Yes, it is possible to mix and match assembly and high-level programming in the same project. For example, you may choose to write the core functions in assembly for efficiency and use a high-level language for more complex or abstract tasks. However, it is important to have a good understanding of both methods and how they interact with each other.

Are there any resources available for learning how to program PICs?

Yes, there are many online tutorials, forums, and books available for learning how to program PICs. Some popular resources include Microchip's official website, the PIC microcontroller forums, and various YouTube channels dedicated to PIC programming. It's also recommended to have a good grasp of the C programming language, as it is commonly used for PIC programming.

Similar threads

Replies
2
Views
1K
Replies
4
Views
1K
Replies
12
Views
2K
Replies
13
Views
1K
Replies
5
Views
2K
Back
Top