EEL 3801

Introduction to Computer Engineering

Summer 1999

LABORATORY MANUAL


Schedule of Assignments:

Lab # Week Due Points Title

1 Next lab 1% The tasm and tlink commands

2 Next lab 2% Hello World in Reverse

3 Next lab 2% Bubble Sort

4 Next lab 1% A Simple Project Using Borland C++

5 Next lab 2% Review of the C language Part 1

6 Cancelled 0% Review of the C language Part 2

7 Next lab 2% Review of the C language Part 3

8 Next lab 2% Objects and Classes

9 Next lab 3% Constructors, Destructors and Friend Functions

10 Cancelled 0% Inheritance

11 Cancelled 0% Inheritance and Constructor Functions

All labs must include the following information. If a lab has more than one part then each part must include this information.

Lab number

Name

Short description of the work done.

Source code with comments.

Program output.

Notes:

· Comments on the source code are to explain what the code does. This will help the lab instructor (TA) as well as your self understand how your code works and what each function does. Each function must have comments indicating exactly what that function does.

· Labs are due at the BEGINING of the lab period for the lab that you are attending on the week it is due. If a lab is late you must make arrangements with the lab instructor to turn in the lab.

· Labs are to be turned in to the lab instructor (TA). DO NOT turn in labs to the course instructor unless you have made special arrangements with the instructor.


Laboratory Assignment #1

EEL 3801 Introduction to Computer Engineering

Topic: Familiarization with the programming environment.

Due: Same day of lab

Turn in: Source code with output.

The tasm and tlink commands.

Each student needs an account to get access to the PC-LAN. If anybody doesn't have one, please see Mr. David Douglas at room Engr 465. Also ask for an electronic key if you need to get into the lab at other time.

Login into the computer by type stulogin login-name under F: prompt, and set to the c:\ prompt. (If under Windows NT, then just type your name and password, and also set to c:\ prompt under dos.)

At the directive c:\tasm, type mouse to activate mouse for DOS application. Also use DOS editor by type edit under c:\tasm to create the source file. Just type in the code bellow and save the file use .asm extension like hello.asm.

Type tasm file-name (hello.asm) to assemble the source code (remember under prompt c:\tasm). If there were no errors in the statement, then type tlink file-name (hello or hello.obj) to link the object file into executable file, otherwise you need to go back to see whether you have made any mistakes and reassemble the source code. If you pass to link the object file, and then you just need to run the program to make sure the code works. Type file-name (hello), you will see Hello World! on the screen.

title Hello World Program (hello.asm)

; This program displays "Hello, world!"

dosseg

.model small

.stack 100h

.code

main proc

mov ax,@data

mov ds,ax

mov ax,0900h

mov dx,offset msg

int 21h

mov ax,4C00h

int 21h

main endp

.data

msg db 'Hello World!',0Ah,0Dh,'$'

end main


Laboratory Assignment #2

EEL 3801 Introduction to Computer Engineering

Topic: Simple looping and stack usage in assembly language.

Due: Beginning of next week's lab time.

Turn in: Source code with output.

Hello World in Reverse

Part 1. Use the sample program from lab #1 as the base; print out the following on the screen:

Hello World!
!dlroW olleH

The data segment should be the following:

.data
msg db 'Hello World!', 0dh, 0ah, '$'
len db $-msg-3 ; len is the length of the 'Hello World!' string

The algorithm of the program:

set ds register

output Hello World! on the screen

while (not the end of the string)

push the char into the stack

while (stack is not empty)

pop out one char to bx register

output the char in bx to the screen


The push and pop instruction inserts and removed data from the stack:

push [bx] ; push the contents of memory location whose address is in bx.

pop bx ; pop the top element in the stack into register bx.


Part 2. Use the following data segment:

.data
startnumb db '1', 0dh, 0ah, '$' ; the start number
endnumb db '9', 0dh, 0ah, '$' ; the stop number
incrnumb db 2, '$' ; the increment number

Write a program to print the series of numbers on the screen, beginning with startnumb, increment by incrnumb each time, until you reach endnumb. As a result you should print out:

1
3
5
7
9

on the screen.

The instruction for compare two values:

cmp a, b ; at least one of the number should be register
jle label ; jump to label if a less or equal than b


Laboratory Assignment #3

