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

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

## HOMEWORK 3
Due: Wed November 5, 1997

### INTRODUCTION

This assignment requires you to write procedures, and gives you some exercise in manipulating strings. For the latter, you need to read chapter 11. Basically, you have to write a procedure to sort a sequence of strings. For instance, if the input strings are

    	'abc', 'hey', 'a', 'heee'

Then the sorted output (in increasing order) is the sequence

    	'a', 'abc', 'heee', 'hey'.

There is a sorting procedure called SELECT in the textbook (section 10.3, page 189). However, procedure SELECT sorts an array of numbers. We want to modify SELECT so that it sorts an array of strings.

### DETAILS

1. First of all, how do you represent strings? We will use the convention in exercise 11, chap.11 (p.227) for representing strings. First define

       	MAX_LEN EQU	20

   Your program should work in case we replace ``20'' by any other value. Assume our strings are at most MAX_LEN characters long, and the end of a string is indicated by storing a carriage return (CR or 0Dh). So the actual length of a string is a number between 0 and MAX_LEN. Note that the CR is not counted in the actual length. In addition to strong CR, we also need two other bytes: the first byte stores the number MAX_LEN, and the second byte stores the actual length of the string. So we need to reserve MAX_LEN+3 bytes for each string.
2. Next you need a procedure to compare two strings. Exercise 10 (chap.11, p.227) describes such a procedure. Please implement this procedure, and call it COMP. Note that the string of length 0 is smaller than any other strings, and COMP must be able to handle this case. The individual characters of a string are compared by using their ASCII code. E.g.,

          '!' < '&' < '0' < '1' < 'A' < 'Z' < 'a' < 'b' < '{'

   You may assume only printable ASCII codes are in a string and CR is not in the string. COMP must be general, i.e., it must not assume that the two strings to be compared have length MAX_LEN or that they have the same maximum length. Assume that COMP returns a value of -1 (if less than), 0 (if equal) and +1 (if greater than).
3. Next, adapt the procedure SELECT so that it calls procedure COMP instead of using the machine instruction CMP. Call your sorting procedure MYSORT. You should retain the conventions and logic of procedure SELECT as much as possible. In particular, the input and output conventions of MYSORT should be those of SELECT (appropriately modified, of course). Note that SELECT calls a SWAP procedure. You also need to modify SWAP.
4. The overall program should have the title of ``TEST MYSORT'' and it should be an adaptation of PGM10_3.ASM (p.191). The data array A in this program should be initialized to contain the following 16 strings:

       	'are',	'at',	'as',	'by',	'but',	'be',
       	'91',	'Johnson',	'ASCII', 'USA',	'New York',
       	'John',	'a1',	'Number One',	'!!!', 'yap@cs.nyu.edu'

   You must write a procedure called PRINT which outputs the strings in array A. Make sure that you call PRINT before and after sorting in the main program. NOTE: You can adapt a very similar procedure found in p.212 of the textbook (PGM 11_2.asm).
5. This assignment is worth 40 points. Here is the breakdown of these points.
   - If your program executes correctly, you automatically get 15 points. If it does not assemble, you get nothing out of these 15 points. So make sure you get rid of all syntax errors!
   - We give up to 5 points for useful and good comments (including specifying the input and output before of each procedure).
   - We give up to 5 points for elegant design of your program. NOTE: usually, elegant programs are also small and compact.
   - Up to 5 points for use of the string instructions (found in chapter 11).
   - Up to 7 points for proper packaging of your homework (details below).
   - 3 discretionary points.

### TO BE HANDED IN

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

       	@rem debug batch file
       	echo DEBUGGING HW3
       	tasm /z /zi hw3
       	tlink /v hw3
       	td hw3
       	echo DONE DEBUGGING HW3

   REASON: we want to execute db.bat to get quickly into the turbo debugger to examine your sorting program. [3 pts]
2. A printout of your program hw3.asm. [2 pts]
3. Hand in everything in an envelop (prefer smaller envelops). Please write your name, ID, ``HW3'', and class information on the envelop and also on the diskette. Please DO NOT use any of the NYU envelops. [2 pts]

### APPENDIX (subsequent information)

1. Apparently (as demonstrated in class today), there is some descrepancy between int 21h, function 10 as described in the book (exercise 11, p.227) and what happens on my machine (and presumably yours as well). The difference is minor -- if the MAX_LEN of a a string is 20, say, then my machine allows only up to 19 characters (not counting the final CR character). In the following, I will follow the book's assumption. (This does no harm for my machine -- it only wastes one byte per string, since the last byte will never be used.)
   A slight elaboration of the program I demo'ed in class is available as readstring.asm from the sample programs directory here.
2. Several of you wondered how to enter the strings into your program. Here is how I would do it: to enter the strings

               'are', 'at, 'zzzz', etc,

   into the array A, my data segment will look like this:

               .data
               A db    MAX_LEN,3,'are',CR,MAX_LEN-3 dup (?)
                 db    MAX_LEN,2,'at',CR,MAX_LEN-2 dup (?)
                 db    MAX_LEN,4,'zzzz',CR,MAX_LEN-4 dup (?)
                 ... etc

3. In your diskette, please do not leave unnecessary files around. If you have such files, I suggest you create a subdirectory called "old" and move them into old. E.g., hw1.asm should be there.
4. Some students pointed out that when they call their mysort program, the printout on the screen just flashes by quickly and one cannot see what is printed out first. There are three solutions to this:
   - You can insert some "pause" instructions to slow down the output. E.g., print a string "PLEASE TYPE ANY KEY" and then call function 1, int 21h (to read the key input, which is of course discarded).
   - You can redirect the output of your program into a file. E.g.,

         		:> mysort > outfile

     will put the output of mysort into the file "outfile".
   - You can "pipe" the output through the "more" program:

         		:> mysort | more

     and this will pause when the output fills one screenful. You need to type a space for it to continue.
   NOTE: if you are familiar with UNIX, solutions (2) and (3) are standard paradigms in UNIX.