Search code examples
assemblyrandommasmx86-16irvine32

RandomRange in assembly with Irvine


I am working on x86 assembly language. I want to get two parameters that one is column and other one is row, using randomrange within the interval [0,5]. Here I tried to adjust the interval by dividing the random number by 6, and getting the number remains in DL which is remainder of the division.

I also implemented an array that holds the numbers that randomized before and jumps back to randomization if the element is already 1, which uses row*6+col to check the index, but this array is not effective yet.

I get a segmentation fault, what could be the problem?

TITLE Program Template     (template.asm)


INCLUDE Irvine32.inc
INCLUDE macros.inc

.data
onezero BYTE 36 DUP(0)

row BYTE 0
col BYTE 0
.code
main PROC

_again:

call randomrange
mov bx, 6
div bx
mov row, dl

call randomrange
div bx
mov col, dl

movzx eax, row
mov ebx, 6
mul ebx
movzx ecx, col
add eax, ecx
mov edi, eax
mov al, 1
cmp al, onezero[edi]
je _again

movzx eax, row
call writeint
movzx eax, col
call writeint

main ENDP

END main

Solution

  • The irvine RandomRange function already has this functionality:

    ; Returns an unsigned pseudo-random 32-bit integer
    ; in EAX, between 0 and n-1. Input parameter:
    ; EAX = n.
    

    Also note that div bx is dividing the 32 bit number formed from dx:ax and not eax.

    As for the segfault, use a debugger and see where the crash is.