CSE306 Processing Systems and Structures Lockwood, Spring 2004

Machine Problems 2 and 3: The Bakery of Hanoi

Assigned Wednesday, March 17, 2004
Due Date MP2: Monday, March 29, 2004, 3pm (50 pts)
MP3: Monday, April 5, 2004, 3pm (50 pts)
Purpose: Understand recursion, text-mode video, and mouse control.
Points50 + 50 = 100

Introduction

A wedding is planned in the town of Hanoi. For the event, a baker has prepared an elaborate wedding cake with multiple tiers. Each tier of the cake is a different size. Initially, the cake is stacked at the bakery with the largest tier at the bottom and progressively smaller cakes stacked above one another. Using manual labor and the bakery's single delivery truck, a plan is needed to transport the entire cake to the recepton. A diagram is shown below:

The baker must transport the entire cake to the reception. Because the cake is heavy, it must be moved one tier at a time. To insure that the cakes are not damaged before the reception, the cake tiers must always be kept at the bakery, in the delivery van, or on the table at the reception. Tiers of cake can be moved directly between all three locations. Tiers of the cake may be stacked, but a larger tier must never be placed atop a smaller one for fear of collapsing the smaller tier.

The baker's delivery man, Igor, has already moved some of the tiers of cake. Wrought with confusion and frustration from moving the cake, Igor has called the baker and quit his job. With the wedding reception only hours away, the baker is desparate to find someone to get all of the cakes to the reception and stacked in the proper order.

As a student of CSE306, the baker has hired you to help him devise a plan to start where Igor left the tiers and assemble the cake at the reception as efficiently as possible.

Implementation

For this machine problem, you will write a program which:

A screen dump of the running program is shown above. Along the top of the screen are a series of buttons. Each of these buttons responds to a mouse-click. Just below the buttons, the program displays the current number of moves as well as the number of tiers of cake. The lower portion of the screen contains the bakery, the delivery truck, and reception. To win the game, the entire cake should be moved to the reception (the right-most location).

Usage Instructions

The library-based MP2.EXE demo can be run from the DOS prompt. Use the mouse to press the on-screen buttons and move tiers of cake between locations. Tiers are moved by moving the mouse to the source, pressing the mouse button, moving the mouse to the destination, then releasing the mouse button. The program will not allow larger tiers of cake to be placed atop smaller tiers. If the mouse does not appear on your screen, enter mouse to load your mouse driver. This program should operate both in full-screen text-mode and within a window.

Data Structures

The following variables are used throughout the program and by the library functions.

Procedures

The ASM procedures that you need to implement are described below. There are working library versions of each of these procedures in libmp2.lib. Unless otherwise noted, it is expected that ALL routines will preserve the value of ALL registers modified, other than the output (i.e., PUSH and POP all registers except for those that need to return a value).

You are encouraged to write your own procedures to handle repetive tasks. You will find, for example, that a function which writes messages on the screen at given location can be used throughout your program.

Be sure that you have experimented with the library-based program before you begin. It is important that you fully understand the problem before you try to write the code. Because library functions call other library functions, you are encouraged to begin coding with the MAIN routine.

Program Assignment

You earn points by replacing each subroutine with your own code. Your score will be proportional to the percentage of the code that your write yourself. The breakdown in points is given below. Your routine MUST perform all functions of the subroutine to receive credit.

Preliminary Procedure

Notes


MP2.ASM

