CS 130-0 - Assignment #1
Becoming familiar with MASM and Code View

Drake University
Fall, 2005


Due date: 8/31




Purpose

The purpose of this first assignment is to install and become somewhat familiar with the "MASM" assembler, "Code View" debugger and "Text Pad" editor that are on the CD that came with your textbook. You will get a taste of what happens internally as a machine language program executes by stepping through such a program using the debugger.

You will need to have access to an Intel-based computer running Windows 98 or NT or 2000 or Millenium or XP, on which you can install these things, and which will be required for the duration of the course. If this presents a problem for you, then we need to discuss it and find a solution right away.



What to submit

For this assignment, you only need to submit written answers, in your own words, to the questions that are asked at the end of these introductory exercises.



The assignment activities

As mentioned, you will need to install software for this course, and learn a little bit about using it. These activities are somewhat special in the context of this course, because most of what follows in later assignments will depend on using this software. It is therefore essential that together we manage to install the software and get you acquainted with it. Here are the activities for this assignment:
  1. Using "My Computer", open the root directory for the CD. Double click the bluish "Setup" icon to install the MASM 6.1.5 assembler. Use the defaults when installing this assembler and supporting software.

  2. Open the "TextPad" folder and double-click the bluish "TextPad4" icon to install the TextPad editor. Again, use the defaults.

  3. Although it is optional, I recommend putting the following shortcuts on your desktop:

    • A shortcut to the MS-DOS Prompt (Command) Window. To get this, find the MS-DOS Prompt by clicking "Start" and selecting "Programs" and looking around for a blue and red and yellow icon. When you find it, highlight it, and right-click on it, and then select "Create Shortcut". After the shortcut icon is created, drag it to the desktop.

    • A shortcut to the TextPad editor. You can find this program using "My Computer". Step into the "Program Files" folder, and then the "Text Pad 4" folder. Right-click on the big-T icon to create a shortcut to this. Drag the shortcut icon to the desktop.

    • A shortcut to the "runQH" batch file. You can find the icon for this in the "Masm615" folder off of your hard drive's root directory. Right-click on this icon, and proceed as before. (This will give you access to some "help" documentation concerning MASM and Code View that might come in handy some time.)

  4. Use the TextPad editor to open the "batch file" New-vars.bat in the directory Masm615. Change each occurrence of MASM611 in this file to MASM615. Also add the line
     SET LIB=C:\MASM615\LIB;%LIB% 
    Then save this.

  5. Start the MS-DOS Prompt program. (You can also start it by clicking Start, selecting Run, typing in command, and clicking OK.) If you are unfamiliar with the MS-DOS Prompt program, this will allow you to enter commands, somewhat similar to the commands you are used to using in a Linux environment. The cd command works very much like cd in Linux. When you start up (assuming you haven't changed the startup info), you will either be in your hard drive's root directory or in the "Windows" directory. You can tell from the prompt. If necessary, give the command
    cd ..
    in order to get to the root directory. Then give the command
    cd masm615
    to step into the Masm615 directory. Then do a
    dir
    to see a listing of the directory. This listing won't fit in your screen, so now try
    dir | more
    instead, to view it a screen's worth at a time.

    Give the command
    type new-vars.bat
    to see the contents of the batch file you altered earlier. Then type the command
    new-vars
    to execute this batch file, and so execute all of the system commands contained in it. This will cause some system variables to be altered, so that the operating system will be able to find necessary files. Next give the command
    make32
    which will cause the operating system to execute the batch file make32.bat, although it will complain about improper usage. Never mind that. For technical reasons that elude me, make32 doesn't always work from inside a different directory, unless you first execute it from inside Masm615. Therefore, execute make32 from this directory whenever you begin a session that will use make32. Ditto for make16.

  6. Now give the commands
    cd examples
    cd ch06
    to step into a directory containing sample assembly language programs from the textbook. Do a
    dir
    here too. Then give the command
    make32 encrypt
    to assemble and link an assembly language program called encrypt. You should experience a nice clean assembling and linking of the code, without error messages. Assuming this, now execute this program by typing
    encrypt
    When it runs, type in a short sentence and see what happens.

    Now, use the TextPad editor to view the source code for this program, which is in the file Encrypt.asm. You are not expected to understand much of what you see here, at this point, but there will be a couple questions about this program later.

  7. Back up to the Masm615 directory (using cd). Then create a new directory off of this by giving the command
    mkdir Projects
    (you may name it whatever you wish though). Then use cd to step into this directory. Now, save the assembly language program at the link mysums.asm in a file named mysums.asm in your Projects directory. Use the dir command to make sure this worked OK. Make sure that it is mysums.asm and not mysums.asm.txt or something else. Now assemble and link this by giving the (case-sensitive) command
    ml -Zi mysums.asm
    Then enter the on-line debugger (Code View) with mysums loaded by giving the command
    cv mysums
    Use the Windows menu option to make sure that you have "Source 1", "Memory 1", "Register" and "Watch" windows open. Also, close any other window (like "Command") by highlighting the window and then clicking its upper left corner. When you have the correct four windows open, select "Windows/Arrange" to organize these nicely.

    Now use "Data/Add Watch" and enter I where prompted for an expression. You should now see this variable and its initial value in the watch window. Do likewise for the other two variables: SUM, SUM2.

    We will now use the debugger to step through this program one instruction at a time. To execute the currently highlighted instruction, press the F8 key. Do this twice to execute the first two instructions. After the MOV DS,AX instruction has been executed, notice that the DS (data segment) register has probably been changed. Whatever value it currently contains (four hex digits), go down to the memory1 window and type these four hex digits (for example, 1A20, in place of the first four hex digits that appear there. This will change the whole display to reflect the contents of the corresponding portion of memory. Each of the pairs of hex digits you see gives the one-byte contents of some memory location. After a few seemingly random bytes, you should see
    01 00 02 00 03 00
    These six bytes of memory are used to store the three variables I, SUM, SUM2 and you can observe them change as you observe the values of the variables change in the watch window. There is a difference though. In the watch window the values are displayed using decimal notation, but in the memory1 and in the registers windows, information is always displayed using hexidecimal. As an experiment, change the above 01 to 11. Remember that this is hex 11. Now go look at the watch window and see what has happened to the variable I.

    SLOWLY use F8 to step through the program one instruction at a time. With each step, record any changes you notice in the watch, memory1 and registers windows. See what you can figure out about this.




Questions

  1. The "include statement" at the top of encrypt.asm tells the assembler to include the source code from a file named Irvine32.inc while assembling the program. However, this code is in a file in a different directory, namely \Masm615\Include. How does the operating system know to look in this directory to find the file?

  2. The assembly language call instructions is used to "call a subroutine". This is essentially the same idea as calling a function (method) in a high level programming language like C++ or Java. Notice that the assembly language program in encrypt.asm begins execution in main, which is blocked off between the two markers main PROC and main ENDP. Inside here there are a number of subroutine calls, and the code for these are found further down in the file. Note that the assembly language instruction ret is equivalent to return in C++ and Java, and forces a return from a subroutine call. Also, notice that the subroutines here call other subroutines, such as InputTheString calling ReadString, but that the source code for these other subroutines is not found in the file encrypt.asm. Based on what you see in encrypt.asm, describe the flow of the program, carefully explaining which subroutine calls which subroutine. For example, to begin with, main calls InputTheString, which in turn calls WriteString. After returning from WriteString, InputTheString then goes on to call ReadString. Etcetera. (Don't worry about finding the source code for ReadString and WriteString, and hence don't worry about any calls that these might make.)

  3. Consider the program mysums now. What exactly happens when the instruction
    MOV I, 0
    is executed? Also, what exactly happens when the instruction
    ADD SUM, AX
    is executed?

  4. What exactly happens when the instruction
    LOOP LOOP1
    is executed? (Hint: pay attention to the CX register.)

  5. Describe carefully what the overall effect of this program is. How would it change if N was changed to 10 instead of 5? Try it.