Page 1 of 1
how to get big endian / motorola value of word ?
Posted: Mon Aug 25, 2003 8:59 pm
by pythagoras_156
Greetings everyone,
My problem is as follows : i'm trying to convert a word variable to its big endian / Motorola value. I've been using this function (found on this forum

) :
Code: Select all
Procedure PeekWMotorola(address.l)
; address of value in Motorola format -> value in Intel format
b1.l = PeekB( address+0 ) & 255
b2.l = PeekB( address+1 ) & 255
ProcedureReturn (b1<<8) + b2
EndProcedure
This works but it returns an
unsigned value for my word variable.
Does anyone has an idea how to make it return a
signed word value ?
thanks in advance,
pythagoras
Re: how to get big endian / motorola value of word ?
Posted: Tue Aug 26, 2003 12:25 am
by Doobrey
You haven`t set a return type for the procedure.. I think PB is defaulting to a long.
Try Procedure.w PeekWMotorola(address.l) instead.
<shameless plug>
Or you could use the SwapWord function from my DnMisc library,from my site at
http://www.doobreynet.co.uk
</shameless plug>
Handling Signed Bigend values
Posted: Tue Aug 26, 2003 11:07 pm
by oldefoxx
PeekB() will only get one byte, so the & 255 is unnecessary. In fact since you are putting it through that ANDing process, you are effectively changing it from a signed value that ranges from -128 to 127, to an unsigned value that ranges from 0 to 255. I believe that without the & 255, it will work correctly. However, you should still use the & 255 for the little end, since you do not want it to be interpreted as having a signed value. Remember, you may be writing 255, but the compiler will probably use a long to represent it when it codes it. So the upper byte in that long will be 00000000 in binary, meaning the upper signed bits will be eliminated.
You could also use ASM code to load the byte, and sign extend it to an integer (or word for unsigned), then extend it again from an integer (or word) to a long (or dword for unsigned).
you could also set up another structure with a field of an integer, then use StructureUnion to overlay the two structures together. You set the integer into the second structure's field, then you can pill the bigend and littleend from the two byte fields in the first strucute. That allows you a passive form of converting between data representations.
I'm not sure why you want to handle bigend and littleend bytes using longs, as you can process them as bytes or integers instead. I've heard arguments fro other quarters that the newer architecture is faster with 43-bit words and integers than it is with 8-bits or 16-bits, but that is really only important if you get into timing instruction sequences and trying to iptimize code chains that are used hundreds or thousands of times as part of massive computations.