Skip to content

Instantly share code, notes, and snippets.

@chinchila
Created January 14, 2018 05:32
Show Gist options
  • Save chinchila/317de6d930f0712c36eb21cf518cfde0 to your computer and use it in GitHub Desktop.
Save chinchila/317de6d930f0712c36eb21cf518cfde0 to your computer and use it in GitHub Desktop.
Example of rdrand eax or rax in C.
#include <stdio.h>
#include <stdint.h>
#ifdef __i386__
uint32_t hwrand()
{
uint32_t eax;
__asm__ volatile( ".byte 0x0f, 0xc7, 0xf0;" : "=a"( eax ) );
return eax;
}
#else
uint64_t hwrand()
{
uint64_t rax;
__asm__ volatile( ".byte 0x48, 0x0f, 0xc7, 0xf0;" : "=a"( rax ) );
return rax;
}
#endif
int
main()
{
#ifdef __i386__
printf("%u\n", hwrand() );
#else
printf("%lu\n", hwrand() );
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment