views:

2177

answers:

3

I'm going to start working on a project where I need to have a decent understanding of Assembly language for the PIC microcontroller's. I'm intimately familiar with C/C++, so I know how to code for the most part, and I have done many projects for the PIC already so I understand the architecture, but have done all my programming for it in 'C'.

Is there a good book or website which does a good job explaining what all the Assembly commands mean and how to do fairly simple tasks (blink LED's, basic math, etc...) for the PIc microcontroller?

Edit: The primary purpose of this post is to request resources for learning Assembly, not debating the merits of C vs Assembly in the PIC's, or whether the PIC is a 'good' microcontroller to use. I am using a PIC18 microcontroller if that makes any difference.

+2  A: 

I would try this: Pic Tutorial

Will Marcouiller
This is what I used to learn some Assembly (for the PIC), however, those in the future be warned: I noticed some errors and some spelling mistakes in the tutorials. That being said, though, I still felt it was a good resource to "get a feel" for Assembly.Another good resource for learning some more Assembly was the "Intro to PIC C" by Nigel Gardner. Its a book about "C" but lists how some of the loops are done in Assembly, so its a good resource as well.
Seidleroni
A: 

I am a believer in writing a disassembler. Start with a very simple one or two line program that loads a register with a constant perhaps (gotta read a tutorial or something to learn that step). Assemble it. Save the binary in a format you are capable or willing to write a program to read (intel hex perhaps, or elf if they support that).

Write a program to read the binary file and extract the program then take those bytes and write a disassembler (even if the vendor has a disassembler you should still write one).

Now start iterating the process, learn a new instruction or a new way to use that instruction, one instruction at a time. Write code to disassemble that instruction or option. Try to write assembler to manipulate each of the bits in the instruction.

By the time you get through the instruction set you will know the instruction set better than most people that use it every day, you will know how to write assembler for each of the options for each of the opcodes, you may also learn why this instruction can only address N bytes from its location and others can access anything, or that instruction can only use an N bit immediate and others can use any value. That sort of thing.

I have used this process many times and learned many instruction sets, ymmv. After the first couple-three the process above may only take an afternoon to complete.

EDIT:

The goal here is education not the next great sourceforge project. The output can be as ugly or incomplete as you like you are the only one going to read it.

Note: A generic disassembler for variable length instruction sets can be somewhat difficult, you don't want to linearly disassemble the binary in that case you want to follow all the execution paths. I would avoid it. Taking simple programs that execute somewhat linearly assembling then disassembling is not difficult even on a variable length instruction set. You can learn quite a bit about an instruction set by disassembling and examining the output of a C compiler (or other high level language), if the compiler doesn't have an assembler output option or doesn't have a disassembler you may not get to take advantage of this (unless it is a fixed length instruction set).

Also note once you learn assembler for one processor, the second one is much easier, and so on. The things you need to learn from one to the next often become how big can this jump be, what are the rules on immediates, indirect addressing, basically all the things that are directly tied to examining the opcodes. You can learn it without looking at the opcodes, but you have to rely on the documentation or assembler error messages being high quality.

dwelch
Are you serious!? It sounds like the OP has to get work done; whose going to pay him to take the approach you suggest?
Clifford
Doing that in an afternoon sounds... fast.
Craig McQueen
yes, quite serious. programming is programming it is a matter of syntax, learning the syntax by reading and not doing is expensive and slow. Trying to write and run and debug programs just to learn an instruction is slow and expensive, this is a very cheap and fast and reliable solution. If you can find a way to learn an ISA inside and out in a few hours without any equipment other than a cheap computer and free compiler, go for it. (okay pdf reader and assembler)
dwelch
You may learn it well that way, but learning it "good enough" is more cost effective on a commercial project. Professional developers do need to be proficient, but at the same time have to understand that they are there to generate wealth, not to become experts in arcane technologies. On a significantly large project you will learn and become more proficient while being productive at the same time. If you are doing this as a hobby, do what you like. I have no doubt that many hobbyists are better assembler programmers that many professionals - because their motivation is different.
Clifford
+4  A: 

There is more that one PIC architecture, with significantly different instruction sets. Microchip do not have the architectural consistency of Atmel AVR for example. So you need to specify what you are using - PIC12, 16, 24, 32 for example.

So first of all I would suggest avoiding PIC assembler on the basis that what you learn on say a PIC12, that may not be very applicable to a PIC24. Secondly I would avoid using a PIC at all if I possibly could as a software developer - though I concede there are other considerations, and you may have no choice.

What you may have a choice over is not using assembler. While the lower end PICs are not perhaps best suited to C code generation, that's the compiler writer's problem; it is still more cost effective in terms of development time to use C where possible. On the other hand for high volume products, if you can fit the code into a smaller part by using assembler, that may be a factor. Deciding you need assembler before you have tested a C solution is often a 'premature optimisation'. Even if the C implementation fails size or time constraints, you can just call it a prototype, and recode it to meet constraints. Doing than may still be faster than starting assembler coding from scratch and struggling with both design and instruction set at the same time. Tell your boss you'll prototype it in C, then when it meets requirements, no one will want to waste money re-coding it is sone non-portable, unmaintainable assembler code.

Finally to answer your question, assuming you need to get work done quickly and efficiently, use Microchip's examples and app-notes as much as possible, and familiarise yourself with the instruction set from the manufacturers instruction set reference. The instruction set for the lower-end parts is not large. For day-to-day work I like to use an "Instruction set reference card" as an aide-mémoire - it summarises all the essential details of each instruction, usually in a couple of pages - print it double sided and laminate it! ;). Here is a PIC16 example

Clifford
I fully agree. I've done PIC programming in assembler, and it's actually not that hard to learn the instruction set. However, programming at such a low level is no joy--C is so much nicer. I'd go for C (and move away from PIC if necessary) unless there's a compelling business case for assembler.
Craig McQueen
For the smaller older pics, asm is fast, easy, better results than C. The new pic32 is derived from a mips so an all together different beast. Learning pic assembler can mean two or three different instruction sets so I absolutely agree you need to specify which PIC you are talking about.
dwelch
Even if you are programming in C, it really helps to know the underlying assembly so you can understand what the compiler will do with what you're typing.
Crashworks
@dwelch: Agreed, the lower end PICs asre not great C platforms, and the compilers are severely constrained. These are the ones you use when the "other considerations" (such as per unit cost) hold sway. Also re. the name PIC, it has become a marketing tool rather than an architecture - the PIC32 bearing no relationship to previous devices. I am not sure that the PIC32 has much of a chance against ARM of making great inroads to the 32bit market. @Crashworks: Yes in embedded systems especially that is true, but being able to read assembler does not require you to be able to write it.
Clifford