random


It gets more and more interesting to learn assembly.
I have read some tutorials and now I have a little problem. This code is meant to be a subroutine but I don't know how it works. Could someone please explain it?

Seed dw 3749h

some other stuff here..

mov ax, Seed
mov dx, 8405h
mul dx
inc ax
mov Seed, ax

This subroutine was named as Random (or something like that).
It is used in a fire effect program but what is the meaning of this?

Comments



  • :
    : It gets more and more interesting to learn assembly.
    : I have read some tutorials and now I have a little problem. This code is meant to be a subroutine but I don't know how it works. Could someone please explain it?
    :
    : Seed dw 3749h
    :
    : some other stuff here..
    :
    : mov ax, Seed
    : mov dx, 8405h
    : mul dx
    : inc ax
    : mov Seed, ax
    :
    : This subroutine was named as Random (or something like that).
    : It is used in a fire effect program but what is the meaning of this?

    This routine returns a "random" value in DX and AX.
    It multiplies two 16bit numbers together:

    AX (the seed) * DX (8405h)
    3749h*8405h = 1c82b86dh

    Since that result is too big to fit into a 16bit register, it gets returned in DX:AX, where DX=1c82 and AX=b86d.

    The "inc ax" is there just in case the result of the multiplication ends up being xxxx0000h, as that would make any subsequent calls return 0 as well.

    The seed value is then updated so that the next time the routine is called, a different "random" number is returned.

    I use the term "random" because every single time this program is executed, it will always return the same sequence of numbers, thus not really very random at all, but for this purpose it's random enough.



  • :
    :
    : :
    : : It gets more and more interesting to learn assembly.
    : : I have read some tutorials and now I have a little problem. This code is meant to be a subroutine but I don't know how it works. Could someone please explain it?
    : :
    : : Seed dw 3749h
    : :
    : : some other stuff here..
    : :
    : : mov ax, Seed
    : : mov dx, 8405h
    : : mul dx
    : : inc ax
    : : mov Seed, ax
    : :
    : : This subroutine was named as Random (or something like that).
    : : It is used in a fire effect program but what is the meaning of this?
    :
    : This routine returns a "random" value in DX and AX.
    : It multiplies two 16bit numbers together:
    :
    : AX (the seed) * DX (8405h)
    : 3749h*8405h = 1c82b86dh
    :
    : Since that result is too big to fit into a 16bit register, it gets returned in DX:AX, where DX=1c82 and AX=b86d.
    :
    : The "inc ax" is there just in case the result of the multiplication ends up being xxxx0000h, as that would make any subsequent calls return 0 as well.
    :
    : The seed value is then updated so that the next time the routine is called, a different "random" number is returned.
    :
    : I use the term "random" because every single time this program is executed, it will always return the same sequence of numbers, thus not really very random at all, but for this purpose it's random enough.
    :
    :
    :
    :
    [blue]Initialize [b]Seed[/b] with DOS GetTime function: 2CH, INT 21H. Get seconds and milliseconds, put them into [b]WORD[/b]...[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories