Question: Assembler : Borland TASM 3 . 0 File Name : project 2 . asm Input : Number of disks, starting pole, ending pole Output :

Assembler : Borland TASM 3.0
File Name : project2.asm
Input : Number of disks, starting pole, ending pole
Output : Sequence of moves to solve the Towers of Hanoi
Input Files : None
Output Files: None
PROCEDURES CALLED:
External procedures called:
FROM iofar.lib: PutStr, PutCrLf, GetDec, PutDec
Internal procedures called:
Greet, Hanoi, InputDisks
INCLUDELIB iofar
;****** BEGIN MAIN PROGRAM *********************
DOSSEG
.186
.model large
.stack 200h ; Increase stack size for recursive calls
;****** MAIN PROGRAM DATA SEGMENT ******************
; Define data segment
.data
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,4ch
int 21h
ProgamStart ENDP
; Procedure to input number of disks and poles
InputDisks PROC
InputDisksStart:
; Input number of disks
mov dx, OFFSET Prompt1
mov ah,09h
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,09h
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,09h
int 21h
call GetDec
mov EndPole, ax
cmp ax,1
jl InvalidInput
cmp ax,3
jg InvalidInput
cmp ax, StartPole
je InvalidInput
ret
InvalidInput:
mov dx, OFFSET InvalidMsg
mov ah,09h
int 21h
jmp InputDisks
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,9h
int 21h
mov ax,[bp +4] ; NumDisks
call PutDec
mov dx, OFFSET FromMsg
mov ah,9h
int 21h
mov ax,[bp +6] ; StartPole
call PutDec
mov dx, OFFSET ToMsg
mov ah,9h
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,9h
int 21h
mov ax,[bp +4] ; NumDisks
call PutDec
mov dx, OFFSET FromMsg
mov ah,09h
int 21h
mov ax,[bp +6] ; StartPole
call PutDec
mov dx, OFFSET ToMsg
mov ah,9h
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 6 ; Clean up the stack by removing the parameters
Hanoi ENDP
comment |
******* PROCEDURE HEADER ***************
PROCEDURE NAME : Greet
PURPOSE : To print initial greeting messages to the user
INPUT PARAMETERS : None
OUTPUT PARAMETERS or RETURN VALUE: None
NON-LOCAL VARIABLES REFERENCED: None
NON-LOCAL VARIABLES MODIFIED :None
PROCEDURES CALLED :
FROM iofar.lib: PutCrLf
CALLED FROM : Main Program
;****** SUBROUTINE DATA SEGMENT ***********
.data
Msg1 db 'Program: Towers of Hanoi $'
Msg2 db 'Programmer: $'
Msg3 db 'Date: $'
;****** SUBROUTINE CODE SEGMENT ***********
.code
Greet PROC NEAR
mov ax, @data
pusha
pushf
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
popf
popa
ret
Greet ENDP
end ProgramStart
This code is not working.Please correct this code and solve for tower of hanoi. Please provide full code not partial. Also provide all PROCEDURE

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 Accounting Questions!