News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Sudoku *Checker*

Started by jenxin, October 13, 2011, 10:25:25 PM

Previous topic - Next topic

jenxin

Also I can only use registers eax,ebx,ecx,and edx. and none of his variables.

jenxin

Also, I have absolutely no clue how to implement that code Clive

EDIT:

I C&P'd it in.. >_>

dedndave

that code is by drizz - not Clive   :P
it looks like he already implemented it for you

clive

Quote from: drizz on October 14, 2011, 02:28:02 AM
>> I'd probably use a C language app to generate the code stream
We already have a language above asm - macro  :wink

Indeed, but it's a lot less flexible, and ill suited to some tasks.

MACRO's would work well here, but jenxin has algorithm issues.
It could be a random act of randomness. Those happen a lot as well.

jenxin

I haven't learned any C :(
I'll integrate clive's code.. I need to explain a lot of the work, Thanks anyways drizz!

And thanks alot clive, may I ask how you generated that code?

drizz

Quote from: jenxin on October 14, 2011, 02:45:30 AM
I haven't learned any C :(
I'll integrate clive's code.. I need to explain a lot of the work, Thanks anyways drizz!
Your welcome, but it was for the topic, I wanted to seed how short the code would be with macros. I wouldn't use macros and other stuff otherwise.  :U
The truth cannot be learned ... it can only be recognized.

clive

This was a quick 5 minute hack, coded for a high probability of success first time around.

#include <stdio.h>

int main(int argc, char **argv)
{
int i, j;
int v, w;

for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
printf("\tmov\tbl,[edx+%d]\t; a[%d][%d]\n",i*9 + j, i, j);
printf("\tor\tbl,bl\n");
printf("\tjz\t@F\t; skip empty\n");

for(v=0; v<9; v++)
if (v != i)
{
printf("\tcmp\tbl,[edx+%d]\t; a[%d][%d]\n", v*9 + j, v, j);
printf("\tjz\tfail\n");
}

for(w=0; w<9; w++)
if (w != j)
{
printf("\tcmp\tbl,[edx+%d]\t; a[%d][%d]\n", i*9 + w, i, w);
printf("\tjz\tfail\n");
}

printf("@@:\n");
}
}

return(1);
}
It could be a random act of randomness. Those happen a lot as well.

jenxin

Is that C? Time for me to start learning hah. I've only taken a intro to Java programming course.

drizz

Pure masm  :P
lea esi,Sudoku_Matrix

; BOUNDS CHK
i = 0
repeat 9*9
cmp byte ptr [esi+i],1
jb @@Fail
cmp byte ptr [esi+i],9
ja @@Fail
i = i + 1
endm

; DUP & SUM CHK
k = 0
repeat 3
i = 0
repeat 9
xor eax,eax
push eax
push eax
push eax
j = 0
repeat 3
if k eq 0;-- BOX
movzx ebx, byte ptr [esi+i*9+j*3+0]
movzx edx, byte ptr [esi+i*9+j*3+1]
movzx ecx, byte ptr [esi+i*9+j*3+2]
elseif k eq 1;-- ROW
movzx ebx, byte ptr [esi+(i/3)*9*3+j*9+0]
movzx edx, byte ptr [esi+(i/3)*9*3+j*9+1]
movzx ecx, byte ptr [esi+(i/3)*9*3+j*9+2]
else;-- COL
movzx ebx, byte ptr [esi+0*3+j*3*9+(i/3)*9+(i mod 3)]
movzx edx, byte ptr [esi+1*3+j*3*9+(i/3)*9+(i mod 3)]
movzx ecx, byte ptr [esi+2*3+j*3*9+(i/3)*9+(i mod 3)]
endif
or byte ptr [esp+ebx-1],1
or byte ptr [esp+edx-1],1
or byte ptr [esp+ecx-1],1
add eax,ebx
add eax,edx
add eax,ecx
j = j + 1
endm
pop ebx
pop edx
pop ecx
and ebx,edx
cmp ebx,01010101h
jne @@Fail
cmp ecx,01h
jne @@Fail
cmp eax,1+2+3+4+5+6+7+8+9
jne @@Fail
i = i + 1
endm
k = k + 1
endm
The truth cannot be learned ... it can only be recognized.

drizz

Or even better without the stack...
lea esi,Sudoku_Matrix

; BOUNDS CHK
i = 0
repeat 9*9
cmp byte ptr [esi+i],1
jb @@Fail
cmp byte ptr [esi+i],9
ja @@Fail
i = i + 1
endm

; DUP & SUM CHK
k = 0
repeat 3
i = 0
repeat 9
xor eax,eax
xor edi,edi
j = 0
repeat 3
if k eq 0
movzx ebx, byte ptr [esi+i*9+j*3+0]
movzx edx, byte ptr [esi+i*9+j*3+1]
movzx ecx, byte ptr [esi+i*9+j*3+2]
elseif k eq 1
movzx ebx, byte ptr [esi+(i/3)*9*3+j*9+0]
movzx edx, byte ptr [esi+(i/3)*9*3+j*9+1]
movzx ecx, byte ptr [esi+(i/3)*9*3+j*9+2]
elseif k eq 2
movzx ebx, byte ptr [esi+0*3+j*3*9+(i/3)*9+(i mod 3)]
movzx edx, byte ptr [esi+1*3+j*3*9+(i/3)*9+(i mod 3)]
movzx ecx, byte ptr [esi+2*3+j*3*9+(i/3)*9+(i mod 3)]
endif
bts edi,ebx
bts edi,edx
bts edi,ecx
add eax,ebx
add eax,edx
add eax,ecx
j = j + 1
endm
cmp edi,1111111110b
jne @@Fail
cmp eax,1+2+3+4+5+6+7+8+9
jne @@Fail
i = i + 1
endm
k = k + 1
endm



...Ok off I go  :bdg
The truth cannot be learned ... it can only be recognized.