Starting to learn assembly language

(instructions, reset, WDT, specifications...) PIC10F2xx, PIC12F5xx, PIC16F5x

Starting to learn assembly language

Postby LadyInRed_2014 » Mon Oct 13, 2014 8:49 pm


I am new on this forum and also new to learn pic asembly programming.
So, to learn asembly programming i start to make simple programs, like the program below (add the first ten numbers).
For this program i select PIC16F916 because i have a development board with this controller.


movlw 0x00
movwf MYVAR1
addwf MYVAR1, 0

Label incf MYVAR1, 1
addwf MYVAR1, 0
btfss MYVAR1, 3
goto Label
btfss MYVAR1, 1
goto Label


The program is ok.
But now i try to make a program to add the first 100 numbers.
Because the registers are on 8 bits of course that the result is not that i expected (overflow).
So, there is a solution to make the sum of the first 100 numbers ?
Look what i have done :

movlw 0x00
movwf MYVAR1
addwf MYVAR1, 0

Label incf MYVAR1, 1
addwf MYVAR1, 0
btfss MYVAR1, 6
goto Label
btfss MYVAR1, 5
goto Label
btfss MYVAR1, 2
goto Label

Posts: 1
Joined: Mon Oct 13, 2014 8:13 pm

Re: Starting to learn assembly language

Postby tunelabguy » Tue Oct 14, 2014 4:46 am

Try to get into the habit of naming variables with as much care as you would give in naming your first-born son. Variables like MyVar1, MyVar2, etc. are like calling your kids Boy1, Boy2, etc. Even when playing around with educational programs like this, it will help you immensely if you name variables according to what function they serve in the program. Also add some comments. I can't see how you are adding the first 10 numbers. What's with all that bit testing in MyVar1? It is really strange way to count to 10.
User avatar
Verified identity
Posts: 29
Joined: Sun Jul 20, 2014 9:41 pm
PIC experience: Professional 5+ years with MCHP products

Re: Starting to learn assembly language

Postby drh » Tue Oct 14, 2014 3:11 pm

User avatar
Verified identity
Posts: 55
Joined: Tue May 27, 2014 3:31 pm
Location: Hemet, Calif.
PIC experience: Professional 5+ years with MCHP products

Re: Starting to learn assembly language

Postby Olin Lathrop » Tue Oct 14, 2014 3:40 pm

Argh! I started writing a detailed reply and wanted to write a list, so clicked on the BBCode link to the right of the edit window. That didn't pop up a new window as expected, and when I got back to the edit window, everything I had typed was gone. How are you supposed to look up the formatting codes when you actually need them, which is when you're writing a post?

Anyway, in much more stripped down format:

* Format your code properly. Labels must start in column 1, but opcodes and the start of the operands should be in specific columns. Everything mushed together as in your post makes the code hard to read.

* Put a little thought into variable names. "Myvar", really!?

* Don't write ",1" or ",0" as second parameter to opcodes like ADDWF and the like. Use ",w" or ",f", or even better, leave the parameter off if you want ",f" since that's the default. No, I'm not going to look up which one is 1 and which one is 0 to understand your code. Remeber that everyone here is a volunteer.

* Use comments. You may think what the program is doing is obvious. Sometimes it may be easy to see what the code is doing, but that still doesn't tell us what you intended it to do. And make sure the comments are meaningful. We don't want to see stuff like "ADDLW 8 ;add 8 to W". We can see that from the bare instruction. Explain what the purpose is, like "ADDLW 8 ;count another byte worth of bits processed".

* Your code never defined MYVAR

* There is no evidence that execution will actually start at START.
User avatar
Olin Lathrop
Verified identity
Posts: 48
Joined: Fri May 30, 2014 3:38 pm
Location: Littleton, MA USA
PIC experience: Professional 5+ years with MCHP products

Re: Starting to learn assembly language

Postby larny » Thu Oct 23, 2014 9:58 am

I suggest you have a look at Nigel Goodwin's tutorials.

Here is a sample of code from one of the programmes I wrote recently.

Code: Select all
   cblock   0x20           ;start of general purpose registers
            count_a      ;used in delay routine
            count_b      ;switch setting
            boolean      ;logic indicator bits

#define    tmr0_of   INTCON, TMR0IF   ;TMR0 counter overflow flag
#define    tmr1_of   PIR1, TMR1IF   ;TMR1 counter overflow flag

#define    c_bit   STATUS, C
#define    z_bit   STATUS, Z
#define    tmr1_on   T1CON, 0   ;control TMR1

#define    pwr PORTA, 0   ;maintain power to PIC
#define    sol  PORTA, 1   ;drives the Solenoid or Relay
#define    sw  PORTA, 2   ;apply high to switch & LK1
#define    LED PORTc, 5   ;LED   
#define    perm_high     boolean, 0

   ORG     0x00

   call   Initialise
   bsf      pwr      ;maintain power to PIC
   bsf      LED

   call   delay_5s         ;5.05 sec LED on time (also allows the oscillator to start)
   bcf      LED         ;LED off
   clrf   count_b
;end 5.1 sec delay *****************************************
   call   chk_sw   ;determine the switch position
   movf   count_b, w
   btfsc   z_bit   ;is count_b 0?
   goto   cce      ;Yes - error switch open or set to 0
   btfsc   perm_high    ;is the sol output to be permanently high?
   bsf      sol         ;yes
Posts: 2
Joined: Thu Oct 23, 2014 5:36 am
PIC experience: Experienced Hobbyist

Re: Starting to learn assembly language

Postby bxdobs » Tue Nov 25, 2014 3:39 am

I have been away from PIC programming for almost 10 years ... published back in 1994 for the 16C84 ... while the PIC RISC Asm mnemonics are simple to master, the MPLAB X IDE for MPASM provides some useful ways to make this even simpler ... with the attached ASM Macro Library (inspired by Karl Lunt's Macra Library from May 1999), C like code can be easily written without the C overhead and without having to get down to the more complicated BIT bashing modes which can be tricky to debug.

I just completed a project for a 10F206 which used 300 lines of macro code (this filled the program memory) ... an XC8 version of the this same code won't fit on this chip ... half the C source uses up all the memory This amount of ASM code took about 1 week to code and test once I proofed the Macro's.
(24.69 KiB) Downloaded 75 times
Posts: 3
Joined: Sun Oct 26, 2014 5:08 am
PIC experience: Experienced Hobbyist

Return to 12-Bit Core

Who is online

Users browsing this forum: No registered users and 1 guest