CS 2011, C Term 2001: Lab 1

                                                                                Name: __________________

                                                                            Section:_______________

Objective

The goal of this lab is to help you become acquainted with the Debug utility. You will need to understand how to use Debug to do your second homework assignment. You will your copy of Irvine for help with the debug instructions!

If you don't understand all the concepts used in this lab, that's ok. You will learn more about registers, data, offsets, byte swapping, and machine code in the machine code lecture and you'll get lots of practice with these concepts when doing Homework 2. You should still feel free to ask the TAs questions during lab though.

Instructions

  1. Open a DOS window on your PC. At the DOS prompt, type "debug" to start up the debug utility.

  2. The register command is used to initialize the registers used by your assembly program. Registers are high-speed memory locations used to hold data (and in some cases, commands). Use the following commands to set up your program:

    1. Set up the data segment using "r DS" and entering a new value for DS, the Data Segment register . Set it to equal 124Ch (the previous DS value shown in the example is 12E6, you may see a different number).

      r ds
      DS 12E6
      :124c

    2. Set up the code segment using "r CS" and entering a new value for CS, the Code Segment register, of 1774h.

      r cs
      CS 12E6
      :1774

    3. Set up the location within the code segment where your program will be using "r IP" and entering a new value for IP, the Instruction Pointer register, of Ch.

      r ip
      IP 0100
      :c

  3. If you want to see the values for all the registers, use the "r" command without giving a register name. Try it out! You should see that DS, CS, and IP have the new values you just gave them.

  4. Enter some data for your program. We want a sixteen bit value equal to 1234h. It's going to be stored at the location offset from the start of the data segment by 4 bytes (a byte is a 8 bit value). Addresses are specified in a Segment:Offset format where the segment is the base of the address and the offset is the value that needs to be added to it.  Since DS points to the start of the data segment, the location four bytes from that is specified by DS:4. After entering 34, hit the space, then enter 12 and hit return.

    e ds:4
    124C:0004 00.34 00.12 

    You'll notice that to have 1234, you enter the 34, then the 12. This is called byte swapping. If the 16 bit value is 1234 then it is entered 8 bits (one byte) at a time - first the low byte, then the high byte.

  5. Lets display back the data you just entered:

    d ds:4

    Can you see where the data shows 34  12? The left-most column gives the segment offset - 124C:0000 in the top row (recall, DS = 124C). If you count over five spaces (start with zero), you should see the 34 12 for DS:0004 and DS:0005.

  6. Now, we're going to enter a couple of machine code instructions. Machine code is entered just like data except that instead of putting it in the data segment (by using DS) you're going to put it in the code segment (by using CS).  Use the following commands:

    e cs:c
    1774:000C 00.bb 00.04 00.00 00.8b 
    1774:0010 00.07

  7. You can use the "u" command to decode memory into assembly instructions. Use it to look at the two instructions you just entered:

    u cs:c

    What are the first two instructions?

    Instruction 1: ____________________

    Instruction 2: ____________________

    They should both be MOV instructions. The first one moves 4 into the BX register. The second one moves the data "pointed to" by the BX register into the AX register (the [BX] means that you are using data "pointed to" by BX).

  8. Use the "r" command to look at the registers again. It will show the registers and the assembly code for the command you are about to execute. You should see something like this:

    1774:000C  BB0400          MOV  BX, 0004

    The BB0400 is the machine code.  The "MOV BX, 0004" is the assembly code. In debug, all numbers are displayed in hexadecimal. 

    Now, use the "t" command to trace through your program. After hitting "t", you will see the registers displayed again, just like when you did the register command.  

    What are the IP, AX, and BX register values after the first "t" command?

    IP = ___________
    AX = __________
    BX = ___________

    What are the IP, AX, and BX register values after the second "t" command?

    IP = ___________
    AX = __________
    BX = ___________


    You should notice a couple of things:

    -- After each instruction, IP increments by the number of bytes of machine code that were just executed. MOV BX, 0004 has machine code BB0400. That is three bytes long (a byte is 8 bits). Prior to executing the instruction, IP was equal to C (12 decimal). After adding 3, IP should be equal to F (15 decimal).

    -- When BX equaled 4 and you copied the value pointed to it into AX, the value in AX was the 1234h you entered earlier. The bytes were swapped when it was copied out of memory into the value we wanted!

  9. Quit debug using the "q" command.

  10. Turn in this sheet of paper. Don't forget your name and section number!