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

发布于 2024-07-12 04:19:27 字数 84 浏览 6 评论 0原文

我对汇编非常陌生,我正在尝试创建一个简单的程序。 为此,我需要生成一个随机数。

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

I am 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏天碎花小短裙 2024-07-19 04:19:27

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

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

生成,

这些数字由x = (a * s + b) MOD m

其中 m、a 和 b 是为算法选取的。 使用了一些流行的这些值集。 如果你取 2 的幂,尤其是 32 位机器的 2^32,那就容易多了。 然后机器会自动完成 mod 步骤。

查看维基百科,他们有流行的 a、b 和 M 集以及更多信息。

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

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

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.

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

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

你是年少的欢喜 2024-07-19 04:19:27

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

显示显着提高了我以前写的旧汇编代码的速度。

I'm a big fan of R250, being much faster to execute than 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.

三生一梦 2024-07-19 04:19:27

查看此维基百科页面,选择一个算法并实现它。

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

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

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

音栖息无 2024-07-19 04:19:27

随机数

这是一个有点含糊的问题。

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

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文