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
; 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
model large
stack h
; Main program data segment
data
; Define data segment
NumDisks dw
StartPole dw
EndPole dw
Prompt db 'Enter the number of disks : $
Prompt db 'Enter the starting pole : $
Prompt db 'Enter the ending pole : $
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 axCh
int h
ProgramStart ENDP
; Procedure to input number of disks and poles
InputDisks PROC
InputDisksStart:
; Input number of disks
mov dx OFFSET Prompt
mov ah
int h
call GetDec
mov NumDisks, ax
cmp ax
jl InvalidInput
cmp ax
jg InvalidInput
; Input starting pole
mov dx OFFSET Prompt
mov ah
int h
call GetDec
mov StartPole, ax
cmp ax
jl InvalidInput
cmp ax
jg InvalidInput
; Input ending pole
mov dx OFFSET Prompt
mov ah
int h
call GetDec
mov EndPole, ax
cmp ax
jl InvalidInput
cmp ax
jg InvalidInput
cmp ax StartPole
je InvalidInput
ret
InvalidInput:
; Print error message
mov dx OFFSET InvalidMsg
mov ah
int h
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 print the move
mov axbp ; NumDisks
cmp ax
jne RecursiveCall
; Print the move
mov dx OFFSET MoveMsg
mov ah
int h
mov axbp ; NumDisks
call PutDec
mov dx OFFSET FromMsg
mov ah
int h
mov axbp ; StartPole
call PutDec
mov dx OFFSET ToMsg
mov ah
int h
mov axbp ; EndPole
call PutDec
call PutCrLf
jmp EndHanoi
RecursiveCall:
; Recursive call: HanoiNumDisks StartPole, StartPole EndPole
mov axbp ; NumDisks
dec ax ; NumDisks
push ax
mov axbp ; StartPole
push ax
mov ax
sub axbp ; StartPole
sub axbp ; EndPole
push ax
call Hanoi
; Print the move
mov dx OFFSET MoveMsg
mov ah
int h
mov axbp ; NumDisks
call PutDec
mov dx OFFSET FromMsg
mov ah
int h
mov axbp ; StartPole
call PutDec
mov dx OFFSET ToMsg
mov ah
int h
mov axbp ; EndPole
call PutDec
call PutCrLf
; Recursive call: HanoiNumDisks StartPole EndPole, EndPole
mov axbp ; NumDisks
dec ax ; NumDisks
push ax
mov ax
sub axbp ; StartPole
sub axbp ; EndPole
push ax
mov axbp ; EndPole
push ax
call Hanoi
EndHanoi:
; Restore registers from stack
popf
popa
; Return to caller module
ret
Hanoi ENDP
; Procedure to print initial greeting messages to the user
Greet PROC
; Print greeting messages
mov dx OFFSET Msg
mov ah
int h
call PutCrLf
mov dx OFFSET Msg
mov ah
int h
call PutCrLf
mov dx OFFSET Msg
mov ah
int h
call PutCrLf
ret
Greet ENDP
; Data segment
data
Msg db 'Program: Towers of Hanoi$
Msg db 'Programmer:$
Msg 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
