Page 1 of 1

RDRAND ASM Syntax error

Posted: Fri Nov 13, 2015 7:49 pm
by ker2x

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() :(

Re: RDRAND ASM Syntax error

Posted: Fri Nov 13, 2015 7:56 pm
by Keya
try "!rdrand eax" instead of "rdrand eax"? and check cpuid because i think only newer cpus support it, mine doesnt heehee :)

Re: RDRAND ASM Syntax error

Posted: Fri Nov 13, 2015 8:05 pm
by ker2x
mine support it. (i7 4770k)

what is this ! operator ?
now i have an error on the next instruction :D

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

Re: RDRAND ASM Syntax error

Posted: Fri Nov 13, 2015 8:07 pm
by ker2x
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

Re: RDRAND ASM Syntax error

Posted: Fri Nov 13, 2015 8:23 pm
by wilbert
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.

Re: RDRAND ASM Syntax error

Posted: Fri Nov 13, 2015 9:03 pm
by ker2x

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