Well i just started in assembly and i came across a problem, random numbers and string parsing. I have no idea on how to do either, I did get the random number prog from Iczelion's site but it didn't help much, i can't tell how it gets the random number or where it stores it in the end. For string parsing i kinda got an idea from Iczelion's http downloader program, where it parses the http header and stuff, but i am still really confused. If you think you can help or point me in the right direction please do.
Posted on 2001-10-23 08:11:05 by Crackpipe2k
There are only pseudo-random number generators. Real random numbers come from external devices such as lava lamps. Psedo-random number generators take a seed and mess around with it to have different numbers. There are several well-working pseudo-random number generators, but you could easily make your own by messing with GetTickCount(). A good generator will give every number an equal chance of showing up. Take a look here: http://www.helsbreth.org/random/rng_combo.html. Its a good and small pseudo-random number generator I have found some time ago.
Posted on 2001-10-23 08:41:41 by comrade
I probally should have put in the psuedo in there yes i do know that, I probally should have been a little more specific. Thanks for the link my only prob with it is how would i limit my psuedo random number to a range ?

The psuedo random number generator i downloaded from Iczelion's site that i downloaded works fine except i don't under stand where it stores the number after it is created. It also uses any seed i want and it recommended to use GetTickCount or something the example source was sorta helpfull but they have a little dice generator,and it uses the following to get the number

...
Invoke GetTickCount
Invoke Randomize,eax
Invoke Rand,6
ADD al,49
MOV Num,al
...

now below so someone can help me under stand it .. is the rand.inc which has the randomize and rand stuff in it.

Rand proto :DWORD ;Generates a number on [0,Max-1]
Randomize proto :DWORD ;Changes the seed for Rand

.const
N equ 624
M equ 397
MATRIX_A equ 9908b0dfh
TEMPERING_MASK_B equ 9d2c5680h
TEMPERING_MASK_C equ 0efc60000h
UM equ 80000000h
LM equ 7fffffffh

.data
mti dd (N+1)
MC dd 69069

.data?
mt dd 2496 dup(?)

.code
Randomize proc Seed:DWORD
lea edi,mt
mov eax,Seed
mov ,eax
mov mti,1
@R: add edi,4
mul MC
mov ,eax
inc mti
cmp mti,N
jnz @R
ret
Randomize endp

Rand proc Max:DWORD
LOCAL Counter:DWORD
lea edi,mt
.IF mti>=N
.IF mti==N+1
INVOKE Randomize,4357
.ENDIF
mov Counter,0
mov esi,edi
@S: mov eax,
and eax,UM
mov ebx,
and ebx,LM
or eax,ebx
mov ecx,eax
shr eax,1
mov edx,esi
add edx,(M*4)
mov ebx,
xor eax,ebx
and ecx,1
.IF ecx
xor eax,MATRIX_A
.ENDIF
mov ,eax
add esi,4
inc Counter
cmp Counter,(N-M)
jnz @S

@T: mov eax,
and eax,UM
mov ebx,
and ebx,LM
or eax,ebx
mov ecx,eax
shr eax,1
mov edx,esi
add edx,((M-N)*4)
mov ebx,
xor eax,ebx
and ecx,1
.IF ecx
xor eax,MATRIX_A
.ENDIF
mov ,eax
add esi,4
inc Counter
cmp Counter,(N-1)
jnz @T

mov edx,edi
add edx,(M-1)*4
mov ebx,
xor eax,ebx
and ecx,1
.IF ecx
xor eax,MATRIX_A
.ENDIF
mov ,eax
mov mti,0
.ENDIF
mov esi,edi
mov eax,mti
inc mti
shl eax,2
add esi,eax
mov eax,
mov ebx,eax
shr eax,11
xor ebx,eax
mov eax,ebx
shl eax,7
and eax,TEMPERING_MASK_B
xor ebx,eax
mov eax,ebx
shl eax,15
and eax,TEMPERING_MASK_C
xor ebx,eax
mov eax,ebx
shr eax,18
xor eax,ebx
xor edx,edx
div Max
mov eax,edx
ret
Rand endp




What i can't undestand is if i called rand with a range larger than 10 where would it go ?
i tried just changing the 6 to a 1000000000 but when i did the add al,49 and the mov num,al
i end up getting characters and things ... How would i be able to get any lengthed psuedo random number.

as for the string parsing i'm still completely lost, any help at all with that would be so very kind.
Posted on 2001-10-23 09:07:20 by Crackpipe2k
Crackpipe2k:

to get a random number within a range, follow this very basic method:

begin:
- generate random number
- divide random number by upper limit (use div or idiv)
- get the remainder that will be sitting in edx
- check that the remainder is above the lower limit
- if the remainder is lower than the lower limit, jump back to begin: to generate another

in the random number generator function i wrote, i pass two parameters to it: the lower bound, and the upper bound. Once in the function, I load up the seed which i keep in a global variable, then multiply it against the return value from GetTickCount to get the random number. (The seed is generated by a call to GetTickCount when the app starts up). I then put the random number through the little algorithm above. Every time i do a full 32bit*32bit multiply in this function, i store the result back in the seed again to be used with the next number generated. Very simple, and it ends up very random.

As for string parsing, maybe you should post a sample string, so we can advise the best method :)
Posted on 2001-10-23 22:48:56 by sluggy
Sluggy, a better method of limiting the random number to a range...
if you have low,high as in (5, 10), I'd do the following:

subtract low from high - this gives you the "range" of random numbers.
Get a random number. Divide by range and get remainder (modulus).
Add lower limit.

This saves you from having the loop, and can end up a good
deal faster :).
Posted on 2001-10-24 01:06:21 by f0dder

subtract low from high - this gives you the "range" of random numbers.
Get a random number. Divide by range and get remainder (modulus).
Add lower limit.



Sweet, i'll put that in my generator :)
I actually wrote it from scratch when i started learning asm, there were plenty of generators out there, but they were either for segmented memory model, or they had no description of what they were doing, so i did my own, best way to learn :)
Posted on 2001-10-24 03:24:15 by sluggy
well the random number thing is no problem now, for the string parsing, here is what i will be using

4d30+2

or any other math operation other than adding. Now the first number won't always be a single digit, and same goes for the second. The math operation will be any of the following + - * /
and the last number will also vary in size. I need to seperate each from a string like that. Thanks again sluggy, and f0dder.
Posted on 2001-10-24 07:33:45 by Crackpipe2k
This sounds like homework to me, crackpipe ;). Separating the tokens
isn't all that hard... set up a register to point to your string.
Set "old position", the enter a loop where you increase current
position and get a byte from there. If it's a 0, you're done. If it's one
of +-/*, you have a token (from old position to current position),
and you update "old position" to current position + 1. You of course
also need to store the operator char.
Posted on 2001-10-24 08:02:29 by f0dder