News:

;) This forum is the property of Proton software developers

Main Menu

7 bytes incrementally

Started by Mapo, Feb 02, 2023, 10:13 AM

Previous topic - Next topic

Mapo

Hello everyone,
I have to manage 7 bytes incrementally, starting from a value of 0 up to FF FF FF FF FF FF FF
Do you recommend using 2 DWords or 7 single bytes?
I use an 18F26K22

John Drew

An interesting one Mapo.
If it is in an 18F I think it would be simpler to use two DWords. Less tests on overflow.
On the other hand if you use a 16 bit PIC you could use 64 bit integers and just keep incrementing.
John

shantanu@india

Can't it be a simple 7-byte array manipulation? You increment the lowest member & when it is equal to 255 you increment the next higher member & set a byte-pointer accordingly.
Initially the array is zero & the byte pointer is also zero. After 255 increments the byte pointer changes from zero to one & the second byte increments from zero to 255. The byte pointer also correspondingly becomes 2 & starts pointing to the third byte in the array & so on.
Regards
Shantanu

tumbleweed

Quote...when it is equal to 255 you increment the next higher member & set a byte-pointer accordingly.
Not following the whole "byte pointer" thing.

You always increment starting at the lowest byte and any overflow (byte value is now = 0) means you have to increment the next byte too. Repeat for all 8 bytes.

tumbleweed

#4
In terms of speed/size, the simplest way would probably be to use dwords...

Dim vdw[2] As Dword

Inc vdw[0]
If vdw[0] = 0 Then
  Inc vdw[1]
EndIf
                       

keytapper

This may be crazy, like me, but seems to be feasible
Device 16F1827
Xtal 32
Dim longVar[8] As Byte
Dim LowDword As Dword At longVar#3
Dim HiDword As Dword At longVar#7

Inc LowDword
If STATUS.0 <> 0 Then
 Inc HiDword
End If
It will consume an extra byte, because of the second Dword  ;)
There are permitted operation on the first LowDword, but must be followed by updating of the syster HiDword, immediately.
Ignorance comes with a cost

Mapo

thank you all for the advice, the solution with 2 dwords is simpler and more manageable

tumbleweed

#7
Quote from: keytapper on Feb 02, 2023, 07:38 PMThis may be crazy, like me, but seems to be feasible

Actually, once you correct the location offsets of the two Dwords that's a pretty good idea!
Dim longVar[8] As Byte
Dim LowDword As Dword At longVar#0     ' was Dword At longVar#3
Dim HiDword As Dword At longVar#4      ' was Dword At longVar#7

Inc LowDword
If STATUS.0 <> 0 Then
 Inc HiDword
End If

All this is assuming you can live with an eight-byte array. If not, then you have to do it long-handed with the seven bytes.

keytapper

#8
Quote from: tumbleweed on Feb 03, 2023, 12:04 AMDim LowDword As Dword At longVar#0     ' was Dword At longVar#3
I haven't evaluated the Dword size starting from right to left order. I just looked at the assembler code that gave me what I supposed was my concept.
In facts the operation in assembler would use a chain of contiguous memory, as long as the carry is propagated to the highest byte of the addition. But to use in basic formulation, I whipped it out like that.

Quote from: tumbleweed on Feb 03, 2023, 12:04 AMAll this is assuming you can live with an eight-byte array

That's the best tradeoff.
Ignorance comes with a cost