Bare metal programming in PureBasic, for experienced users
ker2x
User
Posts: 38 Joined: Sat May 10, 2008 7:52 am
Location: SecondLife
Post
by ker2x » Fri Nov 13, 2015 7:49 pm
Code: Select all
Procedure.f mrdrand()
EnableASM
;MOV RAX, 0
RDRAND EAX
DisableASM
ProcedureReturn
EndProcedure
I get a syntax error on RDRAND line.
(works if i comment it and uncomment the previous one. but... well... obviously, i want a random number as a result, not 0 :p )
btw, i rand some test, RDRAND is slower than calling Random()
Keya
Addict
Posts: 1891 Joined: Thu Jun 04, 2015 7:10 am
Post
by Keya » Fri Nov 13, 2015 7:56 pm
try "!rdrand eax" instead of "rdrand eax"? and check cpuid because i think only newer cpus support it, mine doesnt heehee
ker2x
User
Posts: 38 Joined: Sat May 10, 2008 7:52 am
Location: SecondLife
Post
by ker2x » Fri Nov 13, 2015 8:05 pm
mine support it. (i7 4770k)
what is this ! operator ?
now i have an error on the next instruction
The code i created using /REASM :
Code: Select all
_Procedure4:
;PS4=48
;SUB rsp,40
;
;
;
; ProcedureReturn ((Random(2147483647)/(536870911)) - 2.0)
;PUSH qword 2147483647
;POP rcx
;CALL PB_Random
;MOV qword [rsp-8],rax
;FILD qword [rsp-8]
RDRAND EAX
FILD EAX
FDIV dword [F14]
FADD dword [F15]
MOVSXD rax,eax
JMP _EndProcedure5
ker2x
User
Posts: 38 Joined: Sat May 10, 2008 7:52 am
Location: SecondLife
Post
by ker2x » Fri Nov 13, 2015 8:07 pm
well, it's in the manual of course !!
- It's possible to pass directly an assembly line to the assembler without being processed by the compiler by using the '!' character at the line start. This allow to have a full access to the assembler directives. When using this, it's possible to reference the local variables using the notation 'p.v_variablename' for a regular variable or 'p.p_variablename' for a pointer
wilbert
PureBasic Expert
Posts: 3870 Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands
Post
by wilbert » Fri Nov 13, 2015 8:23 pm
I didn't know that instruction but it seems to work fine on my computer
Code: Select all
Procedure rdrand()
!rdrand_loop:
!rdrand eax
!jnc rdrand_loop
ProcedureReturn
EndProcedure
Debug rdrand()
Keya is right that it would be wise to check if the cpu supports this feature.
Windows (x64)
Raspberry Pi OS (Arm64)
ker2x
User
Posts: 38 Joined: Sat May 10, 2008 7:52 am
Location: SecondLife
Post
by ker2x » Fri Nov 13, 2015 9:03 pm
Code: Select all
Procedure.f mrdrand()
Define r.f
Define d.f = 536870911
Define two.f = 2
EnableASM
! RDRAND eax
! MOV dword [p.v_r], eax
! FILD dword [p.v_r]
! FDIV dword [p.v_d]
! FSUB dword [p.v_two]
! FST dword [p.v_d]
! MOV EAX, [p.v_d]
DisableASM
ProcedureReturn
EndProcedure