Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Assembly vs. C?

  1. Apr 28, 2013 #1
    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 :tongue:
     
  2. jcsd
  3. Apr 28, 2013 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    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.
     
  4. Apr 28, 2013 #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.
     
  5. Apr 28, 2013 #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.
     
  6. Apr 28, 2013 #5

    jim hardy

    User Avatar
    Science Advisor
    Gold Member
    2016 Award

    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.
     
  7. Apr 28, 2013 #6

    nsaspook

    User Avatar
    Science Advisor

    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.
     
  8. Apr 28, 2013 #7
    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.
     
  9. Apr 28, 2013 #8

    OmCheeto

    User Avatar
    Gold Member
    2016 Award

    :rofl:

    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:

    ps. Whatever you do, don't listen to me.
     
  10. Apr 29, 2013 #9
    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:
     
  11. Apr 29, 2013 #10
    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.
     
  12. Apr 29, 2013 #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:
     
  13. Apr 29, 2013 #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.
     
  14. Apr 30, 2013 #13
    So the available manpower was simply insufficient.
    One normally does not have a time-to-market frame of several years.
     
  15. Apr 30, 2013 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Assembly vs. C?
  1. Fortran vs C for CFD (Replies: 2)

Loading...