EEL 3801 Introduction to Computer Engineering

Topic: Nested looping in assembly language.

Due: Beginning of next week's lab time.

Turn in: Source code with output.

Bubble Sort

Write a program using the bubble sort algorithm to sort the string stored in the data segment. Print out the string on screen before and after the sort.

The implementation of the bubble sort consists of a simple double loop. The first iteration of the
inner for loop moves through the record away from bottom to top, comparing adjacent keys. If the lower indexed key's value is greater than its higher-indexed neighbor, then the two values are swapped. Once the smallest value is encountered, this process will cause it to "bubble" up to the top of the array. The second iteration of the inner for loop repeats this process. Since the smallest value reached the top of array on the first pass, there is no need to compare the top two elements on the second pass. Likewise, each succeeding pass through the array compares adjacent elements, looking at one less value than the preceding pass.

The bubble sort algorithm:

For i = string_length - 2 down to 0

for j = 0 to i

if string[j] < string[j + 1] then

swap string[j] with string[j + 1];


Laboratory Assignment #4

EEL 3801 Introduction to Computer Engineering

Topic: Familiarization with the Borland C++ compiler.

Due: Beginning of next week's lab time.

Turn in: Source code with output.

A Simple Project Using Borland C++

A "project' is a program file organization which allows the software engineer to more easily

manage the program and minimize re-compilation when changes are made. Although there are

many different ways to implement projects, in this lab we will cover the most basic

configuration. In a typical C++ project, the code is divided into 3 main sections: The header file,

a node that contains member functions, and the main node.

1) Getting Started:

First a new project must be created. To do that choose Project New Project from the project menu. When asked for a target type choose "Easy Win". Since we are not creating a windows application, a DLL file etc., we'll go with the simplest configuration (Which is "Easy Win") . We could have also created a project for DOS but we'll select windows for its simplicity. After the project is created, delete the flies with .RC and DEF extensions using the right mouse button. A .DEF file contains linking information for windows applications, an .RC file contains information about the graphical interface of the windows application. Since we're not going to build a windows application , we do not need these files.

2) The Header File:

This part of the project includes class declarations. A class is a data structure in Object Oriented Programming which associates attribute / value pairs and functionality which are related to the concept represented by the class. However, as we have not yet learned C++, we will instead implement classes as C structures. Although it is possible to include member functions or body of the functions in the header file, this is not common practice. Put the code below in a header file.

//list of header files

#include <iostream.h>

#include <iomanip.h>

#include <string.h>

#include <stdio.h>

#include <alloc.h>

// first structure definition

typedef struct Auto

{

int auto_quant; // number on hand

double auto_price; // price per unit

char *auto_descrip; //description of item

char *auto_manufacturer; //description of manufacturer

} Auto_type;

// function prototypes

void input_Auto(char*, char*, int, double);

void print_Auto(void);

int get_quant_Auto(void);

double get_price_Auto(void);

char *get_manufacturer_Auto(void);

char *get_description_Auto(void);

3) Member Functions:

In most C++ projects member functions are contained in a separate node. In our case the bodies of the functions will be included here. Include the following piece of code in a separate .CPP file. You must also include the new header file in double quotes. See the code below as an example. The header file that was used was named "tl.h". Name the file below anything other than the project name with .CPP extension. Then use the right mouse button to add this file to the project.

#include "t1.h"

//Global data types

Auto_type car;

//Function Bodies

void print_Auto(void)

{

printf("Auto description %s \n", get_description_Auto());

printf("Auto vendor %s \n", get_manufacturer_Auto());

printf("Auto quantity %d \n",get_quant_Auto());

printf("Auto price %f \n" ,get_price_Auto());

}

void input_Auto(char* cd, char* cv, int qa, double pa)

{

car.auto_descrip = (char*)calloc( 1 ,sizeof(cd));

strcpy(car.auto_descrip,cd);

car.auto_manufacturer = (char*)calloc(1 ,sizeof(cv));

strcpy(car.auto_manufacturer,cv);

car.auto_quant = qa;

car.auto_price = pa;

}

int get_quant_Auto(void)

{

return car.auto_quant;

}

double get_price_Auto(void)

{

return car.auto_price;

}

char* get_description_Auto(void)

{

return car.auto_descrip;

}

char* get_manufacturer_Auto(void)

