📄 random.asm.htm
字号:
<font color=gray>001</font> <font color=green>;**************************************************</font><br><font color=gray>002</font> <font color=green>; 以下代码由“老罗代码着色器”0.2版进行着色</font><br><font color=gray>003</font> <font color=green>; 测试文件名:Random.asm</font><br><font color=gray>004</font> <font color=green>; 测试日期: 2002-12-29</font><br><font color=gray>005</font> <font color=green>;**************************************************</font><br><font color=gray>006</font> <br><font color=gray>007</font> <font color=green>;*********************************************************</font><br><font color=gray>008</font> <font color=green>;程序名称:随机数的产生原理与实现</font><br><font color=gray>009</font> <font color=green>;作者:罗聪</font><br><font color=gray>010</font> <font color=green>;日期:2002-11-21</font><br><font color=gray>011</font> <font color=green>;出处:http://www.LuoCong.com(老罗的缤纷天地)</font><br><font color=gray>012</font> <font color=green>;注意事项:如欲转载,请保持本程序的完整,并注明:</font><br><font color=gray>013</font> <font color=green>;转载自“老罗的缤纷天地”(http://www.LuoCong.com)</font><br><font color=gray>014</font> <font color=green>;*********************************************************</font><br><font color=gray>015</font> <br><font color=gray>016</font> <font color=red>.386</font><br><font color=gray>017</font> <font color=red>.model</font> <font color=red>flat</font>, <font color=red>stdcall</font><br><font color=gray>018</font> <font color=blue>option</font> <font color=red>casemap</font>:<font color=red>none</font><br><font color=gray>019</font> <br><font color=gray>020</font> <font color=blue>include</font> \masm32\<font color=blue>include</font>\windows.inc<br><font color=gray>021</font> <font color=blue>include</font> \masm32\<font color=blue>include</font>\kernel32.inc<br><font color=gray>022</font> <font color=blue>include</font> \masm32\<font color=blue>include</font>\user32.inc<br><font color=gray>023</font> <font color=blue>includelib</font> \masm32\<font color=red>lib</font>\kernel32.lib<br><font color=gray>024</font> <font color=blue>includelib</font> \masm32\<font color=red>lib</font>\user32.lib<br><font color=gray>025</font> <br><font color=gray>026</font> WndProc <font color=blue>proto</font> :<font color=red>DWORD</font>, :<font color=red>DWORD</font>, :<font color=red>DWORD</font>, :<font color=red>DWORD</font><br><font color=gray>027</font> iRand <font color=blue>proto</font> :<font color=red>DWORD</font>, :<font color=red>DWORD</font><br><font color=gray>028</font> <br><font color=gray>029</font> <font color=blue>.const</font><br><font color=gray>030</font> IDC_BUTTON_GENERATE <font color=red>equ</font> 3000<br><font color=gray>031</font> IDC_EDIT_FIRST <font color=red>equ</font> 3001<br><font color=gray>032</font> IDC_EDIT_SECOND <font color=red>equ</font> 3002<br><font color=gray>033</font> <br><font color=gray>034</font> <font color=blue>.data</font><br><font color=gray>035</font> szDlgName <font color=red>db</font> <font color=orange>"lc_dialog"</font>, 0<br><font color=gray>036</font> szCaption <font color=red>db</font> <font color=orange>"Rand Number Generator by LC"</font>, 0<br><font color=gray>037</font> szText <font color=red>db</font> 255 dup(0)<br><font color=gray>038</font> szTemplate <font color=red>db</font> <font color=orange>"(%d ~ %d)随机数:"</font>, 13, 10, 13, 10,\<br><font color=gray>039</font> <font color=orange>" %d"</font>, 13, 10, 13, 10,\<br><font color=gray>040</font> <font color=orange>"老罗的缤纷天地"</font>, 13, 10,\<br><font color=gray>041</font> <font color=orange>"http://www.LuoCong.com"</font>, 0<br><font color=gray>042</font> nFirst <font color=red>dd</font> 0<br><font color=gray>043</font> nSecond <font color=red>dd</font> 0<br><font color=gray>044</font> <br><font color=gray>045</font> <font color=blue>.code</font><br><font color=gray>046</font> main:<br><font color=gray>047</font> <font color=red>invoke</font> GetModuleHandle, NULL<br><font color=gray>048</font> <font color=red>invoke</font> DialogBoxParam, <font color=#808000>eax</font>, <font color=red>offset</font> szDlgName, 0, WndProc, 0<br><font color=gray>049</font> <font color=red>invoke</font> ExitProcess, <font color=#808000>eax</font><br><font color=gray>050</font> <br><font color=gray>051</font> WndProc <font color=red>proc</font> hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM<br><font color=gray>052</font> <font color=red>LOCAL</font> hEdit: HWND<br><font color=gray>053</font> <br><font color=gray>054</font> .if uMsg == WM_CLOSE<br><font color=gray>055</font> <font color=red>invoke</font> EndDialog, hWnd, 0<br><font color=gray>056</font> <br><font color=gray>057</font> .elseif uMsg == WM_COMMAND<br><font color=gray>058</font> <font color=blue>mov</font> <font color=#808000>eax</font>, wParam<br><font color=gray>059</font> <font color=blue>mov</font> <font color=#808000>edx</font>, <font color=#808000>eax</font><br><font color=gray>060</font> <font color=blue>shr</font> <font color=#808000>edx</font>, 16<br><font color=gray>061</font> <font color=blue>movzx</font> <font color=#808000>eax</font>, <font color=#808000>ax</font><br><font color=gray>062</font> .if <font color=#808000>edx</font> == BN_CLICKED<br><font color=gray>063</font> .if <font color=#808000>eax</font> == IDCANCEL<br><font color=gray>064</font> <font color=red>invoke</font> EndDialog, hWnd, NULL<br><font color=gray>065</font> .elseif <font color=#808000>eax</font> == IDC_BUTTON_GENERATE || <font color=#808000>eax</font> == IDOK<br><font color=gray>066</font> <font color=green>;获得上限:</font><br><font color=gray>067</font> <font color=red>invoke</font> GetDlgItemInt, hWnd, IDC_EDIT_FIRST, NULL, <font color=blue>TRUE</font><br><font color=gray>068</font> <font color=blue>mov</font> nFirst, <font color=#808000>eax</font><br><font color=gray>069</font> <font color=green>;获得下限:</font><br><font color=gray>070</font> <font color=red>invoke</font> GetDlgItemInt, hWnd, IDC_EDIT_SECOND, NULL, <font color=blue>TRUE</font><br><font color=gray>071</font> <font color=blue>mov</font> nSecond, <font color=#808000>eax</font><br><font color=gray>072</font> <font color=green>;产生随机数:</font><br><font color=gray>073</font> <font color=red>invoke</font> iRand, nFirst, nSecond<br><font color=gray>074</font> <font color=green>;输出:</font><br><font color=gray>075</font> <font color=red>invoke</font> wsprintf, <font color=red>addr</font> szText, <font color=red>addr</font> szTemplate, nFirst, nSecond, <font color=#808000>eax</font><br><font color=gray>076</font> <font color=red>invoke</font> MessageBox, hWnd, <font color=red>addr</font> szText, <font color=red>addr</font> szCaption, MB_OK <font color=blue>or</font> MB_ICONINFORMATION<br><font color=gray>077</font> .endif<br><font color=gray>078</font> .endif<br><font color=gray>079</font> .else<br><font color=gray>080</font> <font color=blue>mov</font> <font color=#808000>eax</font>, <font color=blue>FALSE</font><br><font color=gray>081</font> <font color=blue>ret</font><br><font color=gray>082</font> .endif<br><font color=gray>083</font> <font color=blue>mov</font> <font color=#808000>eax</font>, <font color=blue>TRUE</font><br><font color=gray>084</font> <font color=blue>ret</font><br><font color=gray>085</font> WndProc <font color=red>endp</font><br><font color=gray>086</font> <br><font color=gray>087</font> <font color=green>;**********************************************************************</font><br><font color=gray>088</font> <font color=green>; 函数功能:产生范围从 first 到 second 的随机数</font><br><font color=gray>089</font> <font color=green>; 传入参数:</font><br><font color=gray>090</font> <font color=green>; first = 下限</font><br><font color=gray>091</font> <font color=green>; second = 上限</font><br><font color=gray>092</font> <font color=green>; 返回参数:</font><br><font color=gray>093</font> <font color=green>; eax = Rand_Number</font><br><font color=gray>094</font> <font color=green>; 所用公式:</font><br><font color=gray>095</font> <font color=green>; Rand_Number = (Rand_Seed * X + Y) mod Z</font><br><font color=gray>096</font> <font color=green>; 补充说明:</font><br><font color=gray>097</font> <font color=green>; (1)本例中用 GetTickCount 来取得随机数种子,</font><br><font color=gray>098</font> <font color=green>; 在实际应用中,可用别的方法代替。</font><br><font color=gray>099</font> <font color=green>; (2)要产生随机数,X和Y其中之一必须是素数,</font><br><font color=gray>100</font> <font color=green>; 所以 X = 23, Y = 7(可用别的素数代替)</font><br><font color=gray>101</font> <font color=green>;**********************************************************************</font><br><font color=gray>102</font> iRand <font color=red>proc</font> <font color=red>uses</font> <font color=#808000>ecx</font> <font color=#808000>edx</font> first:<font color=red>DWORD</font>, second:<font color=red>DWORD</font><br><font color=gray>103</font> <font color=red>invoke</font> GetTickCount <font color=green>; 取得随机数种子,当然,可用别的方法代替</font><br><font color=gray>104</font> <font color=blue>mov</font> <font color=#808000>ecx</font>, 23 <font color=green>; X = ecx = 23</font><br><font color=gray>105</font> <font color=blue>mul</font> <font color=#808000>ecx</font> <font color=green>; eax = eax * X</font><br><font color=gray>106</font> <font color=blue>add</font> <font color=#808000>eax</font>, 7 <font color=green>; eax = eax + Y (Y = 7)</font><br><font color=gray>107</font> <font color=blue>mov</font> <font color=#808000>ecx</font>, second <font color=green>; ecx = 上限</font><br><font color=gray>108</font> <font color=blue>sub</font> <font color=#808000>ecx</font>, first <font color=green>; ecx = 上限 - 下限</font><br><font color=gray>109</font> <font color=blue>inc</font> <font color=#808000>ecx</font> <font color=green>; Z = ecx + 1 (得到了范围)</font><br><font color=gray>110</font> <font color=blue>xor</font> <font color=#808000>edx</font>, <font color=#808000>edx</font> <font color=green>; edx = 0</font><br><font color=gray>111</font> <font color=blue>div</font> <font color=#808000>ecx</font> <font color=green>; eax = eax mod Z (余数在edx里面)</font><br><font color=gray>112</font> <font color=blue>add</font> <font color=#808000>edx</font>, first <font color=green>; 修正产生的随机数的范围</font><br><font color=gray>113</font> <font color=blue>mov</font> <font color=#808000>eax</font>, <font color=#808000>edx</font> <font color=green>; eax = Rand_Number</font><br><font color=gray>114</font> <font color=blue>ret</font><br><font color=gray>115</font> iRand <font color=red>endp</font><br><font color=gray>116</font> <br><font color=gray>117</font> <font color=blue>end</font> main<br><font color=gray>118</font> <font color=green>;******************** over ********************</font><br><font color=gray>119</font> <font color=green>;by LC</font>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -