Unsigned compare

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Unsigned compare

Post 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)
Last edited by mk-soft on Fri Mar 10, 2017 10:35 pm, edited 3 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Unsigned compare

Post by freak »

ebx and rbx are not volatile registers. You have to preserve their content if you use them.
quidquid Latine dictum sit altum videtur
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Unsigned compare

Post by mk-soft »

Bug found...

Update v1.02
Now for 32bit and 64bit

Code: Select all

...
GT :wink:
Last edited by mk-soft on Fri Mar 10, 2017 8:21 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Unsigned compare

Post by mk-soft »

Make small update because labels...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Unsigned compare

Post by Keya »

you still have the r/ebx issue freak pointed out :)
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Unsigned compare

Post by mk-soft »

Ups :(

Now without rbx and ebx registers...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Unsigned compare

Post by idle »

if you're not using osx you could try the EnableUnsigned compiler macro
http://www.purebasic.fr/english/viewtop ... 12&t=65312
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Unsigned compare

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply