如何在 Win64 NASM 程序集中正确使用 rand?
How is rand properly used in Win64 NASM Assembly?
我不确定如何在 x64 程序集中获取特定范围内的随机数。
我在 SO 和网络上看到的大多数示例都在 Linux 上,因此非常感谢有关如何在 Windows 上进行操作的建议!
default rel
extern printf
extern scanf
extern rand
section .rdata ;; constants
prompt: db "What is your guess? (Answer is: %d) ", 0
congrats: db "You guessed it!", 10, 0
section .data ;; mutable variables
guess: dq 0
target: dq 0 ; assumed constant, but technically not
section .text
global main
main:
stack_reserve: equ 40
sub rsp, stack_reserve
;; let's assign our random target
;; rand(num_target) % 100 + 1
lea rcx, [prompt]
call printf
add rsp, stack_reserve
ret
我终于弄明白了:
;; calculate the random number
xor rax, rax
mov rcx, rax
call time
mov rcx, rax
call srand
call rand
;; rand = (rand % 100) + 1
xor rdx, rdx
mov rcx, 100
div rcx
inc rdx
mov [target], rdx
mov rcx, scan_fmt
call printf
我不确定如何在 x64 程序集中获取特定范围内的随机数。 我在 SO 和网络上看到的大多数示例都在 Linux 上,因此非常感谢有关如何在 Windows 上进行操作的建议!
default rel
extern printf
extern scanf
extern rand
section .rdata ;; constants
prompt: db "What is your guess? (Answer is: %d) ", 0
congrats: db "You guessed it!", 10, 0
section .data ;; mutable variables
guess: dq 0
target: dq 0 ; assumed constant, but technically not
section .text
global main
main:
stack_reserve: equ 40
sub rsp, stack_reserve
;; let's assign our random target
;; rand(num_target) % 100 + 1
lea rcx, [prompt]
call printf
add rsp, stack_reserve
ret
我终于弄明白了:
;; calculate the random number
xor rax, rax
mov rcx, rax
call time
mov rcx, rax
call srand
call rand
;; rand = (rand % 100) + 1
xor rdx, rdx
mov rcx, 100
div rcx
inc rdx
mov [target], rdx
mov rcx, scan_fmt
call printf