CS306 Processing Systems and Structures J. W. Lockwood

Assigned Monday, 1/14/2002
Due Date Tuesday, 1/22/2002
Purpose Introduction
Points 25

Introduction

This machine problem will introduce you to the computers, network, and software in the CS306 laboratory. For this machine problem, you will learn how to edit, assemble, debug, and execute an existing, simple assembly language program. A full listing of the program and a Makefile are given at the end of this assignment. Electronic copies of the program are available on-line as mp0.zip.

Preliminary Procedure

  1. In general, you may write your machine problems from home or in the CS306 programming laboratory in Lapota 408. In order to turn in your machine problem, you must visit the CS306 laboratory during the hours that a TA is available.

  2. If your do not already have the NASM and Turbo debugger tools installed on your machine, follow the directions to install NASM/Turbo software for CS306, as described in the Resources section of the CS306 homepage.

  3. Download and extract the MP0 files

  4. Start a COMMAND shell and locate your files.

  5. Edit the program with your favorite text editor.

  6. From the COMMAND window, Assemble and link MP0 by typing make. This command will read Makefile and invoke the following commands:
    nasm -g -f obj -o mp0.obj mp0.asm -l mp0.lst
    tlink /c /v mp0.obj, mp0.exe, mp0.map, lib306.lib
    At this point, you should have just created the executable program called MP0.EXE.

  7. Run the program by just typing its name, MP0
    Try giving the program different input. Remember: this program is looking for upper-case letters!

  8. Now examine your program by running the debugging tool Turbo Debugger (TD).
    TD MP0
    Turbo Debugger will read your MP0.EXE program and allow you to step and trace through the program as it executes.

  9. Now return to your text editor to modify mp0.asm. You need to make the following changes:

    1. Put your name and the current date at the beginning of the program
    2. Add a feature to the program that prints a message of your choice when the grade C is selected.
    3. Assemble and test your program with all possible inputs. Debug with TD as necessary until the program works as expected.

  10. Now return to your text editor and add a line to mp0.asm.

    1. Add a new variable to your program called mystery that is initialized with the hex values of 0FEh,0C0h. (This is NASM's method of entering hex values). Declare this variable in your code at the location immediately after the line where lib306's kbdine function is called."
    2. Re-Assemble your code and run TD.
    3. Run your program a few more times and observe what happens. Try Switching between the Module (source) window and CPU (disassembly) windows.
    4. Browse the CPU window record what addresses the mystery bytes were stored in.
      Address= ______ : ______
    5. In the space below, explain how the program behaves differently and why.
    6. Finally, Move this variable declaration to where it belongs in the code.

Final Steps

  1. Demonstrate your updated MP0.EXE to a TA or to the instructor. Review your solutions to the question with the TA. Be prepared to be quizzed by the TA with additional questions about your TD debugging experiences. The TA will not allow you to submit your MP until he or she is satisified that you have mastered TD.

  2. Have the TA handin your MP online.

  3. Log off by clicking on the start button and navigating to Shutdown and selecting Log off.

  4. You are now finished!

MP0.ASM

; MP0 - Your Name - Today's Date ; ; This program illustrates a (very) basic assembly program and ; the use of LIB306 input and output routines. ; By working on this code, you will have the opportunity to ; exercise the tools for this class, namely the editor, ; the Assembler (NASM), and the debugger (TD). ; Be sure to put your name in the places where it says 'Your Name' ; and also change the date where it says 'Today's Date'. ; The changes that you need to make to this program are ; described in the MP0 assignment page. BITS 16 ;====== SECTION 1: Define constants ======================================= CR EQU 0Dh LF EQU 0Ah BEL EQU 07h ;====== SECTION 2: Declare external procedures ============================ EXTERN kbdine, dspout, dspmsg, dosxit ;====== SECTION 3: Define stack segment =================================== SEGMENT stkseg STACK ; *** STACK SEGMENT *** resb 64*8 ; Reserve 64*8=512 bytes of memory stacktop: ;====== SECTION 4: Define code segment ==================================== SEGMENT code ; *** CODE SEGMENT *** ;====== SECTION 5: Declare variables for main procedure =================== mygrade db 0 question db 'What grade would you like in CS306? ','$' Exitmsg db CR,LF,'Good Luck!',CR,LF,'$' invalid db CR,LF,'Not a valid choice! ',CR,LF,'$' Amsg db CR,LF,'Learn all material and Submit MPs early.',CR,LF,'$' Bmsg db CR,LF,'Keep up in class and submit MPs on time.',CR,LF,'$' Dmsg db CR,LF,'Skip a few machine problems.',CR,LF,'$' Fmsg db CR,LF,'Sleep through exams.',CR,LF,'$' ;====== SECTION 6: Program initialization ================================= ..start: mov ax, cs ; Initialize Default Segment register mov ds, ax mov ax, stkseg ; Initialize Stack Segment register mov ss, ax mov sp, stacktop ; Initialize Stack Pointer register ;====== SECTION 7: Main procedure ========================================= MAIN: mov dx, question ; Prompt user with the grade question call dspmsg call kbdine mov [mygrade],AL ; Save result .CheckGrade: cmp byte [mygrade], 'A' ; Check if A student jne .NotGradeA mov dx, Amsg ; Print message for A students call dspmsg jmp .mpExit .NotGradeA: cmp byte [mygrade], 'B' ; Check if B student jne .NotGradeB mov dx, Bmsg ; Print message for B students call dspmsg jmp .mpExit .NotGradeB: cmp byte [mygrade], 'D' ; Check if D student jne .NotGradeD mov dx, Dmsg ; Print message for D students call dspmsg jmp .mpExit .NotGradeD: cmp byte [mygrade], 'F' ; Check if F student jne .NotGradeF mov dx, Fmsg ; Print message for F students call dspmsg jmp .mpExit .NotGradeF: mov dl, BEL ; Ring the bell if other character call dspout mov dx, invalid ; Print invalid message call dspmsg jmp .FinalExit .mpExit: mov dx, Exitmsg ; Type out exit message call dspmsg .FinalExit: call dosxit ; Exit to DOS

Makefile

MPNAME=mp0 all: $(MPNAME).exe clean: rm -f $(MPNAME).obj $(MPNAME).exe $(MPNAME).lst $(MPNAME).map %.exe: %.obj tlink /c /v $<, $*.exe, $*.map, LIB306.LIB %.obj: %.asm nasm -g -f obj -o $*.obj $< -l $*.lst