How can I do a unsigned division with long ?
Code: Select all
Integer.l = $FFFFFFFE
Integer / 2
Debug Hex(Integer)
But how can I get the good result ($7FFFFFFF) ?
Thank you.
Code: Select all
Integer.l = $FFFFFFFE
Integer / 2
Debug Hex(Integer)
Code: Select all
Macro Div2(Integer)
EnableASM
mov eax, Integer
shr eax, 1
mov Integer, eax
DisableASM
EndMacro
Integer.l = -2
Div2(Integer)
Debug Hex(Integer)
Code: Select all
Procedure Div(A, B)
!mov eax, [p.v_A]
!xor edx, edx
!div dword [p.v_B]
ProcedureReturn
EndProcedure
Code: Select all
; Note that if we divide by 1 and are returning a long, result will be -ve (and unchanged value)
; This should give a result accurate +/- 1
; .. Not yet worked out sure how to fix the inaccuracy
Procedure uLongDiv(value.l, divisor.l)
If value >= 0 ; If positive, do normal divide
result = value / divisor ; This eliminates inaccuracies here
Else ; Otherwise ..
temp = value >> 1 ; Halve the value by shifting right
temp & $7FFFFFFF ; Mask out the high bit (because the shift is arithmetic)
result = temp / divisor ; Divide
result << 1 ; Double result by shifting left (or just * 2)
temp = value & $01 ; Mask out all but the low bit of value
temp / divisor ; Divide again (just in case we are dividing by 1)
result + temp ; Combine the two results
EndIf
Debug result
ProcedureReturn result
EndProcedure
v.q = $0FFFFFFFF
Debug v / 2
Debug v / 3
Debug v / 4
Debug v / 5
Debug v / 6
Debug v / 7
Debug v / 99
Debug "============="
r = uLongDiv(-1,2)
r = uLongDiv(-1,3)
r = uLongDiv(-1,4)
r = uLongDiv(-1,5)
r = uLongDiv(-1,6)
r = uLongDiv(-1,7)
r = uLongDiv(-1,99)