User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Assembly section within the Software Development category of DaniWeb, a massive community of 427,222 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,280 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Assembly advertiser: Programming Forums
Views: 616 | Replies: 3
Reply
Join Date: Jul 2008
Posts: 4
Reputation: fmartins is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fmartins fmartins is offline Offline
Newbie Poster

N-queens problem - solution in nasm

  #1  
Jul 21st, 2008
I'm learning assembly and just finished coding the n-queens problem in nasm.

The n-queens problems consists of placing n queens on a nxn chessboard so that no two queens are in the same row, column or diagonal (so that no queen could take another). For years, this is the first program I write in any new language I learn.

If there is interest in the code, I can post it here.
There are 10 kinds of people: those that understand binary and those that don't.
L. Felipe Martins
Department of Mathematics
Cleveland State University
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Posts: 97
Reputation: Evenbit is on a distinguished road 
Rep Power: 4
Solved Threads: 2
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster in Training

Re: N-queens problem - solution in nasm

  #2  
Jul 25th, 2008
Yes. Please post the code. I would like to see it.

Nathan.
while (CPU is present) {some assembly required}
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: fmartins is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fmartins fmartins is offline Offline
Newbie Poster

Re: N-queens problem - solution in nasm

  #3  
Jul 25th, 2008
Here is the code:

  1. ; queens.asm
  2. ;
  3. ; The n queens problem, in nasm.
  4. ; Solves the n-queens problem by a depth-first tree search.
  5. ; Board symmetries are not considered.
  6. ;
  7. ; Output format:
  8. ; Solutions are printed as a sequence of n integers. Each integer
  9. ; gives the position of a queen in a column. For example, for n=8,
  10. ; the output line
  11. ; 1 5 8 6 3 7 2 4
  12. ; means that queens are placed (using standard chess notation) on
  13. ; squares a1, b5, c8, d6, e3, f7, g2 and h4.
  14. ;
  15. ; Notes:
  16. ; The code assumes that n is at most 20. To increase this limit, change
  17. ; the value specified for the constant NMAX and rebuild. The code is
  18. ; naive, so don't expect fast results for even moderately large n.
  19. ;
  20. ; To assemble/link in windows under Cygwin:
  21. ; nasm -o queens.o -f win32 queens.asm
  22. ; gcc -o queens queens.o
  23. ;
  24. ; Not tested in other platforms. If you test the code in other
  25. ; platforms, or have comments or suggestions, please contact the
  26. ; author.
  27. ;
  28. ; Author: L. Felipe Martins
  29. ; luizfelipe.martins@gmail.com
  30.  
  31. section .data
  32. NMAX equ 20 ; Maximum board size
  33. board times NMAX dd 0 ; The board
  34. nsols dd 0 ; Number of solutions
  35.  
  36. ; Formats for input and output
  37. promptn db 'Enter number of queens (an integer): ',0
  38. fmtintr db '%d', 0
  39. fmtintw db '%d ', 0
  40. promptyn db 'Print all solutions (y or n)? ', 0
  41. fmtstr db '%s', 0
  42. msgerryn db 'Error: invalid input', 10, 0
  43. msgerrint db 'Error: invalid integer', 10, 0
  44. fmtnsol db 'Number of solutions: %u', 10, 0
  45. fmtnl db 10, 0
  46.  
  47. section .bss
  48. n resd 1 ; Board size
  49. bprt resd 1 ; Indicator for printing solutions
  50. buffer resb 128
  51.  
  52. section .text
  53. global _main
  54. extern _printf, _scanf
  55.  
  56. _main:
  57. enter 0, 0
  58. push promptn ; Prompt for n and read it
  59. call _printf
  60. add esp, 4
  61. push n
  62. push fmtintr
  63. call _scanf
  64. add esp, 8
  65. cmp dword [n], 0 ; Check n for correct range
  66. jle invalid_n
  67. cmp dword [n], NMAX
  68. jle get_yn
  69. invalid_n:
  70. push msgerrint
  71. call _printf
  72. add esp, 4
  73. mov eax, 1
  74. jmp finish
  75. get_yn:
  76. push promptyn ; Prompt for y/n and read it
  77. call _printf
  78. add esp, 4
  79. push buffer
  80. push fmtstr
  81. call _scanf
  82. add esp, 8
  83.  
  84. mov dword [bprt],1 ; Set bprt according to y/n
  85. mov al, [buffer]
  86. cmp al, 'y'
  87. jz start_search
  88. cmp al, 'Y'
  89. jz start_search
  90. cmp al, 'n'
  91. jz set_bprt_0
  92. cmp al, 'N'
  93. jz set_bprt_0
  94. push msgerryn ; Invalid input for y/n
  95. call _printf
  96. add esp, 4
  97. mov eax, 1
  98. jmp finish
  99. set_bprt_0:
  100. mov dword [bprt], 0
  101.  
  102. ; Search for solutions starts here
  103. ; ebx points to the column where a new queen
  104. ; is being placed
  105. start_search:
  106. mov ebx, 0
  107. cont_search:
  108. mov eax,[4*ebx+board] ; Increment row
  109. inc eax
  110. cmp eax, dword [n] ; Check if row <= n
  111. jle check_board
  112. dec ebx ; Return to previous column
  113. cmp ebx, 0 ; If ebx < 0, search is finished
  114. jnge near search_finished
  115. jmp cont_search
  116.  
  117. check_board:
  118. mov [4*ebx+board],eax ; Check if last queen does
  119. cmp ebx, 0 ; not attack other queens
  120. je accept_board
  121. mov ecx, ebx
  122. mov edx, [4*ecx + board]
  123. mov esi, board
  124. cld
  125. test_loop:
  126. lodsd
  127. sub eax, edx ; Check if queens are in same row
  128. jz cont_search
  129. cmp eax, 0
  130. jg eax_positive
  131. neg eax
  132. eax_positive:
  133. sub eax, ecx ; Check if queens are in same diagonal
  134. jz cont_search
  135. loop test_loop
  136.  
  137. accept_board:
  138. inc ebx ; Go to next column
  139. cmp ebx, [n] ; If ebx==n, found a solution
  140. jz found_solution
  141. mov dword[4*ebx+board],0
  142. jmp cont_search
  143.  
  144. found_solution:
  145. inc dword [nsols] ; Increment solution count
  146. dec ebx ; Continue search in last column
  147. cmp dword [bprt], 0 ; Check if solution should be printed
  148. jz cont_search
  149.  
  150. mov ecx, [n] ; Print solution
  151. mov esi, board
  152. print_loop:
  153. push ecx
  154. lodsd
  155. push eax
  156. push fmtintw
  157. call _printf
  158. add esp, 8
  159. pop ecx
  160. loop print_loop
  161.  
  162. push fmtnl ; print new line
  163. call _printf
  164. add esp, 4
  165. jmp cont_search
  166.  
  167. search_finished:
  168. push dword [nsols] ; Print number of solutions
  169. push fmtnsol
  170. call _printf
  171. add esp, 8
  172. mov eax, 0
  173.  
  174. finish:
  175. leave
  176. ret
  177.  
There are 10 kinds of people: those that understand binary and those that don't.
L. Felipe Martins
Department of Mathematics
Cleveland State University
Reply With Quote  
Join Date: Mar 2005
Posts: 97
Reputation: Evenbit is on a distinguished road 
Rep Power: 4
Solved Threads: 2
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster in Training

Re: N-queens problem - solution in nasm

  #4  
Jul 26th, 2008
Thank you! It looks nice.

BTW, I can confirm that if you remove the underscores ( change the "_main", "_printf", and "_scanf" symbols to just "main", "printf", and "scanf" ) it assembles ( using "-f elf" rather than "-f win32" option ) fine and runs under Linux.

Nathan.
Last edited by Evenbit : Jul 26th, 2008 at 3:03 am. Reason: parenthetical clarification
while (CPU is present) {some assembly required}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Assembly Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Assembly Forum

All times are GMT -4. The time now is 11:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC