Microchip RISC question

Art

Joined Sep 10, 2007
806
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:
bytea = 0xff
byteb = 0xff
bytec = 0xff
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:
movlw 0xff
movwf bytea
movwf byteb
movwf bytec
I’d have sworn this has worked before, am I missing something obvious?
Cheers, Art.

JohnInTX

Joined Jun 26, 2012
4,664
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:

Art

Joined Sep 10, 2007
806
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.

takao21203

Joined Apr 28, 2012
3,702
Hi Guys,
I’m trying to reduce code to make space, and have been stuck.
WHY??

couldnt you put some data in EEPROM
use better IC

Papabravo

Joined Feb 24, 2006
18,400
WHY??

couldnt you put some data in EEPROM
use better IC
View attachment 105676
Data and code go in different memories. You might not be familiar with the PIC architecture (Harvard).

Papabravo

Joined Feb 24, 2006
18,400
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:
bytea = 0xff
byteb = 0xff
bytec = 0xff
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:
movlw 0xff
movwf bytea
movwf byteb
movwf bytec
I’d have sworn this has worked before, am I missing something obvious?
Cheers, Art.
Are you aware of how threaded code works? This technique will often reduce code space requirements dramatically.

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:

John P

Joined Oct 14, 2008
1,972
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.

Art

Joined Sep 10, 2007
806
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.

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.

Art

Joined Sep 10, 2007
806
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!

Are you aware of how threaded code works? This technique will often reduce code space requirements dramatically.

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.

nsaspook

Joined Aug 27, 2009
9,708
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.
The fact that some old fab technology chips might survive that does not mean the same chip designs with newer process technology will.

takao21203

Joined Apr 28, 2012
3,702
Data and code go in different memories. You might not be familiar with the PIC architecture (Harvard).
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.
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.

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.
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.

JohnInTX

Joined Jun 26, 2012
4,664
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.
XC8 does... Checked it in baseline and enhanced midrange in Free and PRO modes.
Code:
12:  unsigned char a,b,c;
13:  void main()
14:  {
15:  a = b = c = 0xff;
07C2  30FF  MOVLW 0xFF
07C3  00F2  MOVWF c
07C4  00F1  MOVWF b
07C5  00F0  MOVWF a

JohnInTX

Joined Jun 26, 2012
4,664
The fact that some old fab technology chips might survive that does not mean the same chip designs with newer process technology will.
+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.

Art

Joined Sep 10, 2007
806
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:

takao21203

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

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.

dannyf

Joined Sep 13, 2015
2,197
The whole thing was a bunch of nonsensical subroutines that's now hard to make sense of!
That tells you all you need to know about writing more assembly.

Art

Joined Sep 10, 2007
806
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.

NorthGuy

Joined Jun 28, 2014
611
That tells you all you need to know about writing more assembly.
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.

Papabravo

Joined Feb 24, 2006
18,400
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.

...
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.