PAGE 75, 132 TITLE CSE306 - Bakery of Hanoi - Your Name - Date COMMENT % The Bakery of Hanoi ------------------- CSE306: Machine Problem 2/3 Prof. John W. Lockwood Washington University in St. Louis Spring 2004 Revision 1.2 (Updated 3/19/04) % ;====== Constants ========================================================= CR EQU 13 LF EQU 10 ESCKEY EQU 27 SPACE EQU 32 VIDTXTSEG EQU 0B800h ; VGA Video Segment Adddress (Text Mode) ;====== Externals ========================================================= ; -- LIB306 Routines (Free) --- extrn kbdine:near, kbdin:near, dspout:near ; LIB306 Routines extrn dspmsg:near, binasc:near, ascbin:near ; (Always Free) ; -- LIBMP2/3 Routines (You need to write these on your own) --- extrn LibPrintString:near extrn LibClearScreen:near extrn LibRandom:near extrn LibMoveTier:near extrn LibDrawScreen:near extrn LibRedrawScreen:near extrn LibResetGame:near extrn LibDistribute:near extrn LibMouseCtrl:near extrn LibAutoSolve:near extrn LibMain:near extrn mp3xit:near ;====== Stack segment ==================================================== stkseg segment stack ; *** STACK SEGMENT *** db 64 dup ('STACK ') ; 64*8 = 512 Bytes of Stack stkseg ends ;====== Code/Data segment ================================================ cseg segment public 'CODE' ; *** CODE SEGMENT *** assume cs:cseg, ds:cseg, ss:stkseg, es:nothing ;====== Variables ========================================================= Moves DW 0 ; Number of moves required (Reset to zero for each game) Tiers DW 4 ; Number of Tiers (Default with 4 tiers) Bakery DW 0000000000000001b ; -- Sample Bit-mapped array -- Truck DW 0000000000000010b ; ### Recept DW 0000000000001100b ; ######### ####### ##### ; Bakery Truck Recept ; Dest=0 Dest=1 Dest=2 WelcomeMessage db 'Welcome to the BAKERY of HANOI$' PressEscKey db 'Press [Esc] key to continue$' CheckBackground db 'Screen background should now be white$' RandomNumberMessage db 'Random numbers in range {0..2}:$' HanoiMsg db ' CSE306 MP 2/3 ver 1.2 ' db ' THE BAKERY OF HANOI ' db ' Lockwood Spr 2004','$' ; Note that this is really just one long string RandSeed DW 13 ; Random Number Seed PBUF db '$$$$$$$' ; A string to hold binary->ASCII converted numbers PUBLIC HanoiMsg, Moves, Tiers, RandSeed, Bakery, Truck, Recept ;====== Procedures ======================================================== PrintString Proc near call LibPrintString ; Comment the line above and add your own code here ret PrintString EndP ClearScreen Proc near call LibClearScreen ; Comment the line above and add your own code here ret ClearScreen EndP Random Proc near Call LibRandom ; Comment the line above and add your own code here ret Random EndP DrawScreen Proc near call LibDrawScreen ; Comment the line above and add your own code here ret DrawScreen EndP RedrawScreen Proc near call LibRedrawScreen ; Comment the line above and add your own code here ret RedrawScreen EndP MoveTier Proc near call LibMoveTier ; Comment the line above and add your own code here ret MoveTier EndP ResetGame Proc near call LibResetGame ; Comment the line above and add your own code here ret ResetGame EndP Distribute Proc near call LibDistribute ; Comment the line above and add your own code here ret Distribute EndP MouseCtrl Proc near call LibMouseCtrl ; Comment the line above and add your own code here ret MouseCtrl EndP AutoSolve Proc near call LibAutoSolve ; Comment the line above and add your own code here ret AutoSolve EndP PUBLIC PrintString, ClearScreen, Random PUBLIC MoveTier, DrawScreen, RedrawScreen, ResetGame PUBLIC Distribute, MouseCtrl, AutoSolve ;====== Main procedure ==================================================== main proc far mov ax, cseg ; Initialize DS register mov ds, ax mov ax, VIDTXTSEG ; Segment address of video memory mov es, ax mov AX, 0002h ; Put Video card into 80x25 text mode int 10h ; ---- Display a few text messages when the program begins ---- MOV DI, 2*(80*10+25) ; Screen offset for Row=10, Column=25 MOV BX, offset WelcomeMessage MOV AH, 3h ; Cyan=Green+Blue call PrintString ADD DI, 2*80 ; Print next message on line below the first MOV BX, offset PressEscKey MOV AH, 07h ; White text on black background call PrintString call kbdin ; Wait for user input by reading in a key call ClearScreen ; Clear the screen (white background, black text) MOV DI, 2*(80*10+25) ; Screen offset for Row=10, Column=25 MOV BX, offset CheckBackground MOV AH, 74h ; White background with Red Text call PrintString ADD DI, 2*2*80 ; Print next message 2 lines below the first MOV BX, offset RandomNumberMessage MOV AH, 70h ; Black text on white background Call PrintString ADD DI, 2*32 MOV CX,10 PrintTenRandomNumbers: PUSH AX PUSH BX PUSH DI PUSH CX Call Random ; Generate random number between 0..2 MOV BX, offset PBUF Call BinAsc ; Convert to ASCII string MOV AH, 70h ; Black text on white background Call PrintString POP CX POP DI POP BX POP AX ADD DI, 4 Loop PrintTenRandomNumbers MOV DI, 2*(80*14+25) ; Row 14, Col. 25 MOV BX, offset PressEscKey MOV AH, 70h ; Black text on white background Call PrintString call kbdin ; ---- The Program begins here ---- mov tiers, 4 ; Start with Default of 4 Cake Tiers call Distribute ; Randomly distribute to bakery,truck,recept call DrawScreen ; Draw the text-mode video screen mov ax,0 ; init mouse int 33h mov ax,1 ; Show Mouse int 33h ; ------------------------------------------- ; Your MAIN code goes here ; ------------------------------------------- Call LibMain ; Replace this with your own code call mp3xit ; Exit to DOS main endp cseg ends end main