简体   繁体   中英

Assembly Random Number Using Irvine

When I implement the random number generator procedure that was provided for us in Assembly, half the time it gives me a division by zero error and the other half of the time it works perfectly. I believe that I am implementing the code properly, but will provide you how I have it written.

randomCompNum PROC
    call Randomize               ;Sets seed
    mov  eax,10                  ;Keeps the range 0 - 9

    call RandomRange
    mov  compNum1,eax            ;First random number

L1: call RandomRange
    .IF eax == compNum1          ;Checks if the second number is the same as the first
        jmp L1                   ;If it is, repeat call
    .ENDIF
     mov compNum2,eax            ;Second random number

L2: call RandomRange
    .IF eax == compNum1          ;Checks if the third number is the same as the first
        jmp L2                   ;If it is, repeat
    .ELSEIF eax == compNum1      ;Checks if the third number is the same as the second
        jmp L2                   ;If it is, repeat
    .ENDIF
    mov compNum3,eax             ;Third random number stored

    ret

randomCompNum ENDP

Here's the disassembly for RandomRange that Visual Studios provided me

_RandomRange@0:
004019C1  push         ebx  
004019C2  push         edx  
004019C3  mov          ebx,eax  
004019C5  call         _Random32@0 (4019A6h)  ;<---- This function doesn't touch ebx
004019CA  mov          edx,0  
004019CF  div          eax,ebx     ;<---- That's where the error occurs
004019D1  mov          eax,edx  
004019D3  pop          edx  
004019D4  pop          ebx  
004019D5  ret

Do you know what could be causing this error?

I'm tempted to just create my own random number generator.

Background on the RandomRange method: It's simple. You set the seed with Randomize, moving 10 into eax keeps the RandomRange between 0 - 9. That's all the documentation I could find for that function, so that's all I believe there is about it.

I realize this is an old question, but a friend of mine just referenced it.

Your mov eax, 10 belongs to the call RandomRange , so your sample of code should read:

randomCompNum PROC
    call Randomize               ;Sets seed

    mov  eax,10                  ;Keeps the range 0 - 9
    call RandomRange
    mov  compNum1,eax            ;First random number

L1: mov  eax,10                  ;Keeps the range 0 - 9
    call RandomRange
    .IF eax == compNum1          ;Checks if the second number is the same as the first
        jmp L1                   ;If it is, repeat call
    .ENDIF
     mov compNum2,eax            ;Second random number

L2: mov  eax,10                  ;Keeps the range 0 - 9
    call RandomRange
    .IF eax == compNum1          ;Checks if the third number is the same as the first
        jmp L2                   ;If it is, repeat
    .ELSEIF eax == compNum1      ;Checks if the third number is the same as the second
        jmp L2                   ;If it is, repeat
    .ENDIF
    mov compNum3,eax             ;Third random number stored

    ret
randomCompNum ENDP

The mov eax,10 is the parameter sent to the RandomRange function, it would be RandomRange(10); in C. Note that because RandomRange returns its result in eax, you need to set it up before each call.

Random32  PROC
;
; Generates an unsigned pseudo-random 32-bit integer
;   in the range 0 - FFFFFFFFh.

Random32 CAN return 0 that is probably why it is bombing on you when you divide by 0 :-)

The Irvine source is available, just modify RandomRange to handle a 0 when returned from Random32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM