View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19217
|
|
Posted: Mon Jun 01, 2020 12:47 am |
|
|
Or, look at how I've done it with the union.
I pass an 'int32' to the functions as an address, but tell the compiler
this is a union between an int32, and 4*int8's. A union, uses the same
'space' in memory for the declared parts, so the int8's are the four
bytes of the int32. You can read or write to these, and the int32 then
contains the whole 'assembly'.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9101 Location: Greensville,Ontario
|
|
Posted: Mon Jun 01, 2020 4:33 am |
|
|
I see you only have 3 bytes and need to use an int32....
When using either make32() or union , have a 4th 'dummy' byte as a 'filler' to keep the compiler happy. Wile I KNOW the compiler is much smarter than me, this litle bit of housekeeping might prevent some 'bug' for some version of compiler from doing odd things.....
Also when using make32(), be sure the order of the 4 bytes is correct ! I wondered for a few hours why my program was giving 'interesting' numbers only to FINALLY see the order of the bytes was reversed...... |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Mon Jun 01, 2020 9:18 am |
|
|
Definitely structs/unions are the way to go.
Although I never really leave out members of a struct.
For the sake of the compiler guessing or the next reader of my code, I tend to fully justify structs with an even int/byte worth of "stuff" (like 8bits for 8bit CPUs, 16bits for 16bit CPUs and so on) _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19217
|
|
Posted: Mon Jun 01, 2020 9:56 am |
|
|
If you look at what I posted, I input an int32, and then just output the
three low bytes. Since the source value is an int32, all four bytes
'exist', just omitting one on the output is not a problem.
However using it the other way, I'd either 'preload' with a zero value,
or include the fourth byte. |
|
|
|