Post History
x86 32-bit machine code, 26 bytes B6 80 8A 01 41 D4 05 92 28 D0 28 F4 70 F4 F6 EC 04 02 A8 FB 74 EC F6 DE D6 C3 Try it online! Following the fastcall calling convention, this function takes th...
#1: Initial revision
# x86 32-bit machine code, 26 bytes
B6 80 8A 01 41 D4 05 92 28 D0 28 F4 70 F4 F6 EC 04 02 A8 FB 74 EC F6 DE D6 C3
[Try it online!](https://tio.run/##dVLbbtpAEH32fsXUFZKdOBElbRWFUgls7jYXczNKEFrW60tl7Mo2qSlKP710lqQtqdSX3Zk558wc7Q678hk7Ht@GMYt2LodPWe6GyXXwmbwqReHm31oaxr6okfWa5phtdjlfrxXFo1nOaBSpKrCApuApLImz/Dm5yNQqodkWFLBlhXh30jZ5BDfQ4Opd5ZakzzmNNLjnrFgRCQcCRkSidAsfiFSwwAfuFhpwitVstzmx3eglxk5uQKQvCaQo3u4iLKHYdU@0CpFyjl5E/OM90r4LWsz9k@h6s885lAvjI5FSnhNVBnRLzu0LNaMZz@5XUIMDka2e3ho7Q6M9Wky7jf580mkOlvbMrMsakevmzF4Omp3JvN/oThejtjF0xi29ZwnwFfu8jQD/o6ubAjS7jd5oMdBbU2xQH87stmE542W/KcBBy2jbGHfGzqinm@gKGZO51RCgPez058a0a8705aA9ajhWb9Jc1MctAXYwMic9p9G3h8a8NR7oy@7UGrVnCD5VSRjnsKVhrIiApj7TXt7kApNHlRzEZ@VQ4LuUNdhXieQlKShVrLypwS3el5cqkaSvuDi5p8ilDErhQyxr8PdRi5WG@3Keq/gDfyQPsUVZEMYcWOLyO1lgp3l4uwkcfvOgVK442HhfU5RdnIV@zN2T2QvVU@/Rx0qtPsG3IIw4KHthr1zoN@eTQCmFINYhU08WCwHiTuzSWEx7IsfjT@ZF1M@OV9ubCh641DXU8ugX "C (gcc) – Try It Online")
Following the `fastcall` calling convention, this function takes the address of a null-terminated byte string in ECX and returns an 8-bit integer in AL, which is 0 if the string is a valid knight's tour and -1 if it is not.
The basic method is to divmod each ASCII code by 5 to get coordinates, and take differences; the valid knight's moves of (±1, ±2) and (±2, ±1) are characterized by the product of the two coordinates being ±2.
In assembly:
````
f: mov dh, -128 # Set DH to -128.
r: mov al, [ecx] # Load a byte from the string into AL.
inc ecx # Advance the pointer.
aam 5 # Divide by 5; quotient in AH, remainder in AL.
xchg edx, eax # Exchange registers, putting those in DH and DL.
sub al, dl # Subtract DL from AL (this and prev remainders).
sub ah, dh # Subtract DH from AH (this and prev quotients).
jo r # Jump back if signed overflow (occurs 1st time).
imul ah # Multiply the two differences.
add al, 2 # Add 2 to the product.
test al, ~4 # In the result, look at all except the 4s bit.
jz r # Jump back if those bits are zero (from 0 or 4).
neg dh # Negate DH (the quotient from the last byte).
# CF becomes 1 if DH is nonzero, 0 if zero;
# DH is nonzero for letters, zero for the null.
.byte 0xD6 # Undocumented SALC instruction: set AL to -CF.
ret # Return.
````
