random numbers in 16-bit DOS, no 32-bit allowed. VGA graphics

Started by OceanJeff32, November 03, 2005, 06:25:17 AM

Previous topic - Next topic

OceanJeff32

Yes, I am working on something completely different!

Ok, I am in DOS 16-bit mode here, and trying to convert some 32-bit random code, to work in this pixel drawing function for VGA mode X (320x240, 256-color)

If you can help, that's cool.

If you can come up with a 16-bit DOS random function, that's awesome!

Later guys, the file is attached, ASM COM and the comments in the ASM show how to assemble and link with 16-bit linker.

jeff c


[attachment deleted by admin]
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

MichaelW

If you intend to use the nrandom code you should use the newer fixed version.

For a Park-Miller "Minimal Standard" linear congruential generator using Schrage's Method to avoid arithmetic overflow in the intermediate values:
a = 16807
m = 2147483647
q = m / a = 127773
r = m mod a = 2836
k = n0 / q
n1 = a * (n0 - k * q) - r * k

For 16-bit values Park and Miller recommended:
a = 171
m = 30269
So using Schrage's Method to avoid overflows in the intermediate values:
q = m / a = 177
r = m mod a = 2
k = n0 / q
n1 = a * (n0 - k * q) - r * k

Using this I think the nrandom code could be translated to 16-bit code very easily.

For positive values only, I suspect that this might be better than the method used in the fixed nrandom:
if n1 < 0
  n1 = n1 - m
endif

I just remembered a shorter Park-Miller generator that was posted by Abel. I could not find the post (it may have been on the old forum), but here is a version of it I used in some tests:

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; This proc is the shorter Park-Miller implementation that
; was posted by Abel.
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
.data
    ALIGN 4
    abel_nrandom_seed dd RAND_SEED
.code
ALIGN 4
abel_nrandom proc base:DWORD
    mov   eax, abel_nrandom_seed
    mov   ecx, 16807
    mul   ecx
    mov   ecx, 7fffffffh
    div   ecx
    mov   eax, edx
    mov   abel_nrandom_seed, eax ;always>0 and <7fffffff
    mov   edx, 0
    div   base
    mov   eax, edx
    ret
abel_nrandom endp


I'm not sure how this would translate to 16-bit code.
eschew obfuscation

OceanJeff32

Hey, cool, I figured out one thing wrong about my program (...one thing???)  anyways, I was calculating a random value for X, but in the wrong place.

Ok, I fixed the new code, and it works better, but I still don't know what the heck is going on?  It's supposed to have only one X value random, the Y value changes with the loop (CX).

Anyways, if anyone looks at it, have fun!  I know this, I am learning a lot about assembly language at this point...mistakes galore!

later,

jeff c
:U

[attachment deleted by admin]
Any good programmer knows, every large and/or small job, is equally large, to the programmer!