News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Pseudo Random Numbers

Started by tda0626, June 04, 2024, 09:06:52 AM

Previous topic - Next topic

NoCforMe

Right, good point.
A lot of times I use LOOP even in Win32 programming where various functions trash ECX.
Really easy fix for that, and something I do a lot:
       MOV     ECX, loopCount
lptop: PUSH    ECX
       INVOKE  <some Win32 function that trashes ECX>
       INVOKE  <yet another Win32 function)
      . . . .
       POP     ECX
       LOOP    lptop
Assembly language programming should be fun. That's why I do it.

zedd151

That was from experience, btw. Not recently though, was years ago.
Windows 10 'convert' now, but windows xp was the best!   :skrewy:

NoCforMe

You mean you screwed up and changed CX inside a loop?
Tsk, tsk.
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: NoCforMe on June 05, 2024, 12:51:41 PMYou mean you screwed up and changed CX inside a loop?
No it was ecx + was my first time using loop instruction.
Windows 10 'convert' now, but windows xp was the best!   :skrewy:

daydreamer

Quote from: FORTRANS on June 05, 2024, 08:02:57 AM32-bit code works perfectly well in real mode.  Not
sure about specific instructions though.  But the general
purpose 32-bit registers can be useful with the normal
math and logical instructions.  And it can be fun to muck
about with them to see if you can write useful code.  The
32-bit addressing is normally limited to a 64k range, but
can also be used.
Besides 32 bit dos extender,there is a load instruction to load both 64k part and segment reg from 1 meg adress
You could use some of upper 16 bit half too eax, bitshift and Mov ds,ax
I found it convenient that 320 evenly divided by 16 when changing segment reg to "scroll" vertical

Old optimisation tutorials on bigger registers when copy /fill memory = faster work also in dosbox, try rep Movsd and rep stosd instead of Movsd and stosb



my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

tda0626

Quote from: NoCforMe on June 05, 2024, 11:38:36 AMAbout your loops:
You might want to use the LOOP instruction.
When you know exactly how many iterations you want, it makes things much easier.
You set (E)CX to the loop count.
At the bottom of the loop you code the LOOP instruction with the jump target of the loop.
LOOP automagically decrements (E)CX, compares it to zero, jumps to the loop target if not.
Saves you a little bit of bookkeeping.
    MOV  CX, loopCount
lp: ; do stuff
    ; do more stuff
    LOOP  lp
Only "gotcha" is that it's only good for short distance loops (max. 127 bytes of code).

There are even 2 more flavors of this instruction, LOOPE/Z and LOOPNE/Z that will loop while equal (zero flag set) or not equal, from an operation that sets that flag before the LOOP instruction.

The 8086/8088 was an awesome little machine.


Thanks. I will see about using that in my code going on. How can you tell if you are over the 127 byte limit?


I reprogrammed my RNG and got it to work. I used a XORShift LFSR using the triplet 7,9,8. It was the only triplet I could find that was for 16 bits. Most of the documentation on the subject was directed towards 32 and 64 bit triplets. Supposedly, this triplet combination has a period of 65,535. I ran it through some randomness tests and it seems to work pretty good from what I can tell. Did a random line test by drawing 500 lines and I can't see any patterns emerging, see picture below. I don't know, what do you guys think? Check out the code below...





.data
mainseed    DW    0
rand    DW    0
.code
RNDNumber proc modulus:word, seed:word
       
        mov ax, seed      ; seed =  seed XOR ( seed << 7 )
        shl ax, 7
        xor ax, seed
        mov seed,  ax   
       
        mov ax, seed      ; seed = seed XOR ( seed >> 9 )
        shr ax, 9
        xor ax, seed
        mov seed, ax     
       
        mov ax, seed      ; mainseed =  seed XOR ( seed << 8 )
        shl ax, 8
        xor ax, seed
        mov mainseed, ax  ; use mainseed to seed the next call to RNDNumber
       
        xor dx, dx
        div modulus
        mov rand, dx      ; Store remainder
       
        ret
RNDNumber endp       

NoCforMe

Quote from: tda0626 on June 06, 2024, 08:25:05 AMHow can you tell if you are over the 127 byte limit?
The assembler will let you know:
QuoteJump destination too far by XX bytes

Easy fix, though: instead of
        MOV   CX, loop count
lptop:    . . . .
          . . . .
        LOOP  lptop
do this:
        MOV   CX, loop count
lptop:    . . . .
          . . . .
        DEC   CX
        JNZ   lptop
which will work for longer distances.
Assembly language programming should be fun. That's why I do it.

tda0626