Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Where can I find out the following about PIC assembly

Status
Not open for further replies.

matrixofdynamism

Advanced Member level 2
Joined
Apr 17, 2011
Messages
593
Helped
24
Reputation
48
Reaction score
23
Trophy points
1,298
Activity points
7,681
- Are varaibles declared as 8 bit on 8 bit PICs and 16 bit on 16 bit PICs? If so, how does one declare an int or long and do arithmatic with these in assembly on an 8 bit PIC?
- How does one write a function that takes parameters and return a value in assembly? If mean if only use "goto", that is not like calling a function anymore.
- Does assembly have concept of variable scope e.g variables inside functions cannot be accessed from outside in C?
- What are steps to add two numbers in assembly together? Does assembly have concept of type i,e signed, unsigned, float, int, short e.t.c?
- What is the most important thing(s) that a person moving from PIC C to PIC Assembly needs to know before they delve into it?
 

hello

What is the most important thing(s) that a person moving from PIC C to PIC Assembly needs to know before they delve into it?

to deeply study the specsheet data of the used PIC , because 100% hardware dependant
RAM area , page organisation
all registers
EEPROM area
Config bit definitions
Hardware specifications
Registers
... etc ...

if you can read french .. see BiGiONOFF courses about PIC & ASM
 

- Are varaibles declared as 8 bit on 8 bit PICs and 16 bit on 16 bit PICs? If so, how does one declare an int or long and do arithmatic with these in assembly on an 8 bit PIC?
Variables are whatever size you want them to be, the logic and arithmetic operations on them are normally done in the size of the registers. In other words, you specify the storage size of the variable in RAM and write whatever code is needed to manipulate it using register sized chunks. Ints and longs do not have any meaning in assembly language but if for example you wanted a 16-bit variable, typically you would assign two adjacent 8-bit bytes to hold it.

- How does one write a function that takes parameters and return a value in assembly? If mean if only use "goto", that is not like calling a function anymore.
You can pass and return values in registers. For parameters bigger than a register you save them in memory, call your function and if necessary return the results in memory. Memory is accessible from anywhere in the program so your function and the remainder of the program can access it at any time. Incidentally, "goto" in PIC language means load the following address value into the program counter, forcing the next instruction to be picked up from that address, it does not take any parameters.

- Does assembly have concept of variable scope e.g variables inside functions cannot be accessed from outside in C?
Scope has nothing to do with the program itself, it is a concept only understood by the assembler so it will vary from one assembler to another. Most people use MPLAB or MPLABX from Microchip, it does allow for local variable names within routines.

- What are steps to add two numbers in assembly together? Does assembly have concept of type i,e signed, unsigned, float, int, short e.t.c?
They do not exist at assemble language level, everything is treated equally. It is up to you how the data is used. To add numbers, typically you would load one into the W register and use the ADDWF instruction to add it to the address the other variable is held. You can save the result in W or the variable address depending on what you set the destination field to. The STATUS bits are updated after a math instruction so you can check for a result of zero and whether a carry was generated. Your code would be responsible for checking the carry bit and if necessary, propagating it through any additional digits in your variable.

- What is the most important thing(s) that a person moving from PIC C to PIC Assembly needs to know before they delve into it?
In my opinion the most important thing is to realize that a C compiler produces assembly language code which is then assembled into your final program. It means anything you can do in C is possible in assembly, you just bypass the high level constructs that C gives you. The big advantage of that is you are no longer weighed down by the 'glue' needed by C to hold the program together so you can write faster and smaller programs. You can still use libraries of functions, in fact you can use the C libraries if you are careful but after some practice you will find you make your own routines and save them for reuse in other programs. Because you have absolute control over the program execution, you can also make exact predictions about timing, you will find this important if you delve into control systems where signal sequences and precise delays are essential.

As Paulfjujo stated, you should make yourself familar with the data sheet for the device you want to use. Although the PIC families have very similar instruction sets, there are of course differences to allow their different peripherals work.

Brian.
 
What is the most important thing(s) that a person moving from PIC C to PIC Assembly needs to know before they delve into it?

High level languages are built in such a manner that give us a comprehensive understanding of its commands, so that we can write a code much more intuitively, without need to perform the programming of intermediate steps required to achieve certain functions. In other words, the compiler automatically makes for you the assembling of these repetitive constructs, eliminating the possibility to occur errors on code due to the 'human fault'.

The Assembly language allow us to perform operations with more flexibility than if compared to C language, but we have to pay the price to deal with the register level of programing, running into the risk to lose the focus of the algorithm itself; also, another drawback is the lose of code portability if in the future you decide migrate to another core.

Considering the powerfulness of nowadays compilers which supports several levels of optimizations, a priori would not make any sense you think about downgrade from C to Assembly language without a particular reason which motivate that decision.
 
I have been writing C programs for years now. I want to taste assembly and that is why I am asking about it here. It is just for fun really.
I did expect the response to warn be about bank switching or tell me about some very common mistake people make when writing PIC programs in assembly. Anyway, I shall read the datasheet and then reply.
 

I worked with PIC16F family for a time in small designs, and I can say it instructions set, differently from other 8-bit cores such as 51, is quite easy to learn. As yourself pointed out, you have to be always watchful to the bank selection for addressing SFR registers. Another common concern that you have to manage is the limited resources of the uC, especially the RAM memory and Stack size.
 
Bank switching is easy if you use MPLAB or MPLABX (different IDE but same assembler under the hood) because a macro 'banksel' is provided as a directive. For example, if you want to write to PORTA you can precede it with 'banksel PORTA' to ensure the bank is set correctly. The trick, which you master with experience is when NOT to use banksel, you only have to use it if you know the bank has to be changed so for example, if you use 'banksel PORTA' you don't need to use 'banksel PORTB' to access PORTB because it is in the same bank as PORTA. Banksel works with any register name and saves you having to refer to the data sheet to find which bank a register is in. Incidentally, the whole concept of banking only applies to PIC10, PIC12, PIC14 and PIC16 families, the other families have linear addressable memory so although 'banks' still exist in them, they are only used as a way to shorten some instruction lengths and can be ignored in many applications.

People hate it but banking is actually a very efficient way of accessing large address space with small registers. When you work out what it does it can be your friend for life! Think of it this way, a "char *pointer" can only 'point' to as much space as a char sized variable can bound, typically 256 addresses, a banksel is like a pre-selector for where *pointer starts from.

I strongly advise you to use MPLAB or MPLABX if you want to play with assembly language. MPLAB is Windows only and although still very popular is officially obsolete, MPLABX is available on Windows, Linux and MAC platforms. You can download it free from Microchip's web site. Don't worry about licensing costs, the free version is more than adequate for almost all applications.

Brian.
 
To add to the post from betwixt, back when I was programming in assembly, I made a chart that listed the register and which bank it was in. It made the use of "banksel" quite easy.
 

Attachments

  • banksel.jpg
    banksel.jpg
    74.6 KB · Views: 59
There is a chart like that one in all the Micropchip data sheets in the 'memory organization' section. It gets a bit more complicated with some of the enhanced mid-range processors which have 32 banks! In fairness to Microchip, the register locations are fairly consistent throughout the whole range.

Brian.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top