Author Topic: PIC ASM source code help please  (Read 7174 times)

0 Members and 1 Guest are viewing this topic.

Offline glossywhiteTopic starter

  • Regular Contributor
  • *
  • !
  • Posts: 241
PIC ASM source code help please
« on: January 25, 2011, 06:23:56 pm »
Hi there. I have some assembly source which came with my Velleman PIC programmer. I understand everything except for this; why are they inserting DECIMAL 100 as a delay value, and by what value is it decrementing, when decrementing by F?

Am I stupid?

Code: [Select]
;		*********************************************
; *  Example of a delay routine               *
; *********************************************

DELAY_ROUTINE   MOVLW   D'100'         ;54 Generate approx 10mS delay at 4Mhz CLK
                MOVWF   TIMER2
DEL_LOOP1       MOVLW   D'100'        ;60
                MOVWF   TIMER1
DEL_LOOP2       BTFSC PORTA,SW1
GOTO MENU
BTFSC PORTA,SW2
GOTO MENU
BTFSC PORTA,SW3
GOTO MENU
BTFSC PORTA,SW4
GOTO MENU
DECFSZ  TIMER1,F
                GOTO    DEL_LOOP2
                DECFSZ  TIMER2,F
                GOTO    DEL_LOOP1
RETLW   0
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11713
  • Country: my
  • reassessing directives...
Re: PIC ASM source code help please
« Reply #1 on: January 25, 2011, 07:47:39 pm »
H64, use this! (attachment) my homebrew many years old app. i'm not selling anything, its free, so trust me ;)
« Last Edit: January 25, 2011, 07:49:30 pm by shafri »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline monpjc

  • Contributor
  • Posts: 32
    • monpjc
Re: PIC ASM source code help please
« Reply #2 on: January 25, 2011, 07:57:01 pm »
Hi,

This has two loops one over the top of the other. The value of 100 is loaded in the Wreg and stored in TIMER2 and TIMER1 which are memory locations. These are some values that have been choice based on time loop takes to run (number of opcodes and speed of chip).

The code then performs 'some' function.

The DECFSZ opcode decrements the values stored at TIMER1 (or TIMER2) by one. The 'F' tells the processes where to store the result, in this case back in the memory location TIMER1 (or TIMER2). Had the 'F' been replaced with a 'W' then the decrements value would stay in the working register 'Wreg' and not saved back.

So in summary the inner loops till TIMER1 is zero and then dec's TIMER2, loops back reloading TIMER1 and looping round and round again only exiting when TIMER1 and TIMER2 are zero - 10mSeconds later. So loop is processed TIMER1*TIMER2 or 100*100 = 10,000 times.

Hope that helps
Paul
 

Offline ziq8tsi

  • Regular Contributor
  • *
  • Posts: 80
Re: PIC ASM source code help please
« Reply #3 on: January 25, 2011, 08:02:26 pm »
Hi there. I have some assembly source which came with my Velleman PIC programmer. I understand everything except for this; why are they inserting DECIMAL 100 as a delay value, and by what value is it decrementing, when decrementing by F?

DECFSZ always decrements by 1.  The ",F" (or ",1") means to store the result back into the file, as opposed to ",W" (or ",0") which stores the result in the accumulator.  In this example, not storing the results back into the timer variables would make the loops infinite.

The inner loop, DEL_LOOP2, takes 11 instruction cycles assuming that none of the "GOTO MENU" are taken, and that the "GOTO DEL_LOOP2" is taken.  So they have chosen to loop 100*100 times to give a total delay of (just over) 110000 instruction cycles = 440000 oscillator cycles = 110ms.

I suspect the comment about 10ms is a misleading leftover from an earlier version of the routine without the BTFSC/GOTOs in the body, and with different start values.
 

Offline monpjc

  • Contributor
  • Posts: 32
    • monpjc
Re: PIC ASM source code help please
« Reply #4 on: January 25, 2011, 08:17:49 pm »

The inner loop, DEL_LOOP2, takes 11 instruction cycles assuming that none of the "GOTO MENU" are taken, and that the "GOTO DEL_LOOP2" is taken.  So they have chosen to loop 100*100 times to give a total delay of (just over) 110000 instruction cycles = 440000 oscillator cycles = 110ms.

I suspect the comment about 10ms is a misleading leftover from an earlier version of the routine without the BTFSC/GOTOs in the body, and with different start values.


Good point - I missed that!
 

Offline glossywhiteTopic starter

  • Regular Contributor
  • *
  • !
  • Posts: 241
Re: PIC ASM source code help please
« Reply #5 on: January 25, 2011, 08:21:19 pm »
Ah! thanks chaps, I had thought that the DECFSZ was decrementing the timers BY the value "F" - I hadn't realised that "F" pertains to the F register! How do you cause DECFSZ (DECrement F register, Skip if Zero) to decrement by more than 1, per execution?


PS: I get the rest of the routine completely, but thanks for explaining. Doing pretty well for someone who's only been learning ASM for just over 2 weeks.
:D
« Last Edit: January 25, 2011, 08:24:03 pm by glossywhite »
 

