0

I am trying to solve the N queens problem in arm assembly. I have the code in C and need to convert it into arm assembly. I have most of it, but am a little confused on the recursion portion. I know I need to push to the stack but am lost on when to pop the stack.

I have the following code:

__main  
PROC        
mov r9,#8       
    mov r8, #0 
    mov r7, #1
    lsl r7,r7,#8
    sub r7, #1 
    mov r3, #0 
    mov r4, #0
    mov r5, #0 
    mov r6, #0 

    BL  construct

construct 
            mov r10,#0 
    mov r1,#0 
    cmp r3, r9
    beq addone

    b constructelse


constructelse 

  orr r11,r4,r5
  orr r11,r11,r6
  mvn r11,r11
  and r10,r11,r7 
   b loop




loop 

   cmp r10, #0
   beq exit

   neg r12, r10 

   and r1, r12, r10 

   eor r10, r10, r1

   add r3, #1

   orr r4, r4, r1
   lsl r4, r4, #1

   orr r5, r1

   orr r6, r6, r1
   lsr r6, r6, #1

   push {r1}
   push {r2}
   push {r3}
   push {r4}
   push {r5}
   push {r6}
   push {r10}
   push {r11}
   push {r12}

   b construct

addone 
   add r8, #1
   b exit


exit            

    ENDP
    END

I should be able to see how many solutions there are for the n queens problem and save the amount to a reference. Sorry about code formatting it is my first time posting here.

#include <stdio.h>

int  SIZE, MASK, COUNT;

void Backtrack(int y, int left, int down, int right)
{
    int  bitmap, bit;

    if (y == SIZE) {
        COUNT++;
    } else {
        bitmap = MASK & ~(left | down | right);
        while (bitmap != 0) {
            bit = -bitmap & bitmap;
            bitmap ^= bit;
            Backtrack(y+1, (left | bit)<<1, down | bit, (right | bit)>>1);
        }
    }
}

int main(void)
{
    SIZE = 8;   /*  <- N  */
    COUNT = 0;   /* result */

    MASK = (1 << SIZE) - 1;
    Backtrack(0, 0, 0, 0);

    return 0;
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

0 Answers0