PDA

View Full Version : FillMem or ClearMem



BigWumpus
- 25th November 2010, 23:21
I'm just coding a PIC18F-projekt and there is the need to clear some Arrays or so...

What's about a ClearMem-command (or macro), because it is smarter then a "FOR I=0 to 15:Array[I]=0:Next I"-monster ?

Are there any macros to use ?

And what about a sizeof-function ????

HenrikOlsson
- 26th November 2010, 08:42
Hi,
Not much help I'm afraid but anyway,
CLEAR will set all RAM registers to zero. I don't know of a way to selectively clear RAM other than manually doing it, for example with a FOR-NEXT loop like you show.

I'm not sure a SizeOf function would make much sense since arrays can't dynamically resize themself in PBP, you declare them as n bytes (or bits or words or longs) and they stay n bytes long. If you're storing a string for example you can create a subroutine to itterate thru the array and look for your end-of-string character. Could you elaborate a bit on what you need it for, perhaps someone can make a sugestion on how to do it.

/Henrik.

Darrel Taylor
- 26th November 2010, 16:38
For PBP 2.60 ...


MyArraySize CON 32 ; SizeOf
MyArray VAR BYTE[MyArraySize] ; The Array

ARRAYWRITE MyArray,[REP 0\MyArraySize] ; Clear Array

BigWumpus
- 30th November 2010, 20:53
I've done some own work, but it is not tested:



asm
ClearMem macro Array,Size ;Macro um Speicher zu löschen
lfsr 0,Array
movlw Size
clrf POSTINC0
decfsz WREG
bra $-2
endm
endasm


Any hints ?

BigWumpus
- 30th November 2010, 20:56
For PBP 2.60 ...


ARRAYWRITE MyArray,[REP 0\MyArraySize] ; Clear Array


Does this build a loop, or is there a "heavy load" of data in the code ?
I work with PBP 2.50C.

mackrackit
- 30th November 2010, 22:27
You need PBP 2.60 to use ARRAYWRITE.

Darrel Taylor
- 1st December 2010, 17:56
Does this build a loop, or is there a "heavy load" of data in the code ?
Yes, it will use a loop (with 2.60).

And in your macro above ...
Since it uses the LFSR instruction, it will only work on 18F's.
And 18F's are addressed by bytes instead of words, so I think the bra $-2 would need to be bra $-4.
Otherwise, it should work.

BigWumpus
- 1st December 2010, 22:29
And in your macro above ...
Since it uses the LFSR instruction, it will only work on 18F's.
Yes - that is know, the tiny 16Fs doesn't have so big tasks to do ... ;-)

And 18F's are addressed by bytes instead of words, so I think the bra $-2 would need to be bra $-4.
Otherwise, it should work.
No - the datasheet uses a bra $-2 too and Picbasic compiles:


000C12 0003 M loop sleep
000C14 D7FE M bra loop

the same way as


03868 ClearMem _bTier,_lTier ;Tier-Buffer löschen
000F46 EE01 F042 M lfsr 0,_bTier
000F4A 0E20 M movlw _lTier
000F4C 6AEE M clrf POSTINC0
000F4E 2EE8 M decfsz WREG
000F50 D7FE M bra $-2

So, I think it will work for me...

Darrel Taylor
- 1st December 2010, 23:34
No - the datasheet uses a bra $-2 too and Picbasic compiles:


000C12 0003 M loop sleep
000C14 D7FE M bra loop

the same way as


03868 ClearMem _bTier,_lTier ;Tier-Buffer löschen
000F46 EE01 F042 M lfsr 0,_bTier
000F4A 0E20 M movlw _lTier
000F4C 6AEE M clrf POSTINC0
000F4E 2EE8 M decfsz WREG
000F50 D7FE M bra $-2

So, I think it will work for me...
In the "sleep" example, the branch is going to the instruction immediately before the bra.

In your macro, the bra needs to jump 2 instructions up to the clrf POSTINC0.
So it will need to be -4.

BigWumpus
- 2nd December 2010, 00:28
S*it

to late - to much wine