Some procedures for working with unsigned variables

Share your advanced PureBasic knowledge/code with the community.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Some procedures for working with unsigned variables

Post by wilbert »

Convert an unsigned quad to a string

Code: Select all

Procedure.s UStrQ(n.q)
  Protected tmp.l
  !mov ecx, 10
  !xor edx, edx
  !mov eax, [p.v_n + 4]
  !div ecx
  !mov [p.v_n + 4], eax
  !mov eax, [p.v_n]
  !div ecx
  !mov [p.v_n], eax
  !mov [p.v_tmp], edx
  If n
    ProcedureReturn Str(n) + Str(tmp)
  Else
    ProcedureReturn Str(tmp)
  EndIf
EndProcedure



Debug UStrQ($ffffffffffffffff)


Unsigned compare

Code: Select all

Procedure.i UComL(n1.l, n2.l); returns -1 if n1 < n2, 0 if n2 = n2, 1 if n1 > n2 
  !mov eax, [p.v_n1]
  !sub eax, [p.v_n2]
  !jz ucoml_exit
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !sbb rax, rax
  CompilerElse
    !sbb eax, eax
  CompilerEndIf
  !jnz ucoml_exit
  !inc eax
  !ucoml_exit:
  ProcedureReturn
EndProcedure

Procedure.i UComQ(n1.q, n2.q); returns -1 if n1 < n2, 0 if n2 = n2, 1 if n1 > n2 
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov rax, [p.v_n1]
    !sub rax, [p.v_n2]
    !jz ucomq_exit
    !sbb rax, rax
    !jnz ucomq_exit
  CompilerElse
    !mov ecx, [p.v_n1]
    !mov edx, [p.v_n1 + 4]
    !sub ecx, [p.v_n2]
    !sbb edx, [p.v_n2 + 4]
    !sbb eax, eax
    !jnz ucomq_exit
    !or ecx, edx
    !jz ucomq_exit
  CompilerEndIf
  !inc eax
  !ucomq_exit:
  ProcedureReturn
EndProcedure



If UComL($7fffffff, $ffffffff) < 0
  Debug "$7fffffff < $ffffffff"
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Some procedures for working with unsigned variables

Post by Fred »

StrU() should work as well:

Code: Select all

Debug StrU($ffffffffffffffff, #PB_Quad)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Some procedures for working with unsigned variables

Post by wilbert »

Fred wrote:StrU() should work as well:

Code: Select all

Debug StrU($ffffffffffffffff, #PB_Quad)
Thanks. I didn't know StrU existed.
Has that always been there ? I must have overlooked it.
Windows (x64)
Raspberry Pi OS (Arm64)
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Some procedures for working with unsigned variables

Post by Fred »

Yep, almost since the beginning :)
Post Reply