Nasm - "real" randomness via uninitialized variables?

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

In NASM, uninitialized variables are typically filled with zeros or other default values by the operating system or the runtime environment. Therefore, relying on uninitialized variables for "real" randomness is not a reliable approach.

If you need to generate random numbers or obtain random data in NASM, it is recommended to use a proper random number generator or a library that provides random number generation functions. For example, you can use the rand function from the C library, which can be called from NASM using appropriate function call conventions.

Here's an example NASM code that uses the rand function to generate a random number:

assembly
section .data
message db "Random number: %d", 0

section .text
extern printf
extern rand

global _start

_start:
; Initialize the random number generator
mov eax, 0
call srand

; Generate a random number
call rand
mov ebx, eax

; Print the random number
push ebx
push message
call printf
add esp, 8

; Exit the program
mov eax, 1
xor ebx, ebx
int 0x80

Note that this example assumes a Linux environment and uses the printf and srand functions from the C library. If you're working on Windows, you would need to adapt the code accordingly and link against the appropriate libraries.

It's important to use proper random number generation techniques to ensure the randomness and security of your application. Relying on uninitialized variables is not a reliable or recommended approach.