Irvine'sRandomRange
在 0..EAX 范围内创建数字。获得范围 -10..10 是一个解释问题:
0 表示 -10
1 表示 -9
2 表示 -8
...
10 表示 0
...
18 表示 8
19 表示 9
20 表示 10
所以让我们RandomRange
得到范围 0..20 并通过减去 10 重新解释结果:
INCLUDE Irvine32.inc
.DATA
cuntx dword 100 dup(0) ; http://www.parkedlikeacunt.com/?p=901
space byte " ",0 ; String with spaces and terminating null for `WriteString`
delta dword 21 ; Range 0..20, one more needed for `RandomRange`
.CODE
main PROC
call Randomize ; Initialize randomizer
lea edx, space ; Space for `WriteString`
mov ecx, LENGTHOF cuntx
L1:
mov eax,delta ; Range 0..20
call RandomRange ; Get a number in the range
mov esi, eax
call WriteDec ; Write the number
call WriteString ; Space
sub eax, 10 ; Subtract 10 from EAX to get the range -10..10
call WriteInt ; Write the reinterpreted number
call WriteString ; Space
inc cuntx[esi * 4] ; This was wrong in the OP
mov eax, cuntx[esi * 4]
call WriteDec ; Write the current count of the number
call Crlf ; New line
loop L1
exit
main ENDP
END main
OP 的程序指示了一个必须计算的变量范围:
INCLUDE Irvine32.inc
.DATA
a DWORD -10 ; Lower limit
b DWORD 10 ; Upper limit
cuntx dword 100 dup(0) ; http://www.parkedlikeacunt.com/?p=901
space byte " ",0 ; String with spaces and terminating null for `WriteString`
delta dword 0 ; Range 0..20, one more needed for `RandomRange`
.CODE
main PROC
call Randomize ; Initialize randomizer
; Calculate `delta` (condition: a < b)
mov eax, a
cdq
xor eax, edx
sub eax, edx
mov ebx, eax ; EBX = abs(a)
mov eax, b ; EAX = b
add eax, ebx ; abs(a) + b = upper limit of the range
inc eax ; Plus one for `RandomRange`
mov delta, eax
lea edx, space ; Space for `WriteString`
mov ecx, LENGTHOF cuntx
L1:
mov eax,delta ; Range 0..20
call RandomRange ; Get a number in the range
mov esi, eax
call WriteDec ; Write the number
call WriteString ; Space
add eax, a ; Add lower limit to EAX to get the range a..b
call WriteInt ; Write the reinterpreted number
call WriteString ; Space
inc cuntx[esi * 4] ; This was wrong in the OP
mov eax, cuntx[esi * 4]
call WriteDec ; Write the current count of the number
call Crlf ; New line
loop L1
exit
main ENDP
END main