1
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 计算每个数字。我该怎么做?

4

1 回答 1

1

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
于 2015-12-29T10:33:47.637 回答