rand.asm

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc3100/spring2026/notes/flags/code/rand.asm
 
%include "CONSTANTS.h"

section .data

    randNum: dq 0
    fmt: db `The number is %ld, this was %d bytes\n`,0
    errFMT: db `the call to sys_getrandom failed \n`,0

section .bss

section .text

global main
main:
    ;mov rdi, format1
    ;mov rsi, -1
    ;call CallPrintf 

    mov r12, 11

TOP:
    dec  r12
    jz  done

    ; 318 is the system call number for getrandom
    mov rax, 318
    ; address
    mov rdi, randNum
    ; number of bytes
    mov rsi, 7
    ; no flags
    mov rdx, 0
    syscall

    cmp rax, -1
    je ERROR

    mov rdi, fmt
    ; random value
    mov rsi, [randNum]
    ; number of bytes
    mov rdx, rax
    call CallPrintf

    jmp TOP 

ERROR:
    mov rdi, errFMT
    call CallPrintf
   
done:

    jmp Exit