我必须使用 Visual Studio 在 IA-32 程序集中创建一个 asm 块。
我曾尝试按字典顺序使用生成,但现在卡住了,因为我不知道如何继续。
我无法添加变量,也无法更改用 C 编写的部分。我只能创建 asm 块。
作为指南,我使用了维基百科页面,上面写着:
<块引用>以下算法生成下一个排列 在给定的排列之后按字典顺序排列。它改变了给定的 就地排列。
例如,给定序列 [1, 2, 3, 4](按升序排列),并且假设索引是 从零开始,步骤如下:
按照这个 算法,下一个字典排列将是 [1,3,2,4],和 第 24 次排列将是 [4,3,2,1],此时 a[k] < a[k + 1] 不存在,说明这是最后一次排列。
/********************************************************************************
Descrizione: Generate all permutations of the first N natural numbers.
The permutations generated must be inserted within a single
array of integers. For example, if N = 3, the array must contain:
{1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1}.
********************************************************************************/
#include <stdio.h>
void main()
{
// Variabiles
int N=4; // number of integers (<=6)
int Perm[4326]; // array permutations: size is sufficient for N <= 6
int Num = 0; // At the end it must contain the number of permutations generated
//asm block
__asm
{
MOV EAX, N
XOR EBX, EBX //K+1
MOV ECX, 1 //Counter K
MOV EDX, N //L
XOR ESI, ESI //Permutation Counter
XOR EDI, EDI
Inizializzazione : //Set the first N numbers of the array in order
MOV Perm[ECX*4-4], ECX
INC ECX
CMP ECX, EAX
JLE Inizializzazione
INC ESI
MakeOffset: //Use MUL to set the offset index in EDI
MOV EAX, 1
MUL ESI
MOV EDI, EAX
PermStore:
INC EDI
MOV ECX, Perm[EDI*4-4] //a[K+1]
DEC EDI
MOV EBX, Perm[EDI*4-4] //a[K]
CMP EBX, ECX
JL Swap
DEC EDI
JNS PermStore
DEC EAX
JS End
MOV EDI, EAX
DEC EDI
JMP PermStore
Swap:
DEC EDX
MOV ECX, Perm[EDX*4-4] //a[L]
CMP ECX, EBX
JG Greater
Greater:
//I have to use ECX (which is L) and EBX (a[k]), I have to use EDI for K and EAX for the offset moltiplications of L
//Now I take a[k] and store it in position L and then I store the one which was in L in position K
Reverse: //Reverse the sequence from a[k + 1] up to and including the final element a[n]
End:
}
// Stampa su video
{
int i,j,k;
printf("Permutazioni dei primi %d numeri naturali\n",N);
for (i=k=0;i<Num;i++)
{
for (j=0;j<N;j++)
{
printf("%3d",Perm[k++]);
}
printf("\n");
}
}
}