Homework 4: V22.0201.002, Machine Org I, Fall 1997, Chee Yap

### Fall 1997, Professor Yap
V22.0201.002, Machine Organization I

## HOMEWORK 4
Due: Wed November 26, 1997

### INTRODUCTION

This homework is based on chapters 12 and 13. It is in two parts, but the two parts are not necessarily related. PART A tests your knowledge of assembly syntax and simple use of the debugger. PART B requires some video text mode programming. You will also be asked to use MACROS and INCLUDE statements to organize your program.

I mentioned in class on Monday that I was hesitating between requiring you to hand in this homework or not. But I feel strongly enough about the usefulness of this exercise for the final project that I decided that you have to hand it in. Two concessions are (i) I will give you 2 weeks to do this, and (ii) you are to do this in your PROJECT TEAMS. I suggest that each team immediately schedule a FIRST meeting. If you delay this you are just taking up time that you can use for your final project!

### PART A: ASSEMBLY PROGRAMMING KNOWLEDGE

Please answer the following questions. The answers may not be found in the textbook. The recommended way to answer some of these questions is to write trivial programs using these instructions, and assemble them to find out what happens. If necessary, go into the debugger for more information.

1. Which of the following instructions are legal?

       	(a) mov ax, WORD PTR bh
       	(b) mov BYTE PTR [di], bh
       	(c) REP mov ax,bx
       	(d) mov es,ds
       	(e) mov ah,[ByteVar+3]
       	(f) mov si,[si]
       	(g) mov sp,ax
       	(h) mov sp,WordVar

2. How many bytes are used in the machine code for CMPSW?
3. You are using TD to debug a program that displays something on the screen. TD has just executed the code that does the display function (e.g., int 21h, function 2). Without leaving TD, how do you see what is actually displayed? (You need to get out of the debugger windows somehow.) And, how do you return to TD?
4. What is wrong with the following macro definition? Further, please say what the assembler error message is.

       		newmac macro n
       		mov ax,n
       		newmac endm

### PART B: PROGRAMMING

1. MACROS. Please collect the following macros from the book (chapter 13) into a file called mymacros.lib (lib for ``library''). You may add more of your own if you like (but be sure to comment them properly so that we can understand what these macros do!).

       	DOS_RTN, NEW_LINE, DISP_STR, SAVE_REGS, RESTORE_REGS, HEX_OUT

   In the future (e.g., in the game project), please use these macros whenever possible. You can include the macros in your file using the form suggested by the book:

       	IF1
       		INCLUDE MYMACROS.LIB
       	ENDIF

   Whenever you invoke a macro, please add comments saying that you are invoking one. E.g.

       	DOS_RTN		; invoke macro in mymacros.lib

