Question: ; Towers of Hanoi program ; ; This program prompts the user to input the number of disks, starting pole, and ending pole ; and

; Towers of Hanoi program
;
; This program prompts the user to input the number of disks, starting pole, and ending pole
; and then prints the sequence of moves to solve the Towers of Hanoi problem using recursion.
; Assembler: Borland TASM 3.0
; File Name: project.asm
; Input: Number of disks, starting pole, ending pole
; Output: Sequence of moves to solve the Towers of Hanoi
; Procedures called: External procedures called: FROM iofar.lib: PutStr, PutCrLf, GetDec, PutDec
; Internal procedures called: Greet, Hanoi, InputDisks
;
.
DOSSEG
.
186
.
model large
.
stack 200h
; Main program data segment
.
data
; Define data segment
NumDisks dw ?
StartPole dw ?
EndPole dw ?
Prompt1 db 'Enter the number of disks (3-7): $'
Prompt2 db 'Enter the starting pole (1-3): $'
Prompt3 db 'Enter the ending pole (1-3): $'
InvalidMsg db 'Invalid input! Starting and ending poles must be different.$'
MoveMsg db 'Move disk $'
FromMsg db 'from pole $'
ToMsg db 'to pole $'
; Define code segment
.
code
ProgramStart PROC ; Initialize data segment
mov ax, @data
mov ds, ax ; Call Greet procedure
call Greet
; Input number of disks
call InputDisks
; Call the Hanoi procedure with the input parameters
push NumDisks
push StartPole
push EndPole
call Hanoi
; Exit to the operating system
mov ax,4C00h
int 21h
ProgramStart ENDP
; Procedure to input number of disks and poles
InputDisks PROC
InputDisksStart:
; Input number of disks
mov dx, OFFSET Prompt1
mov ah,9
int 21h
call GetDec
mov NumDisks, ax
cmp ax,3
jl InvalidInput
cmp ax,7
jg InvalidInput
; Input starting pole
mov dx, OFFSET Prompt2
mov ah,9
int 21h
call GetDec
mov StartPole, ax
cmp ax,1
jl InvalidInput
cmp ax,3
jg InvalidInput
; Input ending pole
mov dx, OFFSET Prompt3
mov ah,9
int 21h
call GetDec
mov EndPole, ax
cmp ax,1
jl InvalidInput
cmp ax,3
jg InvalidInput
cmp ax, StartPole
je InvalidInput
ret
InvalidInput:
; Print error message
mov dx, OFFSET InvalidMsg
mov ah,9
int 21h
jmp InputDisksStart
InputDisks ENDP
; Procedure to solve Towers of Hanoi recursively
Hanoi PROC
; Save registers on the stack
pusha
pushf
; Set up bp register to point to parameters
mov bp, sp
; If NumDisks is 1, print the move
mov ax,[bp +4] ; NumDisks
cmp ax,1
jne RecursiveCall
; Print the move
mov dx, OFFSET MoveMsg
mov ah,9
int 21h
mov ax,[bp +4] ; NumDisks
call PutDec
mov dx, OFFSET FromMsg
mov ah,9
int 21h
mov ax,[bp +6] ; StartPole
call PutDec
mov dx, OFFSET ToMsg
mov ah,9
int 21h
mov ax,[bp +8] ; EndPole
call PutDec
call PutCrLf
jmp EndHanoi
RecursiveCall:
; Recursive call: Hanoi(NumDisks -1, StartPole, 6- StartPole - EndPole)
mov ax,[bp +4] ; NumDisks
dec ax ; NumDisks -1
push ax
mov ax,[bp +6] ; StartPole
push ax
mov ax,6
sub ax,[bp +6] ; StartPole
sub ax,[bp +8] ; EndPole
push ax
call Hanoi
; Print the move
mov dx, OFFSET MoveMsg
mov ah,9
int 21h
mov ax,[bp +4] ; NumDisks
call PutDec
mov dx, OFFSET FromMsg
mov ah,9
int 21h
mov ax,[bp +6] ; StartPole
call PutDec
mov dx, OFFSET ToMsg
mov ah,9
int 21h
mov ax,[bp +8] ; EndPole
call PutDec
call PutCrLf
; Recursive call: Hanoi(NumDisks -1,6- StartPole - EndPole, EndPole)
mov ax,[bp +4] ; NumDisks
dec ax ; NumDisks -1
push ax
mov ax,6
sub ax,[bp +6] ; StartPole
sub ax,[bp +8] ; EndPole
push ax
mov ax,[bp +8] ; EndPole
push ax
call Hanoi
EndHanoi:
; Restore registers from stack
popf
popa
; Return to caller module
ret 12
Hanoi ENDP
; Procedure to print initial greeting messages to the user
Greet PROC
; Print greeting messages
mov dx, OFFSET Msg1
mov ah,9
int 21h
call PutCrLf
mov dx, OFFSET Msg2
mov ah,9
int 21h
call PutCrLf
mov dx, OFFSET Msg3
mov ah,9
int 21h
call PutCrLf
ret
Greet ENDP
; Data segment
.
data
Msg1 db 'Program: Towers of Hanoi$'
Msg2 db 'Programmer:$'
Msg3 db 'Date:$'
; Code segment
.
code
END ProgramStart
This cod eis not working. Please correct it and provide whole code (from beganing to ending)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!