Created
January 14, 2018 05:32
-
-
Save chinchila/317de6d930f0712c36eb21cf518cfde0 to your computer and use it in GitHub Desktop.
Example of rdrand eax or rax in C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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