;PROGRAM TITLE GOES HERE -- HANOI
;Solves tower of hanoi puzzle.Printout sequence of moves
;of N discs from initial spindle X to final spindle Z.
;using spindle Y for temporery storage
;
datarea segment ;define data segment
message1 db 'N=?',0ah,0dh,'′message2db'
message3 db 'What is the name of spindle Y ?'
db 0ah,0dh,''
flag dw 0
constant dw 10000,1000,100,10,1
datarea ends
;*****************************************************
prognam segment ;define code segment
;-----------------------------------------------------
main proc far
assume cs:prognam, ds:datarea
start:
;set up stack for return
push ds
sub ax,ax
push ax
;set DS register to current data segment
mov ax,datarea
mov ds,ax
;main part of program goes here
lea dx,message1 ;N=7?
mov ah,09h
int 21h
call decibin ;read N into BX
call crlf
Assistant
This program is a solution for the Tower of Hanoi puzzle. It prompts the user to enter the number of discs and the names of the three spindles. It then solves the puzzle and prints out the sequence of moves to move all the discs from the initial spindle to the final spindle.
The program uses a subprocedure called "hanoi" to solve the puzzle recursively. The base case is when there is only one disc, in which case it prints out the move from the initial spindle to the final spindle. Otherwise, it makes a recursive call to solve the puzzle for N-1 discs, moving them from the initial spindle to the temporary spindle. It then prints out the move from the initial spindle to the final spindle and makes another recursive call to solve the puzzle for N-1 discs, moving them from the temporary spindle to the final spindle.
The program also includes the following subprocedures:
"save" and "restor" to save and restore the values of N, X, Y, and Z on the stack during the recursive calls
cmp bx,0 ;if N=0
jz exit ;exit
lea dx,message2 ;X=?
mov ah,09h
int 21h
mov ah,01h ;read X's name into CX
int 21h
mov ah,0
mov cx,ax
call crlf
lea dx,message3 ;Y=?
mov ah,09h
int 21h
mov ah,01h ;read Y'name into SI
int 21h
mov ah,0
mov si,ax
call crlf
lea dx,message4 ;Z=?
mov ah,09h
int 21h
mov ah,01h ;read Z's name into DI
int 21h
mov ah,0
mov di,ax
call crlf
call hanoi ;call HANOI(N,X,Y,Z)
exit: ret ;return to DOS
main endp
;---------------------------------------------------
hanoi proc near ;define subprocedure
;SOlves tower of hanoi puzzle
;Argement :(BX)=N,(CX)=X,(SI)=Y,(DI)=Z.
cmp bx,1 ;if N=1,execute basis
je basis
call save ;save N,X,Y,Z
dec bx
xchg si,di
call hanoi ;call HANOI(N-1,X,Z,Y)
call restor ;restore N,X,Y,Z
call print ;print XNZ
dec bx
xchg cx,si
call hanoi ;call HANOI(N-1,Y,X,Z)
jmp return
basis: call print ;print X1Z
return: ret ;return
hanoi endp ;end subprocedure
;-------------------------------------------------------
print proc near
mov dx,cx ;print X
mov ah,02h
int 21h
call binidec ;print N
mov dx,di ;print Z
mov ah,02h
int 21h
call crlf ;skip to next line
ret
print endp
;-----------------------------------------------------------
save proc near
pop bp
push bx
push cx
push si
push di
push bp
ret
save endp
;-----------------------------------------------------------
restor proc near
pop bp
pop di
pop si
pop cx
pop bx
push bp
ret
restor endp
;------------------------------------------------------------
decibin proc near
;prodecure to convert decimal on keyboard to binary.
;result is left in BX register
mov bx,0
;get digit from keyboard ,convert to binary
newchar:
mov ah,1 ;keyboard input
int 21h ;call DOS
sub al,30h ;ASCII to binary
jl exit1 ;jump if<0
cmp al,9d ;is it >9d?
jg exit1 ;yes,not dec digit
cbw ;BYTE in AL to WORD in AX
;(digit is now in AX)
;multiply number in BX by 10 decimal
xchg ax,bx ;trade digit&number
mov cx,10d ;put 10 dec in CX
mul cx ;number times 10
xchg ax,bx ;trade number &digit
;add digit in AX to number in BX
add bx,ax ;add digit to number
jmp newchar ;get next digit
exit1: ret ;return from decibin
decibin endp ;end of decibin proc
;------------------------------------------------------------
binidec proc near
;procedure to convert binary number in BX to decimal
;on console screen
push bx
push cx
push si
push di
mov flag,0
mov cx,5
lea si,constant
dec_div:
mov ax,bx ;number high half
mov dx,0 ;zero out low half
div word ptr[si] ;divide by contant
mov bx,dx ;reminder into BX
mov dl,al ;quotient into DL
code
cmp flag,0 ;have not leading zero
jnz print1
cmp dl,0
je skip
mov flag,1
;print the contents of DL on screen
print1: add dl,30h ;convert to ASCII
mov ah,02h ;display fuction
int 21h ;call DOS
skip: add si,2
loop dec_div
pop di
pop si
pop cx
pop bx
ret ;return from dec_div
binidec endp
;-------------------------------------------------------------
crlf proc near
mov dl,0ah ;linefeed
mov ah,02h ;display function
int 21h
;
mov dl,0dh ;carriage return
mov ah,02h ;display function
int 21h
;
ret
crlf endp
;-----------------------------------------------------------
prognam ends ;end of code segment
;*************************************************************
end start ;end assembly
"decibin" to convert a decimal number from the keyboard input to binary in the BX register
"binidec" to convert a binary number in the BX register to decimal and print it on the console screen
"crlf" to move the cursor to the next line on the console screen
The program uses DOS interrupts to read from the keyboard and print to the console screen.
To run the program, assemble and run the compiled code using an assembler and execute the resulting program.
;PROGRAM TITLE GOES HERE -- HANOI
;Solves tower of hanoi puzzle.Printout sequence of moves
;of N discs from initial spindle X to final spindle Z.
;using spindle Y for temporery storage
;
datarea segment ;define data segment
message1 db 'N=?',0ah,0dh,'$'
message2 db 'What is the name of spindle X ?',0ah,0dh,'$'
message3 db 'What is the name of spindle Y ?',0ah,0dh,'$'
message4 db 'What is the name of spindle Z ?',0ah,0dh,'$'
flag dw 0
constant dw 10000,1000,100,10,1
datarea ends
fileseg segment
inputname db 256 dup (0)
outputname db 256 dup (0)
fileseg ends
;*****************************************************
prognam segment ;define code segment
;-----------------------------------------------------
main proc far
assume cs:prognam, ds:datarea, es:fileseg
start:
;set up stack for return
push ds
sub ax, ax
push ax
;set DS register to current data segment
mov ax, datarea
mov ds, ax
;main part of program goes here
lea dx, message1 ;N=?
mov ah, 09h
int 21h
call decibin ;read N into BX
call crlf
cmp bx, 0 ;if N=0
jz exit ;exit
lea dx, message2 ;X=?
mov ah, 09h
int 21h
mov ah, 01h ;read X's name into CX
int 21h
mov ah, 0
mov cx, ax
call crlf
lea dx, message3 ;Y=?
mov ah, 09h
int 21h
mov ah, 01h ;read Y'name into SI
int 21h
mov ah, 0
mov si, ax
call crlf
lea dx, message4 ;Z=?
mov ah, 09h
int 21h
mov ah, 01h ;read Z's name into DI
int 21h
mov ah, 0
mov di, ax
call crlf
; check if input and output file names provided as command line arguments
mov ah, 62h ; get command line
mov dl, 0 ; pointer to command line buffer
mov al, 256 ; buffer size
int 21h
mov si, dx ; SI points to command line buffer
next_arg:
; read input file name
mov ah, [si] ; get the first character of argument
cmp ah, 0 ; check if end of command line
je skip_args ; exit if end of command line
cmp ah, ' ' ; check if argument is a space
je read_output_name ; go to read output file name if input file name is empty
mov word ptr [inputname+si-dx], ax ; copy character to input name buffer
inc si ; move to next character
jmp next_arg
read_output_name:
inc si ; skip the space character
; read output file name
mov ah, [si] ; get the first character of argument
cmp ah, 0 ; check if end of command line
je skip_args ; exit if end of command line
mov word ptr [outputname+si-dx], ax ; copy character to output name buffer
inc si ; move to next character
jmp next_arg
skip_args:
; check if input file name provided
mov al, [inputname] ; check if first character of input name is empty
cmp al, 0
je skip_input
; open input file
mov dx, offset inputname
mov al, 0 ; open for read
jmp open_file
skip_input:
; open console for input
mov dx, NULL
mov al, 2 ; open stdin
mov bh, 0 ; default device
jmp open_file
open_file:
push si ; save SI register
lea dx, DOSfilename
mov cx, 0 ; default attributes
mov si, dx ; get the pointer to the file name
mov ah, 3dh ; open file
int 21h ; call DOS
mov bx, ax ; save file handle in BX
pop si ; restore SI register
mov ah, 3fh ; read from file
mov cx, 256 ; buffer size
mov dx, offset buffer ; pointer to buffer
int 21h ; call DOS
xor bx, bx ; clear BX register
mov bl, [buffer+1] ; get the number of discs from the buffer
mov ah, 2 ; set output to file
mov dl, 1 ; file number
jmp check_output_file
exit:
ret ; return to DOS
main endp
;---------------------------------------------------
hanoi proc near ;define subprocedure
;Solves tower of hanoi puzzle
;Argement :(BX)=N,(CX)=X,(SI)=Y,(DI)=Z.
cmp bx, 1 ;if N=1,execute basis
je basis
call save ;save N,X,Y,Z
dec bx
xchg si, di
call hanoi ;call HANOI(N-1,X,Z,Y)
call restor ;restore N,X,Y,Z
call print ;print XNZ
dec bx
xchg cx, si
call hanoi ;call HANOI(N-1,Y,X,Z)
jmp return
basis: call print ;print X1Z
return: ret ;return
hanoi endp ;end subprocedure
;-------------------------------------------------------
print proc near
mov dx, cx ;print X
mov ah, 02h
int 21h
call binidec ;print N
mov dx, di ;print Z
mov ah, 02h
int 21h
call crlf ;skip to next line
ret
print endp
;-----------------------------------------------------------
save proc near
pop bp
push bx
push cx
push si
push di
push bp
ret
save endp
;-----------------------------------------------------------
restor proc near
pop bp
pop di
pop si
pop cx
pop bx
push bp
ret
restor endp
;------------------------------------------------------------
decibin proc near
;prodecure to convert decimal on keyboard to binary.
;result is left in BX register
mov bx, 0
;get digit from keyboard, convert to binary
newchar:
mov ah, 1 ;keyboard input
int 21h ;call DOS
sub al, 30h ;ASCII to binary
jl exit1 ;jump if <0
cmp al, 9d ;is it >9d?
jg exit1 ;yes, not decimal digit
cbw ;BYTE in AL to WORD in AX
;(digit is now in AX)
;multiply number in BX by 10 decimal
xchg ax, bx ;trade digit & number
mov cx, 10d ;put 10 decimal in CX
mul cx ;number times 10
xchg ax, bx ;trade number & digit
;add digit in AX to number in BX
add bx, ax ;add digit to number
jmp newchar ;get next digit
exit1:
ret ;return from decibin
decibin endp ;end of decibin proc
;------------------------------------------------------------
binidec proc near
;procedure to convert binary number in BX to decimal
;on console screen
push bx
push cx