如何使用Assembly tasm随机化数字?我必须重新定位坐标的数字......我在基础水平上学习装配,但我知道有一种使用时钟的方法。我必须在第二次随机化6个数字,所以它不起作用.... 抱歉我的英文!
答案 0 :(得分:0)
我提出的是一种用于提取32位随机数(KISS()函数)的著名算法的实现;用于随机化的种子是_RndXSeed0,可以使用_RndXSeed0 = Round(Frac(Time)* 4294967295)的系统计时器进行设置。
.386p
IDEAL
MODEL FLAT
CODESEG
PUBLIC _RndXSeed0 DD 123456789 ; X: Seed
PUBLIC _RndXSeed1 DD 362436000 ; Y: Must be <>0
PUBLIC _RndXSeed2 DD 521288629 ; Z: Must be <>0
PUBLIC _RndXSeed3 DD 7654321 ; C: Must be <>0
PUBLIC rndx_
rndx_:
; Returns a 32 Bit random number
Push ESI
LEA ESI,_RndXSeed0
Mov EAX,[ESI]
Mov EDX,69069
Mul EDX
Add EAX,12345
Mov [ESI],EAX ; RndXSeed[0]=69069*RndXSeed[0]+12345;
Mov EAX,[ESI+4]
ShL EAX,13
XOr [ESI+4],EAX ; RndXSeed[1]=RndXSeed[1] XOr (RndXSeed[1] ShL 13);
Mov EAX,[ESI+4]
ShR EAX,17
XOr [ESI+4],EAX ; RndXSeed[1]=RndXSeed[1] XOr (RndXSeed[1] ShR 17);
Mov EAX,[ESI+4]
ShL EAX,5
XOr [ESI+4],EAX ; RndXSeed[1]=RndXSeed[1] XOr (RndXSeed[1] ShL 5);
Mov EAX,[ESI+8]
Mov EDX,698769069
Mul EDX
Add EAX,[ESI+12]
AdC EDX,0 ; EDX:EAX=698769069*RndXSeed[2]+RndXSeed[3];
Mov [ESI+12],EDX ; RndXSeed[3]=T ShR 32;
Cmp EAX,[ESI+8]
Mov EAX,0
SetE AL
Or EDX,EDX
SetE DL
And AL,DL ; EAX=CARDINAL(RndXSeed[2]=T)
Add EAX,[ESI]
Add EAX,[ESI+4] ; RndX=RndXSeed[0]+RndXSeed[1]+CARDINAL(RndXSeed[2]=T);
Pop ESI
Ret
;-----------------------------------------------------------------------------
END
.