1

下面是我试图转换成 armv8 的 java 代码。他的 armv8 代码是我的工作。它似乎不能正常工作,因为它目前使用 1 到 13 的数字,而数独应该只使用数字 1-9。

我有一个静态表,它用 1 到 9 的随机数和一些零初始化。当程序正在玩游戏时,应该替换这些零。

private boolean isAllowed(int row, int col,int number)
{
    return !(containsInRow(row, number) || containsInCol(col, number) || containsInBox(row, col, number));
}

public boolean solveSudoku()
{
    for(int row=0;row<9;row++)
    {
        for(int col=0;col<9;col++)
        {
            if(sudoku[row][col]==UNASSIGNED)
            {
                for(int number=1;number<=9;number++)
                {
                    if(isAllowed(row, col, number))
                    {
                        sudoku[row][col] = number;
                        if(solveSudoku())
                        {
                            return true;
                        }
                        else
                        {
                            sudoku[row][col] = UNASSIGNED;
                        }
                    }
                }
                return false;
            }
        }
    }
    return true;
}

我创建的汇编代码版本:

solveSudoku:
    stp x29,x30,[sp,-32]!
    add x29,sp,0

         mov     w19,0      //w19=Register for rows
        mov     w20,0       //w20=Register for col
    mov     w24,1       //w24=register for numbers

solve:
        adrp    x0,tab
        add     x0,x0,:lo12:tab

        mov     w27,9       

         and     w22,w22,wzr    //set 0 to w22
        mul     w22,w27,w19 
        add     w22,w22,w20 
        lsl     w22,w22,2   //calculate the table position

        ldr     w23,[x0,x22]    //w23=number in specific position in the table(position calculate above in w22)
        cbnz    w23,.incCol //if is not zero go to label .incCol to increase the j

//        mov     w24,1

nums:
        mov     w2,w19      
        mov     w6,w24
        bl      containsInRow   //check if number contains in a row
        mov     w25,w7
        mov     w2,w20
        mov     w6,w24
        bl      containsInCol   //check if number contains in a col
    mov w26,w7
    mov w2,w19
    mov w3,w20
    mov w4,w24
    bl  containsInBox   //check if number contains in a box
     mov     w28,w5
        mov     w1,w25
        mov     w2,w26
        mov     w3,w28
        bl      isAllowed   //if number is allow to be place in specific position
        mov     w28,w5
        cbz     w28,.next   //if is not allow go to next number


          and     w25,w25,wzr
        mul     w25,w27,w19
        add     w25,w25,w20
        lsl     w25,w25,2

        str     w24,[x0,x25]    //store number from register w24 in position calculate above
        bl      solveSudoku
    cbnz    w21,.end    //if is not zero move to check register w21=1 and return
        str wzr,[x0,x25]    //if is zero store zero to the position had changed above
    b   .next       
.next:
        add     w24,w24,1   //increase number
        cmp     w24,10      //if number is 10 set register w21 to 0 and return
        b.eq    .again
        b       nums        //go back to check next number
.incCol:
        add     w20,w20,1   //increase col
        cmp     w20,9       //if col is 9 go to increase rows
        b.eq    .incLine
        b.ne    solve       //if is not 9 go back to check next column

.incLine:
        sub     w20,w20,9   //set col back to 0
        add     w19,w19,1   //increase row
        cmp     w19,9       //if row is 9 set register w21 to 1 and return
        b.eq   .end
        b.ne    solve       //if is not 9 go back to check next row

.again: 
        mov w21,0       //return false
    ldp x29,x30,[sp],32
    ret

.end:
    mov w21,1       //return true
    ldp x29,x30,[sp],32
    ret

数独的结果:

在此处输入图像描述

4

0 回答 0