生成 IA-32 Assembly

时间:2021-05-25 07:45:11

标签: assembly visual-c++ x86 permutation

我必须使用 Visual Studio 在 IA-32 程序集中创建一个 asm 块。
我曾尝试按字典顺序使用生成,但现在卡住了,因为我不知道如何继续。
我无法添加变量,也无法更改用 C 编写的部分。我只能创建 asm 块。

作为指南,我使用了维基百科页面,上面写着:

<块引用>

以下算法生成下一个排列 在给定的排列之后按字典顺序排列。它改变了给定的 就地排列。

  1. 找到最大的索引 k 使得 a[k] < a[k + 1]。如果没有这样的索引 存在,该排列是最后一个排列。
  2. 找出最大的 索引 l 大于 k 使得 a[k] < a[l]。
  3. 交换 a[k] 的值 与 a[l]。
  4. 将序列从 a[k + 1] 反转到 and 包括最后一个元素 a[n]。

例如,给定序列 [1, 2, 3, 4](按升序排列),并且假设索引是 从零开始,步骤如下:

  1. 索引k = 2,因为3放在满足条件的索引处 是仍然小于 a[k + 1] 的最大索引,即 4
  2. 索引 l = 3,因为为满足条件 a[k] < a[l],4 是序列中唯一大于 3 的值。
  3. 该 a[2] 和 a[3] 的值交换形成新序列 [1,2,4,3]。
  4. k-index a[2] 之后到最后一个元素的序列是 颠倒了。因为只有一个值位于该索引(3)之后,所以 在这种情况下,顺序保持不变。因此字典序 初始状态的后继被置换:[1,2,4,3]。

按照这个 算法,下一个字典排列将是 [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");
        }
    }
}

0 个答案:

没有答案
相关问题