{

return car.auto_manufacturer;

}


4) Main Program:

This part of the project contains the main function. Include the code below in your main program, which has the same name as your project. Remember to include the appropriate header file. When finished invoke File Save All, then Build All and Run the project.

#include "t1.h"

void main(void)

{

input_Auto("Car", "Nissan",4,25000);

print_Auto();

}

5) Borland C++ Debugger:

Borland C++ debugger comes with many features such as trace, step, watch and evaluate. These features are added by going into the debug menu and choosing the appropriate function.

a. Add Watch:

Borland C++ allows programmers to watch over the variables as he/she steps over or traces into the program. Add watch for car.auto_quant, car.auto_price, car.auto_descrip and car.auto_manufacturer so you can watch them change as you trace or step into the program:

b. Trace Into:

A trace on a program will go into each individual function and stop after evaluating each line. Do a trace into the code above and watch the assignment of values for car.auto_quant, car.auto_price, car.auto_descrip and car.auto_manufacturer.

c. Step Over:

A step will only stop at each line in main. Do a step over the code above and watch the assignment of values for car.auto_quant, car.auto_price, car.auto_descrip and car.auto_manufacturer.

d. Evaluate:

During a trace or step it is also possible to evaluate the values of any variable. Evaluate the value of the variable car.auto_quant any time during the trace/step.


Laboratory Assignment #5

EEL 3801 Introduction to Computer Engineering

Topic: Review of the C language Part 1 including while loop, for loop, do-while loops, if-else statement, switch statement, and the scanf( ) and printf( ) functions.

Due: Beginning of next week's lab time.

Review of the C language - Loops, Control Structures and formatted I/O

The following three labs are to review the C programming language. This should be a review since programming knowledge is a prerequisite for this class (EEL 3801).

We are going to use the switch statement to select from a menu a variety of choices. Based on the choice a test will be performed. The tasks are:

a. Display the ASCII table in a nice formatted table.

b. Display the ACSII code given the ASCII character.

c. Display the ASCII character given an ASCII code.

d. Display the menu.

e. Quit

For task a, use a for or a nested for loop to index through all of the ASCII codes (0 to 255). If an ASCII code does not display well i.e. the carriage return, NULL, beep etc. do not print the code but rather print a message for the code i.e. CR, NULL, BEEP etc. For a blank do not print blank but rather the work BLANK. The table must be aligned.

Task b and c get the input from the user and display the corresponding output.

Task d redisplays the menu in case the user forgot.

Task e quits the program.

The program must first display the menu then ask the user for a choice. Once the user enters a choice, execute the task and repeat the cycle by asking the user for another choice. Do not redisplay the menu unless the user selects that task. Quit when the user selects the Quit menu item.

Notes:

· Use the function int isprint(int ch) in the file ctype.h to determine if a character is printable. inprint returns true if ch is printable.

· Use a double for loop to print the ASCII table.

· Use a while loop to recycle through the menu selections until the user chooses to quit.

· Use an if statement to decide to print the character or not.

· Use a switch statement to select the menu action.

· Use printf and scanf to interact with the user.


Laboratory Assignment #6

EEL 3801 Introduction to Computer Engineering

Topic: Review of the C language Part 2.

Due: Beginning of next week's lab time.

Review of the C language

1. Multidimensional arrays in C:

2. Data Scope in C.


Laboratory Assignment #7

EEL 3801 Introduction to Computer Engineering

Topic: Review of the C language Part 3 including files and the passing of arguments to functions.

Due: Beginning of next week's lab time.

Review of the C language - Files and Passing of arguments

The following lab is to review the C programming language. This should be a review since programming knowledge is a prerequisite for this class (EEL 3801). For some this lab will introduce files. Files are simple and straightforward.

1. Files in C:

Files are how we access data that is stored on a disk. Before we access the data we must first open the file. We open a file by using fopen():

fp = fopen(filename,"w");

Where filename is a variable of type array of character or a literal as in "file1.dat". The "w" means open for writing. If a file with the same name exist it will be deleted. A new file will be created. To read an existing file use "r" instead of "w". A file with the same name must exist. fopen returns a pointer to the file control block (FCB). This pointer must be passed to the functions that read and write to the files. The pointer to the file control block can be declared as:

FILE *fp;