Offline monpjc

  • Contributor
  • Posts: 32
    • monpjc
Re: PIC ASM source code help please
« Reply #6 on: January 25, 2011, 08:32:13 pm »
Unfortunately you cant, you could dec before then do a decfsz.

However if the first dec took the value to zero, the decfsz would make it rollover and hence non-zero so would loop round again.

Not a good route to be honest if your checking the value. If you want to loop faster, lower the value down from 100.

What chip are you using? I commend the use of ASM but the free Hi-Tech C compilers is really very good and what we use at work on PIC12 and PIC16's and very little overhead.

(wait for ASM programmers to complain about C overhead) - honest its not that bad with the Hi-Tech.!
 

Offline glossywhiteTopic starter

  • Regular Contributor
  • *
  • !
  • Posts: 241
Re: PIC ASM source code help please
« Reply #7 on: January 25, 2011, 08:48:44 pm »
Unfortunately you cant, you could dec before then do a decfsz.

However if the first dec took the value to zero, the decfsz would make it rollover and hence non-zero so would loop round again.

Not a good route to be honest if your checking the value. If you want to loop faster, lower the value down from 100.

What chip are you using? I commend the use of ASM but the free Hi-Tech C compilers is really very good and what we use at work on PIC12 and PIC16's and very little overhead.

(wait for ASM programmers to complain about C overhead) - honest its not that bad with the Hi-Tech.!

I use the 16f627 and 16f628, and MPLAB (latest) with the Hi-Tech compiler. I know C a little, but to be honest I am finding ASM much more intuitive and prefer the "nitty gritty" of being able to visualise actual registers in my mind. ASM is teaching me things I never thought about before. Also, am I right in thinking that, for PIC, there is only one ASM format, whereas with C, there seem to be quite a few variants, each differing?

I am not a fanboy of either, I just prefer to do things at "component level", if you see my analogy, as opposed to dealing with abstracted blocks of code.
 

Offline monpjc

  • Contributor
  • Posts: 32
    • monpjc
Re: PIC ASM source code help please
« Reply #8 on: January 25, 2011, 09:00:50 pm »
PIC has a core language using the ASM opcodes, you will finder the PIC18 and up will have the same opcodes and some extra ones too. Were as the PIC10 / 12's has less opcodes.

C is quite flat as a language - you will find embedded C for each chip differs, but not the core language, names of registers and some addressing modes etc. C on the PIC24 is really easy.!

Watch for getting caught with the BANKSEL and PAGSEL functions. PIC's like using paged memory and registers and you have to flip flags in the STATUS reg to hop between them - Big drawback on the PIC16-18 range. This is where HiTech helps as it works all these out for you.

I get you point about grass root programming - always good to understand what under the bonnet!

Once you have understood ASM, I'd give C ago - and this is coming from someone who used to code in HEX on a z80 cpu back in the 80's!

enjoy
Paul
 

Offline glossywhiteTopic starter

  • Regular Contributor
  • *
  • !
  • Posts: 241
Re: PIC ASM source code help please
« Reply #9 on: January 25, 2011, 09:18:06 pm »
PIC has a core language using the ASM opcodes, you will finder the PIC18 and up will have the same opcodes and some extra ones too. Were as the PIC10 / 12's has less opcodes.

C is quite flat as a language - you will find embedded C for each chip differs, but not the core language, names of registers and some addressing modes etc. C on the PIC24 is really easy.!

Watch for getting caught with the BANKSEL and PAGSEL functions. PIC's like using paged memory and registers and you have to flip flags in the STATUS reg to hop between them - Big drawback on the PIC16-18 range. This is where HiTech helps as it works all these out for you.

I get you point about grass root programming - always good to understand what under the bonnet!

Once you have understood ASM, I'd give C ago - and this is coming from someone who used to code in HEX on a z80 cpu back in the 80's!

enjoy
Paul

Nope, bank switching was the very first thing which I "got", so no issues there. Find it SO simple :)

[EDIT] Finally got my head around how this iterative delay works; can't beat pen and paper, and it sure gave my mind a good workout!:

« Last Edit: January 25, 2011, 11:41:23 pm by glossywhite »
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11713
  • Country: my
  • reassessing directives...
Re: PIC ASM source code help please
« Reply #10 on: January 27, 2011, 05:27:42 pm »
asm is good for manipulating registers and mcu specific features directly, but "not so good" at showing the obvious logic of the algorithm. your Original Posting is a good example how obscure the following code is:
Code: [Select]
'higher level basic language

for timer2 = 1 to 100
  for timer1 = 1 to 100
    ' do something
  next timer1
next timer2
you will be likely to master at manipulating hardware with asm, but less likely to build a moderate to complicated algorithm, let alone the simpler floating point operations.
« Last Edit: January 27, 2011, 05:33:41 pm by shafri »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline CafeLogic

  • Regular Contributor
  • *
  • Posts: 117
    • Cafe Logic