2. MORE EDITOR FEATURES. The program PGM12_3.asm in chapter 12 of the text has the outlines of a very simple editor. It was demostrated in class, and you can even pick up a copy from our course homepage! Your assignment is to modify this editor to add some more features.
   - INSERTION MODES. The ``insertion mode'' in PGM12_3 may be called the REPLACE MODE, that is, the charaters you type will replace the characters already on the screen. We want you to implement another kind of insertion mode, called the APPEND MODE. In the APPEND MODE, the characters you type are INSERTED into the current position. That means each character to the RIGHT of the current position in the current line must move one position to the right. E.g., if the cursor is at the letter ``c'' in the following line,

         		____abcd__12345______WXYZ_____

     then after you type ``HERE'', the new line will become

         		____abHEREcd__12345______WXYZ_

     and the cursor would still be at the letter ``c''. NOTE: the underscores (

         ___

     ) signify blanks.

     More precisely, consider what happens when you insert a single character (inserting many characters just repeats this procedure): of course, each character to the right of the current cursor position moves one position to the right. The interesting question is what to do with the last character. We have two possibilities:
     (1) if the last character is a blank, then it simply disappears and the insertion process ends.
     (2) if the last character is any non-blank character, it will be inserted into the beginning of the next row. Of course, this may have a cascading effect and cause further insertions into the row after next, etc. If this causes an insertion into the row beyond the last row (row 24) then we create a new blank row by scrolling the screen up by one row (the contents of the previous row 0 is lost).

     E.g., in the above example, suppose that there are two blank spaces after ``WXYZ'' before the end of line. Then inserting ``HERE'' in the example will cause (in turn) the insertion of ``YZ'' into the beginning of the next line.

   - TOGGLING BETWEEN THE TWO MODES. At any moment, we are in the REPLACE or the APPEND MODE. We want you to toggle between the two modes whenever the special key INS is typed (INS or ``INSERT KEY'') has scan code of 82 (or 52h), as seen in table 12.7 (p.245).
   - MARKING THE CURRENT POSITION. We also want to be able to ``mark'' the current position whenever the user types the function key F1 (=scan code 3Bh; see table H.1, p.528). We can have only one marked position at a time. So any previously marked position is forgotten after you type F1. Initially, the position (0,0) is marked.
   - JUMPING TO THE MARKED POSITION. The user can jump to the marked position by typing the function key F2 (=scan code 3Ch).
   - NOT REQUIRED FOR THIS HW BUT WOULD BE NICE TO DO:
     (i) Implement the ability to DELETE a character (the opposite of the APPEND MODE).
     (ii) Write the contents of the current screen into a file (called myfile.txt) whenever the user types F3. To do this, you need to do some simple file I/O. See chapter 19 for some INT 21h functions for this purpose.
     (iii) Files I/O is also useful if your editor wants to keep track of information that is larger than one screenful (so when you scroll up or scroll down, nothing is really lost).
   - HOW TO MODIFY PGM12_3.asm. We suggest that you retain the overall logic of the original program as much as possible. Any new functionality should be implemented by defining new procedures and using function calls. We further suggest that you debug each new function thoroughly before putting them all together!

### GRADING

- This assignment is worth 65 points. PLEASE HAND IN ONLY ONE SOLUTION SET PER TEAM. Part A is worth 20 points. Part B is worth 45 points, broken down as follows.
  - If your program assembles and executes correctly, you automatically get 20 points. If it does not assemble, you get nothing out of these 20 points. So make sure you get rid of all syntax errors!
  - Up to 5 points for using including MYMACROS.LIB and using macros the there whenever possible.
  - Up 5 points for placing useful and good comments (including specifying the input and output before of each procedure and macro).
  - Up to 5 points for elegant design of your program. NOTE: usually, elegant programs are also small and compact.
  - Up to 6 points for proper packaging of your homework (details below).
  - We will give you 2 points if you break up your program into two separate modules, hw4.asm (containing the main procedure) and hw4a.asm (containing the additional procedures). See section 14.2 for how to do this (you need to use the EXTRN and PUBLIC pseudo instructions). HINT: You will find this skill useful in the final project. So why not learn it now?
  - 2 discretionary points.

### TO BE HANDED IN

1. A 3.5'' disk containing your program file (named hw4.asm), an executable (named myeditor.exe), and a bat file called db.bat. The db.bat file contains the following lines

       	@rem debug batch file
       	echo DEBUGGING HW4
       	tasm /z /zi hw4
       	tlink /v hw4
       	td hw4
       	echo DONE DEBUGGING HW4

   If you have two modules (hw4.asm and hw4a.asm) then these instructions must be modified appropriately. [2 pts]
2. A printout of your files mymacros.lib and hw4.asm (and possibly hw4a.asm). [2 pts]
3. Hand in everything in an envelop (prefer smaller envelops). Please write the following information on the envelop, and also on the diskette:
   (a) your names, IDs and email addresses for each team member.
   (b) ``HW3'',
   (c) class information (``V22.0201.002'').
   Please DO NOT use any of the official NYU envelops. [2 pts]