UnsignedCompare

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
mk-soft
Beiträge: 3856
Registriert: 24.11.2004 13:12
Wohnort: Germany

UnsignedCompare

Beitrag von mk-soft »

Ein Variable von Type Long kann man auch als Unsigned betrachten und so mit rechnen und ausgeben (StrU(... , #PB-Long).
Es fehlen nur die Vergleichsoperationen für Unsigned.

Habe mal was dafür gebastelt.

Code: Alles auswählen

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

Procedure Above(a.l, b.l)

EnableASM
; If a > b
  MOV    ebx,dword [p.v_a]
  CMP    ebx,dword [p.v_b]
  JBE   l_not_above
; ProcedureReturn #True
  MOV    eax,1
  ProcedureReturn
not_above:
; ProcedureReturn #False
  XOr    eax,eax
  ProcedureReturn
DisableASM

EndProcedure

; ***************************************************************************************

Procedure AboveEqual(a.l , b.l)

EnableASM
; If a >= b
  MOV    ebx,dword [p.v_a]
  CMP    ebx,dword [p.v_b]
  JB   l_not_above_equal
; ProcedureReturn #True
  MOV    eax,1
  ProcedureReturn
not_above_equal:
; ProcedureReturn #False
  XOr    eax,eax
  ProcedureReturn
DisableASM
  
EndProcedure

; ***************************************************************************************

Procedure Below(a.l, b.l)

EnableASM
; If a < b
  MOV    ebx,dword [p.v_a]
  CMP    ebx,dword [p.v_b]
  JAE   l_not_below
; ProcedureReturn #True
  MOV    eax,1
  ProcedureReturn
not_below:
; ProcedureReturn #False
  XOr    eax,eax
  ProcedureReturn
DisableASM
  
EndProcedure

; ***************************************************************************************

Procedure BelowEqual(a.l, b.l)

EnableASM
; If a <= b
  MOV    ebx,dword [p.v_a]
  CMP    ebx,dword [p.v_b]
  JA   l_not_below_equal
; ProcedureReturn #True
  MOV    eax,1
  ProcedureReturn
not_below_equal:
; ProcedureReturn #False
  XOr    eax,eax
  ProcedureReturn
DisableASM
  
EndProcedure

; ***************************************************************************************

; test

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)
FF :wink:
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Benutzeravatar
mk-soft
Beiträge: 3856
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: UnsignedCompare

Beitrag von mk-soft »

Kleines Problem bei X64 Version. Es kommt ein falsches Ergebnis herraus

Code: Alles auswählen

  Procedure Above(a.i, b.i)
  
  
  EnableASM
  ; If a > b
    MOV    rbx,qword [p.v_a]
    CMP    rbx,qword [p.v_b]
    JBE   l_not_above
  ; ProcedureReturn #True
    MOV    rax,1
    ProcedureReturn
  not_above:
  ; ProcedureReturn #False
    XOr    rax,rax
    ProcedureReturn
  DisableASM
  
  EndProcedure
  
??? :|
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
andi256
Beiträge: 100
Registriert: 06.11.2004 11:23
Computerausstattung: PB 5.30 (x64) Win7
Wohnort: Österreich

Re: UnsignedCompare

Beitrag von andi256 »

hast du das von "remi_meier" mal gelesen / getestet ?

weis nicht ob Dir dieses Thread hilft -> http://forums.purebasic.com/german/view ... 6fe2731926

Code: Alles auswählen

Procedure.l UINT_Greater(a.l, b.l) 
  !XOR Eax,Eax 
  !MOV Ebx,[p.v_a] 
  !CMP Ebx,[p.v_b] 
  !SETA al 
  ProcedureReturn 
EndProcedure
aber obs auf 64bit maschinen läuft keine Ahnung ... bin ein ASM noop ... und hab kein 64bit rechner :-(

mfg andi
Benutzeravatar
mk-soft
Beiträge: 3856
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: UnsignedCompare

Beitrag von mk-soft »

Habe den Fehler gefunden und im englischen Forum gemeldet.

Hier jetzt auch mit 64bit Version.

Code: Alles auswählen

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

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86 

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

CompilerElse 

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

  EnableASM
  ; 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
  not_above:
  ; ProcedureReturn #False
    XOr    rax,rax
    ProcedureReturn
  DisableASM
  
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure AboveEqual(a.i , b.i)
  
  EnableASM
  ; 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
  not_above_equal:
  ; ProcedureReturn #False
    XOr    rax,rax
    ProcedureReturn
  DisableASM
    
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure Below(a.i, b.i)
  
  EnableASM
  ; 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
  not_below:
  ; ProcedureReturn #False
    XOr    rax,rax
    ProcedureReturn
  DisableASM
    
  EndProcedure
  
  ; ***************************************************************************************
  
  Procedure BelowEqual(a.i, b.i)
  
  EnableASM
  ; 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
  not_below_equal:
  ; ProcedureReturn #False
    XOr    rax,rax
    ProcedureReturn
  DisableASM
    
  EndProcedure
  
  ; ***************************************************************************************

CompilerEndIf
; test

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)

FF :wink:
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Benutzeravatar
NicTheQuick
Ein Admin
Beiträge: 8812
Registriert: 29.08.2004 20:20
Computerausstattung: Ryzen 7 5800X, 64 GB DDR4-3200
Ubuntu 24.04.2 LTS
GeForce RTX 3080 Ti
Wohnort: Saarbrücken

Re: UnsignedCompare

Beitrag von NicTheQuick »

Kann man sowas auch in Makros packen?
Benutzeravatar
mk-soft
Beiträge: 3856
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: UnsignedCompare

Beitrag von mk-soft »

Schwierig wegen der Zuweisung des Ergebnisses... :cry:
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Antworten