BBC BASIC for Windows
Programming >> Assembly Language Programming >> Converting signed 64-bit int to signed 32-bit int
http://bb4w.conforums.com/index.cgi?board=assembler&action=display&num=1244798970

Converting signed 64-bit int to signed 32-bit int
Post by David Williams on Jun 12th, 2009, 09:29am

Okay, this is embarrassing.

I'm trying to implement the MOD operator as an ASM routine using the definition given in the BB4W docs:

A MOD B = A - ( A DIV B ) * B

For my purposes, A is a signed 32-bit integer (so it may be negative), and B is always a positive integer never greater than 16 bits.

Things are fine until one supplies a negative A, because I can't work out how to convert a signed 64-bit integer to a signed 32-bit integer (the absolute value of the signed 64-bit integer is assumed never to exceed 32 bits, or 31 bits, strictly speaking).

So, doing the A DIV B part, I've got:

Code:
; EAX = A,  EBX = B
xor edx, edx
idiv ebx 


How do I convert the possibly negative 64-bit value in EDX:EAX to a signed 32-bit value (again bearing in mind the absolute value in EDX:EAX doesn't exceed 32 bits)?

I did try using floating point instructions, but without success.


David.



Re: Converting signed 64-bit int to signed 32-bit
Post by admin on Jun 12th, 2009, 10:20am

Quote:
How do I convert the possibly negative 64-bit value in EDX:EAX to a signed 32-bit value (again bearing in mind the absolute value in EDX:EAX doesn't exceed 32 bits)?

I don't understand the question. If the 64-bit integer value in EDX:EAX is within the range of a 32-bit (signed) number (i.e. -2147483648 to +2147483647) then no conversion is necessary: the answer is simply the value of EAX.

Am I missing some subtlety here?

Richard.