TITLE random practice (rand.asm)
; Penpa Gyaltsen,csc310
INCLUDE Irvine32.inc
.data
a dword 1
b dword 10
cuntx dword 100 dup(0)
space byte " ",0
delta dword ?
.code
main PROC
call randomize
mov eax, 0
mov esi, 0
mov eax, b
sub eax, a
inc eax
mov delta, eax
mov ecx, delta
mov esi, offset cuntx
mov edx, offset space
mov ecx, lengthof cuntx
L1:
mov eax,delta
call randomrange
inc eax
call writedec
call writestring
mov esi,eax
inc cuntx[esi]
mov eax,cuntx[esi]
call writedec
call crlf
loop L1;
exit
main ENDP
END main
我需要计算从-10到10的每个数字。我该怎么做?
答案 0 :(得分:1)
Irvine的RandomRange
创建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