Microchip RISC question

Discussion in 'Embedded Systems and Microcontrollers' started by Art, May 7, 2016.

  1. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    Hi Guys,
    I’m trying to reduce code to make space, and have been stuck.
    I have a few variables that are all regularly set to the same value, where in a higher level language it might looks like this:
    Code (Text):
    1.  
    2. bytea = 0xff
    3. byteb = 0xff
    4. bytec = 0xff
    5.  
    I understand if it were a sizeable array one might step through it.
    When it comes to RISC asm for pic, I thought w wasn’t erased, but this doesn’t appear to be working:
    Code (Text):
    1.  
    2. movlw 0xff
    3. movwf bytea
    4. movwf byteb
    5. movwf bytec
    6.  
    I’d have sworn this has worked before, am I missing something obvious?
    Cheers, Art.
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    It should work fine and load bytea,b,and c with FF. Be sure that you have the correct RAM bank selected. You are correct, W is not erased between the writes. In fact it will remain the same value until explicitly modified.

    If you're running interrupts, be sure you are saving W as part of the context save routine.
    Be sure that you have defined bytea et. al. as addresses of RAM that is on your particular chip. Look in the symbol table at the end of the assembler listing to see what value the name 'bytea' etc. evaluates to. It has to be a valid RAM address of course.

    Which PIC?
     
    Last edited: May 8, 2016
  3. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    Hi John,
    Thanks for the reply. It looks like for 16F628A I simply forgot the chip has four banks and two status bits to select them,
    where I was only resetting bit 5 for bank 0, the compiler must have last been looking in bank 3.
    Status bit 6 had to also be cleared.
     
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    WHY??

    couldnt you put some data in EEPROM
    use better IC
    giphy.gif
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    Data and code go in different memories. You might not be familiar with the PIC architecture (Harvard).
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    Are you aware of how threaded code works? This technique will often reduce code space requirements dramatically.

    https://en.wikipedia.org/wiki/Threaded_code

    I've used this technique on numerous platforms and the results can be dramatic. It can be implemented in assembly language or a higher level language such a s C.
     
    Last edited: May 8, 2016
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    I've always thought that a line like this:
    a = b = c = d = 0xff;

    ought to compile to one movlw and a bunch of movwf's as in the original posting. But none of the compilers I've ever tried does it that way.
     
  8. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    I've watched the decompile of PBP evolve over revisions.
    A=0 used to write a zero value to the accumulator and copy to file rather than clrf.
    Adding or subtracting 1 never took advantage of inc or decf single instructions, etc.

    It's been some time since I've been in this position, but it's coming back.
    I've avoided the EEPROM due to the extra instruction time taken to get it's data to RAM.
    I'm already sacrificing readability for memory.

    Better chip is subjective. 12C/16F series is robust, and can be powered from 12 Volts for some time which I don't know of modern micros.
     
  9. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    I worked on & off on a 2k asn program for 12 months once,
    all to gain memory and speed was no issue.
    The whole thing was a bunch of nonsensical subroutines that's now hard to make sense of!

     
  10. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,913
    2,187
    The fact that some old fab technology chips might survive that does not mean the same chip designs with newer process technology will.
     
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    If there would be good books how to program microcode, students can be quite effective solving a problem like this. But it not easy:

    Basically what you normally assume is a computer, terminal, IO and HID, storage media, is not really seen at this level. If it is visible, theres a hardware line.
    Normally you would thread everything that repeats.

    So even if it is just 5x some MOV,

    you pass a parameter and use CALL.

    I found the RISC assebler quite powerful. You could build your own adressing "instructions".
    But then there are problems:

    1 You get code rot. Code you dont understand anymore even if it only does some thing simple
    2 Its not portable especially because the bankking.
    3 Even if your discussion parter knows PIC they may not know the pecularities of the IC your using
    4 The Arduino student will not even think C is mostly portable. And think its totally incompatible.
    5 When you "think" assembler too much youre pretty close to autism. Uninitiated people will misunderstand your precize verbal constructs.
     
  12. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    XC8 does... Checked it in baseline and enhanced midrange in Free and PRO modes.
    Code (Text):
    1.  
    2. 12:  unsigned char a,b,c;
    3. 13:  void main()
    4. 14:  {
    5. 15:  a = b = c = 0xff;
    6. 07C2  30FF  MOVLW 0xFF
    7. 07C3  00F2  MOVWF c
    8. 07C4  00F1  MOVWF b
    9. 07C5  00F0  MOVWF a
    10.  
     
  13. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    +1. There are few PICs with onboard shunt regulators that can handle more than 5V but that is only for those specific devices. Exceeding (or even running at) the Absolute Maximum Ratings given for the specific PIC is poor practice to put it politely.

    As far as assembler being hard to understand after awhile, that depends on who wrote it. I routinely return to PIC assembler code a decade or more old without any issues. Others have taken over projects that I've written for them and have little or no problems with that, either. Its how you design, write and document it that counts, not whether it's written in assembler - or any other language for that matter.
     
    Sensacell likes this.
  14. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    It was understandable just as this one, until I decide making space is more important than readability later, at least for me.
    By this time any tailing end of any subroutine prior to a return instruction is fair game to reuse for anything that will
    consume more than the words for the call command to get it there. Speed is a bit of an issue for this one though.

    Running the chip from 12 Volts is not what I’m doing, but have tested before on several midrange pics.
    Most recently on a new stock 16F628A, but also 16F877A, and 12C508/9/A (in the past).
    This is a Tesla coil driver, and is the same as another thread here with the little LCD scope.
    All other things equal, I think it prudent to use this than a 3.x Volt dspic for example.
    I have managed already to save enough memory to keep the scope display which is good.
    The first bad thing that comes to mind that could happen is the ground becomes modulated,
    or an AC signal superimposed on it, but that could be solved with opto isolation for the DC SSR inputs.

    Saving space and gaining speed from a PBP compiled program has always been easy to begin with.
    It has go between variables to send values from your defined variables to it’s canned routines such as
    LCDOUT, or software serial, sound, frequency, etc.
    They just don’t need to exist, and furthermore, you can later send values to them in the accumulator.

    Speaking of EEPROM, it would be nice if not for the extra time reading to RAM,
    but can you use on chip EEPROM when a timer interrupt is running?
    Maybe you could sneak the reads in just after the interrupt has triggered.
     
    Last edited: May 9, 2016
  15. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    My advice is to mothball it.
    You can tune up c sources and it's just a mechanized way to spell Assembler.

    There's a tradeoff for portability.
    But you still can research the disassy.

    You can't demand from others to make themselves familiar with some chip. They'll just walk away.

    Do you see Assembler apps on phones? People talking about GEOS WORKS?

    So even if you are quite good at it, the number of people who will be able to understand you is small.

    Did you see this Japanese video game programmer who died at 46 from burnout? He looked like a zombie.

    Did you see these guys who rewrote amiga OS 3.1 in Assembler? It was praised to be, well, compact and fast. Nobody talks about it anymore.
     
  16. dannyf

    Well-Known Member

    Sep 13, 2015
    1,836
    367
    That tells you all you need to know about writing more assembly.
     
  17. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,693
    2,765
    Still waiting...
     
  18. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    If it's about C vs assembler or another language, it's apples & oranges & depends what you want to do.
    I've gained the most satisfaction from assembler, but the program has to be worth some committment which my current one isn't.
    The assembler for this one is to optimise BASIC.
    I write C for dspic, iOS & now the Amiga too, but I still do prefer a Microcontroller project has a requirement for a modern chip before using one, and the mid range pics and their competitors, I do think were intended to be written natively.
     
  19. NorthGuy

    Active Member

    Jun 28, 2014
    605
    121
    When I don't know something, or don't understand, I learn. I never listen to people who advise me not to waste time on learning. The more I learn, the more I'm able to make informed decisions. This has been working well for me so far.
     
  20. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    There are plenty of good resources on this subject. I don't think this is really the issue. In a Harvard architecture you can't reduce the amount of code space consumed by moving data from one type of memory to another.
     
Loading...