📄 hanoi塔谜题的程序设计.asm
字号:
.model small
.stack 64
data segment
num db 'please input the num of plates:', 0ah, 0dh,'$'
spindle1 db 'what is the name of spindle X ?'
db 0ah,0dh,'$'
spindle2 db 'what is the name of spindle Y ?'
db 0ah,0dh,'$'
spindle3 db 'what is the name of spindle Z ?'
db 0ah,0dh,'$'
line db 0dh,0ah,'$'
step db 'move steps is as follows:',0ah,0dh,'$'
arrow db '->$'
node db ':'
plateid db 'plate$'
flag dw 0
constant dw 10000,1000,100,10,1
data ends
code segment
main proc far
assume cs: code,ds:data
start:
push ds ;save stack for return
sub ax,ax
push ax
mov ax,data ;move data segment to ds
mov ds,ax
lea dx,num ;show message to input the num of plate(N)
mov ah,09h
int 21h
call decibin ;change N into binary form
call enter
cmp bx,0
jz exit ;if N=0,exit
lea dx,spindle1 ;show messsage to input the name of spindleX
mov ah,09h
int 21h
mov ah,01h ;get the name
int 21h
mov ah,0
mov cx,ax ;read N to cx
call enter
lea dx,spindle2 ;show message to input the name of spindleY
mov ah,09h
int 21h
mov ah,01h ;get the name
int 21h
mov ah,0
mov si,ax ;read name to si
call enter
lea dx,spindle3 ;show message to input the name of spindleZ
mov ah,09h
int 21h
mov ah,01h ;get the name
int 21h
mov ah,0
mov di,ax ;read name to di
call enter
lea dx,step
mov ah,09h
int 21h
call hanoi
exit: ret
main endp
hanoi proc near
cmp bx,1
je one ;if N=1
call rpush ;save N,X,Y,Z
dec bx ;N-1
xchg si,di ;exchange Y<->Z
call hanoi ;hanoi(N-1,X,Z,Y)
call rpop ;recover N,X,Y,Z
call print ;print plateN:X->Z
dec bx ;N-1
xchg cx,si ;exchange X<->Y
call hanoi ;hanoi(N-1,Y,X,Z)
jmp exit1
one: call print ;N=1,print directly
exit1: ret
hanoi endp
print proc near ;the form is plateN:X->Z
lea dx,plateid ;print plate
mov ah,09h
int 21h
call binidec ;print N
lea dx,node ;print :
mov ah,02h
int 21h
mov dx,cx ;print X
mov ah,02h
int 21h
lea dx,arrow ;print ->
mov ah,09h
int 21h
mov dx,di ;print Z
mov ah,02h
int 21h
call enter
ret
print endp
rpush proc near
pop bp
push bx ;save N
push cx ;save X
push si ;save Y
push di ;save Z
push bp
ret
rpush endp
rpop proc near
pop bp
pop di ;recover Z
pop si ;recover Y
pop cx ;recover X
pop bx ;recover N
push bp
ret
rpop endp
decibin proc near ;get N from keyboard,and convert to binary
mov bx,0
newchar:
mov ah,1
int 21h
sub al,30h
jl exit2
cmp al,9d
jg exit2
cbw
xchg ax,bx
mov cx,10d
mul cx
xchg ax,bx
add bx,ax
jmp newchar
exit2: ret
decibin endp
binidec proc near ;convert binary N in BX to decimal N,then display it
push bx
push cx
push si
push di
mov flag,0
mov cx,5
lea si,constant
dec_div:
mov ax,bx
mov dx,0
div word ptr[si]
mov bx,dx
mov dl,al
cmp flag,0
jnz print1
cmp dl,0
je skip
mov flag,1
print1:
add dl,30h
mov ah,02h
int 21h
skip:
add si,2
loop dec_div
pop di
pop si
pop cx
pop bx
ret
binidec endp
enter proc near
lea dx,line
mov ah,09h
int 21h
ret
enter endp
code ends
end start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -