查看單個文章
舊 2010-12-27, 10:15 PM   #1
avgx
註冊會員
榮譽勳章

勳章總數0
UID - 343354
在線等級: 級別:0 | 在線時長:1小時 | 升級還需:4小時
註冊日期: 2010-10-27
文章: 2
精華: 0
現金: 2 金幣
資產: 2 金幣
預設 masm615 組合語言 的 不重複亂數問題,附有程式碼

大家好~~
我所使用的組合語言軟體是 masm615

問題是,我想要用組合語言寫出不重複亂數,我有用C寫出來,但是組合不會寫
但是去 不知道錯誤在那裡,請高手們 幫幫忙寫出來,若可以的話,請順手注解意思

謝謝~~~

INCLUDE Irvine32.inc
.data
a DWORD 10 dup(0)

count dword 1
x dword 0
y dword 0

str1 byte "success",13,10,0

.code
main PROC
call clrscr

mov esi,offset a

call randomize
.while (x<10)
mov eax,10
call randomrange

mov esi,offset a
.while(count == 1)
mov y,0
.while(y<10)
.if([esi]==eax)
mov count,1
mov esi,offset a
mov eax,10
call randomrange
.else
add esi,4
mov count,0
.endif
add y,1
.endw ;while inside
.endw

mov [esi],eax
add esi,4

call writeint

inc x
.endw

exit
main ENDP
END main

我學習過 用 DEC C++ 寫 C程式
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define max 10

int HasAppear(int a[max],int);/*檢查已出現的數值*/
void ShowArray(int a[max]);/*顯示陣列*/

//===================================
int main(void)
{
int i,j;
int card[max]={0};

int temp;/*作為 暫存亂數 的 元素*/

srand(time(NULL));

for(i=0;i<max;i++)
{
temp=rand()%max + 1; /*產生亂數*/
while(HasAppear(card, temp) == 1)/*呼叫副程式比較是否曾經出現*/
{
temp=rand()%max +1;/*曾經出現,再重新產生亂數*/
}
card[i]=temp;/*暫存元素 存入 陣列內*/
}
ShowArray(card);

system("Pause");
return 0;
}

//======================================================
int HasAppear(int a[max], int temp)
{
int i,j;
for(i=0;i<max;i++)
{
if(a[i]==temp) return 1;/*跑陣列,01234這樣慢慢的比較整個回圈,相同的就回傳true*/
}
return 0;/*沒有相同的就 回傳 false*/

}
//=======================================================
void ShowArray(int a[])
{
int i,j;
for(i=0;i<max;i++)
{
printf("%2d", a[i]);
printf("\n");
}
}
avgx 目前離線  
送花文章: 0, 收花文章: 0 篇, 收花: 0 次
回覆時引用此帖