Page 1 of 1

Unsigned compare

Posted: Wed Dec 09, 2009 10:13 am
by mk-soft
One can regard a variable of type Long also as Unsigned. Only comparison operations for Unsigned are missing.

Update v1.04

Code: Select all

;-TOP
; Kommentar     : 
; Author        : mk-soft
; Second Author : 
; Datei         : UnsignedCompare.pb
; Version       : 1.05
; Erstellt      : 10.03.2009
; Geändert      : 10.03.2017
; 
; Compilermode  :
;
; ***************************************************************************************

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86 

  ; X86 ************************************************************************************
 
  Procedure Above(a.i, b.i)
  
    ; If a > b
    !MOV    eax,dword [p.v_a]
    !CMP    eax,dword [p.v_b]
    !JBE    l_not_above
    ; ProcedureReturn #True
    !MOV    eax,1
    ProcedureReturn
    !l_not_above:
    ; ProcedureReturn #False
    !XOr    eax,eax
    ProcedureReturn
  
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure AboveEqual(a.i , b.i)
  
    ; If a >= b
    !MOV    eax,dword [p.v_a]
    !CMP    eax,dword [p.v_b]
    !JB     l_not_above_equal
    ; ProcedureReturn #True
    !MOV    eax,1
    ProcedureReturn
    !l_not_above_equal:
    ; ProcedureReturn #False
    !XOR    eax,eax
    ProcedureReturn
  DisableASM
    
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure Below(a.i, b.i)
  
    ; If a < b
    !MOV    eax,dword [p.v_a]
    !CMP    eax,dword [p.v_b]
    !JAE    l_not_below
    ; ProcedureReturn #True
    !MOV    eax,1
    ProcedureReturn
    !l_not_below:
    ; ProcedureReturn #False
    !XOr    eax,eax
    ProcedureReturn
    
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure BelowEqual(a.i, b.i)
  
    ; If a <= b
    !MOV    eax,dword [p.v_a]
    !CMP    eax,dword [p.v_b]
    !JA     l_not_below_equal
    ; ProcedureReturn #True
    !MOV    eax,1
    ProcedureReturn
    !l_not_below_equal:
    ; ProcedureReturn #False
    !XOr    eax,eax
    ProcedureReturn
  DisableASM
    
  EndProcedure

CompilerElse 

 ; X64 ************************************************************************************
 
  Procedure Above(a.q, b.q)

    ; If a >= b
    !MOV    r15,qword [p.v_a]
    !CMP    r15,qword [p.v_b]
    !JBE    l_not_above
    ; ProcedureReturn #True
    !MOV    rax,1
    ProcedureReturn
    !l_not_above:
    ; ProcedureReturn #False
    !XOr    rax,rax
    ProcedureReturn
  
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure AboveEqual(a.i , b.i)
  
    ; If a >= b
    !MOV    r15,qword [p.v_a]
    !CMP    r15,qword [p.v_b]
    !JB     l_not_above_equal
    ; ProcedureReturn #True
    !MOV    rax,1
    ProcedureReturn
    !l_not_above_equal:
    ; ProcedureReturn #False
    !XOr    rax,rax
    ProcedureReturn
    
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure Below(a.i, b.i)
  
    ; If a < b
    !MOV    r15,qword [p.v_a]
    !CMP    r15,qword [p.v_b]
    !JAE    l_not_below
    ; ProcedureReturn #True
    !MOV    rax,1
    ProcedureReturn
    !l_not_below:
    ; ProcedureReturn #False
    !XOr    rax,rax
    ProcedureReturn
    
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure BelowEqual(a.i, b.i)
  
    ; If a <= b
    !MOV    r15,qword [p.v_a]
    !CMP    r15,qword [p.v_b]
    !JA     l_not_below_equal
    ; ProcedureReturn #True
    !MOV    rax,1
    ProcedureReturn
    !l_not_below_equal:
    ; ProcedureReturn #False
    !XOr    rax,rax
    ProcedureReturn
    
  EndProcedure
  
  ; ***************************************************************************************

CompilerEndIf
; test

c = Above(1,2)

Debug "Unsigned grösser"
Debug Above( -1, 1)
Debug Above( 1, 1)

Debug "Unsigned grösser gleich"
Debug AboveEqual( -1, 1)
Debug AboveEqual( 1, 1)

Debug "Unsigned kleiner"
Debug Below( 1, -1)
Debug Below( 1, 1)

Debug "Unsigned kleiner gleich"
Debug BelowEqual( 1, -1)
Debug BelowEqual( 1, 1)

Re: Unsigned compare

Posted: Wed Dec 09, 2009 7:25 pm
by freak
ebx and rbx are not volatile registers. You have to preserve their content if you use them.

Re: Unsigned compare

Posted: Thu Dec 10, 2009 4:21 pm
by mk-soft
Bug found...

Update v1.02
Now for 32bit and 64bit

Code: Select all

...
GT :wink:

Re: Unsigned compare

Posted: Fri Mar 10, 2017 6:21 pm
by mk-soft
Make small update because labels...

Re: Unsigned compare

Posted: Fri Mar 10, 2017 7:05 pm
by Keya
you still have the r/ebx issue freak pointed out :)

Re: Unsigned compare

Posted: Fri Mar 10, 2017 8:23 pm
by mk-soft
Ups :(

Now without rbx and ebx registers...

Re: Unsigned compare

Posted: Fri Mar 10, 2017 9:49 pm
by idle
if you're not using osx you could try the EnableUnsigned compiler macro
http://www.purebasic.fr/english/viewtop ... 12&t=65312

Re: Unsigned compare

Posted: Fri Mar 10, 2017 10:39 pm
by mk-soft
@idle
Very nice, but i use mac os... :wink:

PB helps
- On x86 processors, the available volatile registers are: eax, ecx and edx, xmm0, xmm1, xmm2 and xmm3. All others must be always preserved.
- On x64 processors, the available volatile registers are: rax, rcx, rdx, r8, r9, xmm0, xmm1, xmm2 and xmm3. All others must be always preserved.
I have changed the code to valid registers.