%include "MY_IO.h" extern GET_RANDOM extern SEED_RANDOM extern RANDOM_SEED segment .data label1 db "rdtes " size1 equ $ - label1 label2 db "getrandom " size2 equ $ - label2 label3 db "time " size3 equ $ - label3 mod db " % " equal db " = " segment .bss segment .text global _start ; I am going to pass the arguments on the stack ; rbp + 16 will hold the generator to use 0-2 ; rbp + 24 will hold the label to print with ; rbp + 32 will hold the length of the label ; rbp + 40 will hold the times to run the computation ; rbp + 48 will hold the mod ; ; I will use the following registers, so I want to preserve them ; r15 to save results ; r14 for a lcv ; r13 to hold the mod value RUN_RANDOM_TEST: ; start the activation record push rbp mov rbp, rsp ; preserve any preserveds we will use push r15 push r14 push r13 push r12 ; seed the random number generator, ; get the value from the stack mov rdi,[rbp + 16] call RANDOM_SEED ; save the results just in case rax changes. (syscall in PRINT routines) ; cout << label << srand() << endl mov r15, rax; mov rdi, [rbp + 24] mov rsi, [rbp + 32] call PRINT_STRING call PRINT_NEWLINE mov rdi, r15 call PRINT_INT call PRINT_NEWLINE ; load the loop information mov r14, [rbp + 40] mov r13, [rbp + 48] top: test r14, r14 jz done ; r15 = rax = rand() call GET_RANDOM mov r15, rax ; rax = rax % modAmount mov rdx, 0 div r13 mov r12, rdx ; cout << r15 << " % " << r13 " = " << rax << endl mov rdi, r15 call PRINT_INT mov rdi, mod mov rsi, 3 call PRINT_STRING mov rdi, r13 call PRINT_INT mov rdi, equal mov rsi, 3 call PRINT_STRING mov rdi, r12 call PRINT_INT call PRINT_NEWLINE ; r14-- dec r14 jmp top done: ; exit code ; restore the preserved registers pop r12 pop r13 pop r14 pop r15 ; finally restore rbp and return pop rbp ret _start: ; to show that things work right. mov r15, 15 mov r14, 14 mov r13, 13 mov r12, 12 push 999999999 push 100 ; mod amount push 20 ; iterations push size1 push label1 push 0 call RUN_RANDOM_TEST call PRINT_NEWLINE call PRINT_NEWLINE ; propbably should pop everything, but mov qword [rsp], 1 mov qword [rsp+8], label2 mov qword [rsp+16], size2 call RUN_RANDOM_TEST call PRINT_NEWLINE call PRINT_NEWLINE ; propbably should pop everything, but mov qword [rsp], 2 mov qword [rsp+8], label3 mov qword [rsp+16], size3 call RUN_RANDOM_TEST call PRINT_NEWLINE call PRINT_NEWLINE ; remove the argumets from the stack add rsp, 40 ; just to prove the registers came back ; print the 12 mov rdi,r12 call PRINT_INT call PRINT_NEWLINE ; print the 13 mov rdi,r13 call PRINT_INT call PRINT_NEWLINE ; print the 14 mov rdi,r14 call PRINT_INT call PRINT_NEWLINE ; print the 15 mov rdi,r15 call PRINT_INT call PRINT_NEWLINE call PRINT_NEWLINE ; prove the stack is back too pop rdi call PRINT_INT call PRINT_NEWLINE ; exit mov eax, SYS_EXIT mov edx, SUCCESS syscall