note this patches the assembly in the assembler and could yield unexpected results, use with caution!
Code: Select all
;EnableUnsigned v1.0a
;Author Idle 25/3/16
;Supports PB 5.42LTS Window, Linux
;Unsigned arithmetic and logical comparisons for
;x64: Long, Integer, Quads
;x86: Long, Integer
;Usage:
;Scope unsigned arithmetic and logic in EnableUnsigned() DisableUnsigned() blocks
;EnableUnsigned()
;if ux > uy ; changes the operators to unsigned: supports < > <= >= <> =
; do something unsigned: supports * / + - << >>
; DisableUnsigned()
; ;back to signed
;EndIf
;Disableunsigned() ;Note you can call Disableunsigned() where ever you need it
CompilerIf SizeOf(integer) = 4
Macro EnableUnsigned()
!macro IDIV var
!{
!mov edx,0
!div var
!}
!macro IMUL reg,var
!{
!mov eax,reg
!mul var
!mov reg,eax
!}
!macro SAR reg,var
!{
!shr reg,var
!}
!macro SAL reg,var
!{
!shl reg,var
!}
!macro CDQ {}
!macro JG arg
!{
!JA arg
!}
!macro JGE arg
!{
!JAE arg
!}
!macro JL arg
!{
!JB arg
!}
!macro JLE arg
!{
!JBE arg
!}
EndMacro
Macro DisableUnsigned()
!purge IDIV
!purge IMUL
!purge SAR
!purge SAL
!purge CDQ
!purge JG
!purge JGE
!purge JL
!purge JLE
EndMacro
CompilerElse
Macro EnableUnsigned()
!macro IDIV var
!{
!mov rdx,0
!div var
!}
!macro IMUL reg,var
!{
!match =qword x , var
!\{ mov rax, reg
!mov r15, var
!\}
!mul reg
!mov reg,rax
!}
!macro SAR reg,var
!{
!shr reg,var
!}
!macro SAL reg,var
!{
!shl reg,var
!}
!macro MOVSXD reg,var
!{
!match =dword x , var
!\{ mov eax, var
!mov reg,rax \}
!}
!macro CQO {}
!macro CDO {}
!macro CWD {}
!macro CBW {}
!macro CWDE{}
!macro CDQE {}
!macro CDQ {}
!macro JG arg
!{
!JA arg
!}
!macro JGE arg
!{
!JAE arg
!}
!macro JL arg
!{
!JB arg
!}
!macro JLE arg
!{
!JBE arg
!}
EndMacro
Macro DisableUnsigned()
!purge IDIV
!purge IMUL
!purge SAR
!purge SAL
!purge MOVSXD
!purge CQO
!purge CDO
!purge CWD
!purge CBW
!purge CWDE
!purge CDQE
!purge CDQ
!purge JG
!purge JGE
!purge JL
!purge JLE
EndMacro
CompilerEndIf
OpenConsole()
CompilerIf #PB_Compiler_IsMainFile
Global x.l,y.l
x=2
y=$FFFFFFFC
If x > y
PrintN("Result not unsigned, activating EnableUnsigned()")
EnableUnsigned()
EndIf
If y >= x
PrintN("OK Unsigned Enabled ")
EndIf
If PeekL(@y) >= PeekL(@x)
PrintN("OK peekL Unsigned Enabled ")
EndIf
x = -1
If x = $ffffffff
PrintN("unsigned -1")
EndIf
y=$FFFFFFFE
x=2
If x < y
PrintN("OK x <= y")
y / 2
PrintN(Hex(y,#PB_Long) + " " + StrU(y)) ;7FFFFFFF
y * x
PrintN(Hex(y,#PB_Long) + " " + StrU(y)) ;FFFFFFFE
y >> 1
PrintN(Hex(y,#PB_Long) + " " + StrU(y)) ;7FFFFFFF
y << 1
PrintN(Hex(y,#PB_Long) + " " + StrU(y)) ;FFFFFFFE
y - x
PrintN(Hex(y,#PB_Long) + " " + StrU(y)) ;FFFFFFFC
y + x
PrintN(Hex(y,#PB_Long) + " " + StrU(y)) ;FFFFFFFE
DisableUnsigned()
y / 2
PrintN(Hex(y,#PB_Long) + " " + StrU(y) + " should be 7FFFFFFF if unsigned was used ")
y * x
PrintN(Hex(y,#PB_Long) + " " + StrU(y)) ;FFFFFFFE -
EndIf
x = -1
If x = $ffffffff
PrintN("signed -1")
EndIf
CompilerIf SizeOf(Integer) = 8;
Global xx.q,yy.q
xx=2
yy=$FFFFFFFFFFFFFFFC
Debug yy
If xx > yy
PrintN("Result not unsigned, activating EnableUnsigned()")
EnableUnsigned()
EndIf
If yy >= xx
PrintN("OK Unsigned Enabled ")
EndIf
If PeekQ(@yy) >= PeekQ(@xx)
PrintN("OK peekQ Unsigned Enabled ")
EndIf
yy=$FFFFFFFFFFFFFFFE
If xx <= yy
PrintN("OK xx <= yy")
yy / 2
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy)) ;7FFFFFFFFFFFFFFF
yy * xx
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy)) ;FFFFFFFFFFFFFFFE
yy >> 1
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy)) ;7FFFFFFFFFFFFFFF
yy << 1
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy)) ;FFFFFFFFFFFFFFFE
yy - xx
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy)) ;FFFFFFFFFFFFFFFC
yy + xx
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy)) ;FFFFFFFFFFFFFFFE
DisableUnsigned()
yy / 2
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy) + " should be 7FFFFFFFFFFFFFFF if unsigned was used ")
yy * xx
PrintN(Hex(yy,#PB_Quad) + " " + StrU(yy)) ;FFFFFFFFFFFFFFFE-
EndIf
CompilerEndIf
CompilerEndIf
Input()
CloseConsole()