Re: PIC ASM source code help please
« Reply #11 on: January 27, 2011, 06:15:26 pm »
I will add that ASM ties you to a platform whereas embedded C, while they all have quirks like you said, is largely all the same. If you get pissed off at Microchip, you can go grab an STM8 and be up and running in no time. I don't want to discourage you from the educational standpoint (I commend you for that), just as a long term solution. Another thing to think about is what happens when you want to make Ethernet and USB enabled applications? What happens when you need an RTOS? Not happening in assembly.
 

Offline glossywhiteTopic starter

  • Regular Contributor
  • *
  • !
  • Posts: 241
Re: PIC ASM source code help please
« Reply #12 on: January 27, 2011, 11:42:12 pm »
asm is good for manipulating registers and mcu specific features directly, but "not so good" at showing the obvious logic of the algorithm. your Original Posting is a good example how obscure the following code is:
Code: [Select]
'higher level basic language

for timer2 = 1 to 100
  for timer1 = 1 to 100
    ' do something
  next timer1
next timer2
you will be likely to master at manipulating hardware with asm, but less likely to build a moderate to complicated algorithm, let alone the simpler floating point operations.


I'll have to completely disagree with you on the point of ASM not showing the obvious logic of an algorithm - it is working at only one step up from machine code - could you get any clearer a picture, if your mind is able to work this way (mine is)? If abstracting away from something by "dumbing it down" is clarification for the sake of teaching a beginner, then I see your point, but to say that it is inobvious... kinda doesn't makes sense - to me, anyway.

Someone once told me that C is a direct, terse language. Yeah? Well, ASM seems much more terse, possibly to the point of ambiguity, until you learn that all the instructions are merely acronyms, and VERY logically names ones at that. I sure don't miss all the "{...}" brace balancing, opening and closing routines and remembering where to put my "()" and which is the best way to write a pointer; "something *pointerToSomething" or whatever. Ugh. ASM seems to be as simple and grass roots as it gets, because your mind is INSIDE the registers ALL the time, and you're not being spoonfed.

Ugh, do I sound like a fanboy? I hope not - I am not, I just know I love all I have learned about ASM, and it is (apparently) the hardest language to learn... really? Well that's just not true to me - all those safety nets and cuddly OOP abstractions just baby me, and hide what is really going on in my chip. If I need cotton wool, I'll go buy some from the chemist :P hehe
 

Offline CafeLogic

  • Regular Contributor
  • *
  • Posts: 117
    • Cafe Logic
Re: PIC ASM source code help please
« Reply #13 on: January 28, 2011, 12:50:54 am »
We humans are not very smart and we can't keep track of more than a few things at once. We rely on abstractions for everything. ASM is far from the bottom, it's just another level in the abstraction tree. You could go grab an FPGA and put your software in schematic mode, then you wouldn't have to deal with those annoying abstractions like the arithmetic unit. You could just bang logic gates together. Of course, logic gates are abstractions for clumps of transistors.

Personally, I prefer to go as high up the abstraction tree as is possible for the given task. If I am writing a program for the desktop, I don't want anything to do with C either. I also prefer ohms law over Maxwell's equations. I am sure there will be base concepts that will elude me as I go about my abstracted life but I guess that's the price I pay for productivity, to each is his own.
 

Offline glossywhiteTopic starter

  • Regular Contributor
  • *
  • !
  • Posts: 241
Re: PIC ASM source code help please
« Reply #14 on: January 28, 2011, 12:59:40 am »
<snippety snip>

...to each is his own

Agreed!
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11713
  • Country: my
  • reassessing directives...
Re: PIC ASM source code help please
« Reply #15 on: January 28, 2011, 01:59:39 am »
I'll have to completely disagree with you...
Ugh, do I sound like a fanboy? I hope not - I am not, I just know I love all I have learned about ASM
sure you can do asm, if you think it suits you. i dont want to be a fan boy either, so we can do whatever we like and love to do. i was just trying to convey some message from my mind. ;)
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline monpjc

  • Contributor
  • Posts: 32
    • monpjc
Re: PIC ASM source code help please
« Reply #16 on: January 28, 2011, 12:18:15 pm »
It’s always interesting to see the old ASM v C Code arguments and glossywhite is right in saying each to his own.

I think you can expand on this too and say "hours for courses". I once worked on an embedded product which was using heavy C++, lots of object code and all to display the weight of something on the pan - not a good choice.

People will tend to use what they are most happy with and will perform best that way. I certainly would not use ASM to try and write a OS or RTOS but then some simple tiny uP thas just waggling an IO pin is better written in ASM then having C code.?

There are lots of pro and con arguments that C is quicker to write, or it uses more code space than ASM. But ASM can be written to run faster and has less overhead blah, blah, blah.

There are some clear cases where one language will fit better than the other, and you need a skilled software engineer who knows that language to use it. The language is only a tool to generate machine code.

Many of the problems some people get into is due to their coding styles - Glossywhite understand the register level and odd bank switching of a PIC that many fall over and get stung by. Others who have no interest in this level or understanding and will want to work at a higher level language are well suited for C etc.

Stressing my point - as someone who is an electronics engineer, not a software engineer I started out coding in HEX on a z80 and now happily program in ASM, C and VHDL for FPGAs. I know that how well a language works is down to me and my understanding to use that tool, not because one language is or is not better than the other.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf