random - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

  • jeffleydajeffleyda Posts: 390Member


    :
    : 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.



  • AsmGuru62AsmGuru62 Posts: 6,519Member
    :
    :
    : :
    : : 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.