flat assembler
Message board for the users of flat assembler.

Index > Main > Small random integer generator

Author
Thread Post new topic Reply to topic
r22



Joined: 27 Dec 2004
Posts: 805
r22 15 Feb 2005, 04:31
I was messing around with random numbers and made this little algorithm. Its small and produces very good random streams. It can of course, always be improved.
Code:
.code
;;;randomizes or uses default seed
;;;if randomize not called, same sequence of randomness
;;;this just makes a 64bit seed
Randomize:
 .resv equ ebp-8
push ebp
mov ebp,esp
sub esp,8
  call [GetTickCount]
  xchg al,ah
  mov word[.resv],ax
  adc al,ah
  adc ah,al
  mov word[.resv+2],ax
  call [GetTickCount]
  neg ax
  mov word[.resv+4],ax
  sub ah,al
  adc al,ah
  mov word[.resv+6],ax
  movq mm0,qword[.resv]
  movq qword[seed],mm0
add esp,8
mov esp,ebp
pop ebp
retn 0

RandInt:
.resv equ ebp-8
.high equ ebp+8
push ebp
mov ebp,esp
sub esp,8
push edx
push ebx
xor edx,edx
  movq mm0, qword[seed]
  movq qword[.resv],mm0
  mov ecx,dword[.resv+4] ;;2nd half of seed
  mov eax,dword[.resv]   ;;1st half of seed
  shld eax,ecx,1         ;;shift in 1 bit from 2nd half to first
  mov dword[.resv],eax   ;;update seed in local memory
  adc ecx,0              ;;add the carry flag
  ror ecx,3              ;;odd roll-right
  bswap ecx              ;;make more random
  mov dword[.resv+4],ecx ;;update 2nd half of seed in local memory
  movq mm0, qword[.resv]
  movq qword[seed],mm0
  mov ecx,dword[.high]   ;;process high
  cmp ecx,-1
  je near .rdone         ;;if high = FFFFFFFFh retn rnd dword
  test ecx,ecx
  jz  near .rdone         ;;if high = 0 retn rnd dword
  inc ecx
  div ecx
  xchg eax,edx
.rdone:
pop ebx
pop edx
add esp,8
mov esp,ebp
pop ebp
retn 4

.data
;64bit seed default
seed db 0aah,011h,00h,0bbh,0ffh,22h,0ddh,77h
    
Post 15 Feb 2005, 04:31
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 15 Feb 2005, 14:22
i can competite with this Smile
Code:
org 256

call set320x200x256
call initrandomz


mainloop:

mov ecx,320
call randomz3
mov bx,ax

mov ecx,200
call randomz3
mov cx,ax

push ecx
mov ecx,256
call randomz3
pop ecx

call putpixel320x200x256 ; al=color bx=x cx=y

push eax
call bkeycheck
cmp al,27
pop eax
jnz mainloop

call set80x25t

int 20h

initrandomz: ; modifies edx, eax
rdtsc
ret


randomz3: ; Z3 ecx=range transparent
push ebx  ; number returned is:  0 <= n < ecx
push edx
mov ebx,eax
rdtsc

mov dx,cx

mov cl,al
xor cl,ah
ror ch,4
xor bl,ch
and cx,$f
rol bx,cl
btc bx,cx
xor bx,ax
ror eax,16
xor bx,ax
ror eax,16
mov cl,al
xor cl,ah
ror ch,4
xor bl,ch
and cx,$f
ror bx,cl
btc bx,cx

mov cl,bl
.minorloop:
rol eax,cl
and cx,$f
btc ax,cx
loopw .minorloop
mov cl,bh
.majorloop:
ror eax,cl
and cl,$f
btc ax,cx
loopw .majorloop
mov cx,dx

mul ecx
mov eax,edx
pop edx
pop ebx
ret

putpixel320x200x256: ; al=color, bx=x, cx=y
        push    es
        push    $A000
        pop     es
        mov     di,cx   ; y
        shl     cx,2
        add     di,cx   ; 5y
        shl     di,6    ; 320y
        add     di,bx
        stosb
        pop     es
        ret

bkeycheck:  ;returns: AH = BIOS scan code AL = ASCII character note: enhanced
mov ah,0x11 ;Return: ZF set if no keystroke available, ZF clear if keystroke available
int 0x16    ;only checks buffer without removing key
ret

set320x200x256:
mov ax,$13
int $10
ret

set80x25t:
mov ax,$03
int $10
ret
    
Post 15 Feb 2005, 14:22
View user's profile Send private message Visit poster's website Reply with quote
asmdemon



Joined: 18 Jan 2004
Posts: 97
Location: Virginia Beach, VA
asmdemon 15 Feb 2005, 15:40
well, check this out... 29 bytes without the grayscale pallet, 40 if you want grayscale...
Code:
$=0x0100
use16
;320*200*256
mov al, 0x13
int 0x10

;setup es
les cx, [bx]

;set grayscale
;xor ax, ax
;mov dx, 0x03c9
;setcolor:
;out dx, al
;out dx, al
;out dx, al
;inc ax
;jnz setcolor


next:
in al, 0x40
stosb
shl ax, 8
in al, 0x40
imul ax, 133
mov cx, ax
pausenow:
loop pausenow
mov ah, 1
int 0x16
jz next
ret
    
Post 15 Feb 2005, 15:40
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 15 Feb 2005, 15:55
well its small ... Smile
Post 15 Feb 2005, 15:55
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.