View previous topic :: View next topic |
Author |
Message |
Meister
Joined: 27 May 2010 Posts: 319
|
Posted: Mon Mar 19, 2018 8:51 am Post subject: Can Memcopy be used to fill a whole array with an Integer or |
|
|
Hello,
I want to fill an Array of Long with a constant number, thought I could use Memcopy, like
$regfile = "m328pdef.dat"
Dim I As Integer , C(10) , Maxi As Long
Maxi = 6
I = Memcopy(maxi , C(1) , 8 , 2)
End
But this code fills 8 sequent memory bytes of C with "06" instead of "06 00 00 00 ...".
From Help, I find this, which works for a variable (L), but how to use it for an array?
Dim W As Word , L As Long
W = 65511
J = Memcopy(w , L , 2)
Can an array be filled with Integer or Word numbers?
Best regards, Meister
(BASCOM-AVR version : 2.0.8.1 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Mar 19, 2018 1:20 pm Post subject: Re: Can Memcopy be used to fill a whole array with an Intege |
|
|
Meister wrote: | But this code fills 8 sequent memory bytes of C with "06" instead of "06 00 00 00 ...". |
If you have read the help, you should have also read that memcopy() works by byte-wise copy, with option 2 the source address is not increased.
This already answers your question.
Quote: | From Help, I find this, which works for a variable (L) | If you would not have omitted this quote from the sample
Code: | 'copy 2 bytes from word to long | you would have noticed there's a big difference between what you're trying to do and what is explained by the sample. |
|
Back to top |
|
|
Meister
Joined: 27 May 2010 Posts: 319
|
Posted: Mon Mar 19, 2018 5:03 pm Post subject: |
|
|
The "Secret " is to omit the "option" parameter (bts = MEMCOPY(source, target , bytes [, option]).
In that case whole Variables rather than Bytes apparently are being copied. I might overlook that in the specifications of Memcopy.
Anyway, this replicates "Maxi" 8/4=2 times in the C-Array beginning at C(1):
$regfile = "m328pdef.dat" 'This fills C(1) and C(2) with 2147483647.
Dim I As Integer , C(10) , Maxi As Long
Maxi = 2147483647
I = Memcopy(maxi , C(1) , 8 )
End |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Mar 19, 2018 7:24 pm Post subject: |
|
|
Hmm, an Easter Egg, extended functionality without naming it.
I suggest to extend the help accordingly. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Mar 20, 2018 9:11 pm Post subject: |
|
|
There is no easter egg. At least there are some but this is not one of them.
When you omit the option parameter, the default 3 is used like is mentioned in the help.
That the code works is only due the fact that the variables are dimensioned after each other.
I would not depend on it unless memory is allocated at an absolute address using AT. _________________ Mark |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Mar 20, 2018 9:28 pm Post subject: |
|
|
LOL, fell myself into this trap. My test code was too simple and confirmed at first sight what Meister wrote.
Thought whoa, how ingenious, the compiler notices it's an four-byte source variable, so it cycles through the source again and again.
But instead var(1) is copied into var(2), var(2) into var(3), a.s.o.
Works basically and is usable, but not as intended.
Funny |
|
Back to top |
|
|
Meister
Joined: 27 May 2010 Posts: 319
|
Posted: Tue Mar 20, 2018 9:43 pm Post subject: |
|
|
Thanks for the clarification and the warnings.
In my tests the variables indeed had been dimensioned one after the other. In real life that it is not yet the case...so I have to rearrange.
Apparently with option 3 ("both the source and target address will be increased after each copied byte") the source address is reset on each transaction.
There might be more things to play with if it makes fun: What happens if the source is for instance a two-element array? Is the code that clever?
Best regards, Meister |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Mar 20, 2018 10:12 pm Post subject: |
|
|
Meister wrote: | Apparently with option 3 ("both the source and target address will be increased after each copied byte") the source address is reset on each transaction. | Huh?
Quote: | What happens if the source is for instance a two-element array? Is the code that clever? | Could it be that you still do not understand it?
Memcopy() does byte-wise copy from source to destination, what kind of cleverness do you expect?
It is however usable for your purpose of filling an array and the save way is to first preset array element 1 with data to copy.
Then call memcopy with element 1 as source and element 2 as destination. As copy of element 2 is finished, the source address points to start of element 2, while the destination address points to element 3. As element 2 just got filled with data, this data can be copied to element 3 a.s.o. |
|
Back to top |
|
|
Meister
Joined: 27 May 2010 Posts: 319
|
Posted: Wed Mar 21, 2018 8:56 am Post subject: |
|
|
Quote: | It is however usable for your purpose of filling an array and the save way is to first preset array element 1 with data to copy.
Then call memcopy with element 1 as source and element 2 as destination. As copy of element 2 is finished, the source address points to start of element 2, while the destination address points to element 3. As element 2 just got filled with data, this data can be copied to element 3 a.s.o. |
Bingo, yes I had found by trial and error using the simulator that it can be done (see my example, but only understanding late tonight what it means that the dims have to be in that order from the way variables are being assigned to memory locations in Bascom, e.g. )
Dim trgt , scr As Long.
It is crystal clear why it works with that simple byte copier but it might be seen as a "trick". |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed Mar 21, 2018 2:43 pm Post subject: |
|
|
Meister wrote: | it means that the dims have to be in that order.
Dim trgt , scr As Long. |
That's the reason why I wrote:
Quote: | the save way is to first preset array element 1 with data to copy ... Then call memcopy with element 1 as source ... |
as then it won't matter:
Code: | Dim myPreset As Word
Dim xyz As Byte
Dim abc As Long
Const myArrSize = 10
Const myArrVarSize = 2 ' word is two byte in size
Dim myArraytoFill(myArrSize) As Word
myPreset = 12345
myArraytoFill(1) = myPreset ' preset first element
'myArraytoFill(1) = 5678 ' alternatively assign a constant directly to the first element
Const btc = (myArrSize - 1) * myArrVarSize ' 1st array var is set, so var count to copy must be array size less one
Dim bcpd As Integer
bcpd = memcopy(myArraytoFill(1) , myArraytoFill(2) , btc) ' fill var array
end |
|
|
Back to top |
|
|
|