如何使用FASM编译器在汇编语言中生成随机数?

时间:2023-01-14 03:15:47

Hey guys, I'm really new to assembly and I'm trying to create a simple program. For this I need to generate a random number.

嘿伙计们,我对组装很新,我正在尝试创建一个简单的程序。为此,我需要生成一个随机数。

Anybody know how I can do this with the FASM compiler?

有人知道如何用FASM编译器做到这一点吗?

Thanks, Sam

4 个解决方案

#1


You could use a Linear-Congruential Algorithm. Its the most common psuedo-random number algorithm.

您可以使用线性同余算法。它是最常见的伪随机数算法。

Basically, you have a seed value. And then once you start generating random numbers each number becomes the seed for the new request.

基本上,你有种子价值。然后,一旦开始生成随机数,每个数字就成为新请求的种子。

The numbers are generated by

数字由...生成

x = (a * s + b) MOD m

x =(a * s + b)MOD m

Where m, a and b are picked for the algorithm. There are some popular sets of these values used. Its a lot easier if you make m a power of 2, especially 2^32 for 32 bit machines. Then the mod step is done automatically by the machine.

其中m,a和b被选择用于算法。使用了一些流行的这些值集。如果你的功率为2,特别是32位机器的2 ^ 32,那么它就容易多了。然后mod步骤由机器自动完成。

Check out the wikipedia, they have popular sets of a, b and M and a lot more information.

查看*,他们有流行的a,b和M组以及更多信息。

There are more complicated things can be done with seeds as well (setting the seed based on the current time, for instance)

种子也可以做更复杂的事情(例如根据当前时间设置种子)

#2


I'm a big fan of R250, being much faster to execute than LCG. http://www.ddj.com/184408549?pgno=7

我是R250的忠实粉丝,执行速度比LCG快得多。 http://www.ddj.com/184408549?pgno=7

Shows a significant increase in speed in the old assembly code I used to write back in the day.

显示我过去在当天回写的旧汇编代码中的速度显着提高。

#3


Take a look at this Wikipedia page, pick an algorithm, and implement it.

看一下这个Wikipedia页面,选择一个算法并实现它。

Edit: Or you could take the easy route. Use your OS's C runtime and call their rand functions.

编辑:或者您可以采取简单的路线。使用OS的C运行时并调用它们的rand函数。

#4


random number

This is a slightly ambiguous question.

这是一个有点模棱两可的问题。

Most of the posters so far are probably right; they're explaining how to generate a pseudo-random number and that's probably what you need. Seed the algorithm with the current time (you'll have to ask the OS for that, or read it from the clock chip). That'll give you "random" numbers that are good enough for games and other simple uses.

到目前为止,大多数海报都可能是正确的;他们正在解释如何生成伪随机数,这可能就是你所需要的。使用当前时间对算法进行种子处理(您必须向操作系统询问,或者从时钟芯片读取它)。这将给你“随机”数字,这些数字足以满足游戏和其他简单用途。

But please don't use those "random numbers" for any security application (encryption, key generation, etc). For security applications you need a really good cryptographically-secure random number generator. Writing one of those is really hard. (Netscape got it wrong so early versions of Netscape Navigator had an easily-hackable HTTPS implementation; Debian very recently got it wrong leading to loads of easily-hackable SSH and HTTPS/SSL keys).

但请不要将这些“随机数”用于任何安全应用程序(加密,密钥生成等)。对于安全应用程序,您需要一个非常好的加密安全随机数生成器。写其中一个真的很难。 (Netscape错了所以Netscape Navigator的早期版本有一个易于破解的HTTPS实现; Debian最近错误地导致了大量易于破解的SSH和HTTPS / SSL密钥)。

#1


You could use a Linear-Congruential Algorithm. Its the most common psuedo-random number algorithm.

您可以使用线性同余算法。它是最常见的伪随机数算法。

Basically, you have a seed value. And then once you start generating random numbers each number becomes the seed for the new request.

基本上,你有种子价值。然后,一旦开始生成随机数,每个数字就成为新请求的种子。

The numbers are generated by

数字由...生成

x = (a * s + b) MOD m

x =(a * s + b)MOD m

Where m, a and b are picked for the algorithm. There are some popular sets of these values used. Its a lot easier if you make m a power of 2, especially 2^32 for 32 bit machines. Then the mod step is done automatically by the machine.

其中m,a和b被选择用于算法。使用了一些流行的这些值集。如果你的功率为2,特别是32位机器的2 ^ 32,那么它就容易多了。然后mod步骤由机器自动完成。

Check out the wikipedia, they have popular sets of a, b and M and a lot more information.

查看*,他们有流行的a,b和M组以及更多信息。

There are more complicated things can be done with seeds as well (setting the seed based on the current time, for instance)

种子也可以做更复杂的事情(例如根据当前时间设置种子)

#2


I'm a big fan of R250, being much faster to execute than LCG. http://www.ddj.com/184408549?pgno=7

我是R250的忠实粉丝,执行速度比LCG快得多。 http://www.ddj.com/184408549?pgno=7

Shows a significant increase in speed in the old assembly code I used to write back in the day.

显示我过去在当天回写的旧汇编代码中的速度显着提高。

#3


Take a look at this Wikipedia page, pick an algorithm, and implement it.

看一下这个Wikipedia页面,选择一个算法并实现它。

Edit: Or you could take the easy route. Use your OS's C runtime and call their rand functions.

编辑:或者您可以采取简单的路线。使用OS的C运行时并调用它们的rand函数。

#4


random number

This is a slightly ambiguous question.

这是一个有点模棱两可的问题。

Most of the posters so far are probably right; they're explaining how to generate a pseudo-random number and that's probably what you need. Seed the algorithm with the current time (you'll have to ask the OS for that, or read it from the clock chip). That'll give you "random" numbers that are good enough for games and other simple uses.

到目前为止,大多数海报都可能是正确的;他们正在解释如何生成伪随机数,这可能就是你所需要的。使用当前时间对算法进行种子处理(您必须向操作系统询问,或者从时钟芯片读取它)。这将给你“随机”数字,这些数字足以满足游戏和其他简单用途。

But please don't use those "random numbers" for any security application (encryption, key generation, etc). For security applications you need a really good cryptographically-secure random number generator. Writing one of those is really hard. (Netscape got it wrong so early versions of Netscape Navigator had an easily-hackable HTTPS implementation; Debian very recently got it wrong leading to loads of easily-hackable SSH and HTTPS/SSL keys).

但请不要将这些“随机数”用于任何安全应用程序(加密,密钥生成等)。对于安全应用程序,您需要一个非常好的加密安全随机数生成器。写其中一个真的很难。 (Netscape错了所以Netscape Navigator的早期版本有一个易于破解的HTTPS实现; Debian最近错误地导致了大量易于破解的SSH和HTTPS / SSL密钥)。