I have an assembly32 code for Hanoi, but I can't compile it, i get the error: hanoi.asm(9) : error A2070: invalid instruction operands I use MASM32.
The full code:
.586
.MODEL FLAT
PUBLIC _towers
EXTERN _printf:NEAR
.CODE
_towers:PUSH EBP
MOV EBP, ESP
SUB ESP, 4
CMP [EBP+8], 1 ;ERROR
JNE L1
MOV EAX, [EBP+16]
PUSH EAX
MOV EAX, [EBP+12]
PUSH EAX
PUSH OFFSET FLAT:format;
CALL _printf
ADD ESP, 12
JMP Done
L1: MOV EAX, 6
SUB EAX, [EBP+12]
SUB EAX, [EBP+16]
MOV [EBP-4], EAX
PUSH EAX
MOV EAX, [EBP+12]
PUSH EAX
MOV EAX, [EBP+8]
DEC EAX
PUSH EAX
CALL _towers
ADD ESP, 12
MOV EAX, [EBP+16]
PUSH EAX
MOV EAX, [EBP+12]
PUSH EAX
PUSH 1
CALL _towers
ADD ESP,12
MOV EAX, [EBP+16]
PUSH EAX
MOV EAX, [EBP-4]
PUSH EAX
MOV EAX, [EBP+8]
DEC EAX
PUSH EAX
CALL _towers
ADD ESP, 12
Done: MOV ESP,EBP
POP EBP
RET 0
.DATA
format DB "Move from %d to %d\n"
END
Can you please help me, how can I make this work?
Your code didn't work for me, I ran it but something is wrong with the algorithm, it never found the solution.
So, here is another hanoi towers solution that actually works. It's made with EMU8086 (MASM Intel syntax), you just copy-paste and run :
.model small
.stack 300H
.data
x dw 3
text db "Move from peg "
d1 db ?
text2 db " to peg "
d2 db ?
newline db 0AH, 0DH, '$'
.code
main proc
mov ax, @data
mov ds, ax
mov ax, 1
push ax
mov ax, 3
push ax
mov ax, 2
push ax
mov ax, x
push ax
call solve
mov ax, 4C00H
int 21h
main endp
solve proc
push bp
mov bp, sp
cmp word ptr ss:[bp+4], 0
je down
push word ptr ss:[bp+0AH]
push word ptr ss:[bp+6]
push word ptr ss:[bp+8]
mov ax, word ptr ss:[bp+4]
dec ax
push ax
call solve
push word ptr ss:[bp+0AH]
push word ptr ss:[bp+08]
call print
push word ptr ss:[bp+06H]
push word ptr ss:[bp+8]
push word ptr ss:[bp+0AH]
mov ax, word ptr ss:[bp+4]
dec ax
push ax
call solve
pop bp
ret 8
down:
pop bp
ret 8
solve endp
print proc
push bp
mov bp, sp
mov d1, '0'
mov al, byte ptr ss:[bp+06]
add d1, al
mov d2, '0'
mov al, byte ptr ss:[bp+4]
add d2, al
lea dx, text
mov ah, 09
int 21h
pop bp
ret 4
print endp
end
Remember what I said about initialization data? Well, this one has it! Maybe that's why it works.