PIC ASM source code help please - Page 1
Forum section: electronics / general

author: glossywhite
author_rank: Regular Contributor
timestamp: January 25, 2011, 06:23:56 pm
content: 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:;*********************************************;* Example of a delay routine *;*********************************************DELAY_ROUTINE MOVLW D'100' ;54 Generate approx 10mS delay at 4Mhz CLKMOVWF TIMER2DEL_LOOP1 MOVLW D'100';60MOVWF TIMER1DEL_LOOP2 BTFSCPORTA,SW1GOTOMENUBTFSCPORTA,SW2GOTOMENUBTFSCPORTA,SW3GOTOMENUBTFSCPORTA,SW4GOTOMENUDECFSZ TIMER1,FGOTO DEL_LOOP2DECFSZ TIMER2,FGOTO DEL_LOOP1RETLW 0

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 25, 2011, 07:47:39 pm
content: H64, use this! (attachment) my homebrew many years old app. i'm not selling anything, its free, so trust me

author: monpjc
author_rank: Contributor
timestamp: January 25, 2011, 07:57:01 pm
content: 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 helpsPaul

author: ziq8tsi
author_rank: Regular Contributor
timestamp: January 25, 2011, 08:02:26 pm
content: Quote from: glossywhite on January 25, 2011, 06:23:56 pmDECFSZ 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,notstoring 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"istaken. 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,andwith different start values.

author: monpjc
author_rank: Contributor
timestamp: January 25, 2011, 08:17:49 pm
content: Quote from: ziq8tsi on January 25, 2011, 08:02:26 pmGood point - I missed that!

author: glossywhite
author_rank: Regular Contributor
timestamp: January 25, 2011, 08:21:19 pm
content: 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 (DECrementFregister,Skip ifZero) 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.

author: monpjc
author_rank: Contributor
timestamp: January 25, 2011, 08:32:13 pm
content: 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.!

author: glossywhite
author_rank: Regular Contributor
timestamp: January 25, 2011, 08:48:44 pm
content: Quote from: monpjc on January 25, 2011, 08:32:13 pmI 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 ASMmuchmore 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 onlyoneASM 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.

author: monpjc
author_rank: Contributor
timestamp: January 25, 2011, 09:00:50 pm
content: 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!enjoyPaul

author: glossywhite
author_rank: Regular Contributor
timestamp: January 25, 2011, 09:18:06 pm
content: Quote from: monpjc on January 25, 2011, 09:00:50 pmNope, bank switching wasthevery first thing which I "got", so no issues there. Find it SO simple 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!:

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 27, 2011, 05:27:42 pm
content: 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:'higher level basic languagefor timer2 = 1 to 100for timer1 = 1 to 100' do somethingnext timer1next timer2you 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.

author: CafeLogic
author_rank: Regular Contributor
timestamp: January 27, 2011, 06:15:26 pm
content: 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.

author: glossywhite
author_rank: Regular Contributor
timestamp: January 27, 2011, 11:42:12 pm
content: Quote from: shafri on January 27, 2011, 05:27:42 pmI'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 seemsmuchmore 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 isreallygoing on in my chip. If I need cotton wool, I'll go buy some from the chemisthehe

author: CafeLogic
author_rank: Regular Contributor
timestamp: January 28, 2011, 12:50:54 am
content: 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.

author: glossywhite
author_rank: Regular Contributor
timestamp: January 28, 2011, 12:59:40 am
content: <snippety snip>...to each is his ownAgreed!

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 28, 2011, 01:59:39 am
content: Quote from: glossywhite on January 27, 2011, 11:42:12 pmsure 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.

author: monpjc
author_rank: Contributor
timestamp: January 28, 2011, 12:18:15 pm
content: 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.