Laboratory Assignment #1



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

#include

#include

#include

#include

// 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;

To write to a file we can use the fprintf function. This works the same as printf except that the file pointer must be passed. For example:

fprintf(fp,"The answer is %d\n",sum);

Note if sum has 37 then the file will get the line:

The answer is 37

To read from a file we use the fscanf function. This is the file version of scanf. For example, to read 3 integers from a file we can use:

fscanf(fp,"%d %d %d",&x, &y, &z);

When done we must close the file. We use the finction fclose() as in:

fclose(fp);

When reading, the function feof(fp) returns true if you have already read all of the data in the file.

Your task is to write a program that will read a file for reading and print the contents to the screen. You must ask the user for a filename. Open the file. While the end of the file is not reached, read the next line in the file and print is on the screen.

Before running the program, create an input file using an ASCII editor like the Borland C++ editor with at least 10 lines. You may use an old C program file as the input.

You may consider using the following algorithm

Get the file name from the user.

Open the file for input.

While the end of the file is not reached

Read a line from the file.

Write the line to the screen.

Close the file.

Turn in a listing of the program code along with the output.

2. Passing arguments to functions.

When passing arguments to functions there are two options. Consider the following function call:

1. Pass by value:

A copy of the contents of the variable is sent. The function can not modify the variable. What is passed to bill is a copy of the data stored in x. In this case 5 is passed.

Example 1:

void bill(int k)

{

printf("The value passed to bill is %d\n",k);

k = 10;

printf("The value passed to bill after being modified is %d\n",k);

}

void main( )

{

int x = 5;

printf("The value of x before bill is %d\n",x);

bill(x);

printf("The value of x after bill is %d\n",x);

}

The output is:

The value of x before bill is 5

The value passed to bill is 5

The value passed to bill after being modified is 10

The value of x after bill is 5

2. Pass by reference:

The address (pointer) of the variable is passed. The function can modify the variable. Any changes to the variable inside of bill will be reflected in the calling code. In this case the address of x is passed not the value 5. Note to pass the address we must put a & in front of the variable being passed. &x means the address of x. Also the function is going to receive the address of the argument not its value so we put a * in front of the argument. *k refers to what k points to, not k itself.

Example 2:

void bill(int *k)

{

printf("The value passed to bill is %d\n",*k);

k = 10;

printf("The value passed to bill after being modified is %d\n",*k);

}

void main( )

{

int x = 5;

printf("The value of x before bill is %d\n",x);

bill(&x);

printf("The value of x after bill is %d\n",x);

}

The output is:

The value of x before bill is 5

The value passed to bill is 5

The value passed to bill after being modified is 10

The value of x after bill is 10

Your task is to copy the program in example 3 and run it and explain the results in the comments of the program. The function bill will print the contents of x and y of which the contents of y is the address of x so use %X instead of %d.

Example 3:

void bill(int x, int *y)

{

printf("The arguments passed to bill are x = %d y = %X\n",x,y);

x = x + 5;

*y = *y + 5;

printf("The arguments after being modified are x = %d y = %x\n",x,y);

printf("What got modified is x = %d *y = %d\n",x,*y);

}

void main( )

{

int a = 5;

int b = 10;

printf("The values before bill are a = %d b = %d\n",a,b);

bill(a,&b);

printf("The values after bill are a = %d b = %d\n",a,b);

}

Laboratory Assignment #8

EEL 3801 Introduction to Computer Engineering

Topic: Objects and Classes.

Due: Beginning of next week's lab time.

Turn in: Any source code with output.

Objects and Classes

Objects and classes are what really makes C++ different from C. They form the basis for object-oriented programming. Classes and objects provide features that are unknown to C programmers, and provide significant power and flexibility, even if they are not used in object-oriented programming. This assignment introduces you to this powerful paradigm.

1. Structures in C++:

Classes in C++ are similar in many ways (but not all) to structures in C, and are declared in the same way as structures - by simply replacing the word struct with the word class. Take the following structure and re-declare it as a class.

struct date

{

int day;

int month;

int year;

};

2. Objects:

Objects are nothing more than variables, which, in turn, are nothing more than instances of a class. The class is the template, and the object is an instance of the class, or in other words, a copy of the template that has its own identity. Make objects today and tomorrow (e.g., instance variables of class date) in a main() program. Also instantiate a C data type using the C strucure date shown above, and call this variable yesterday.

3. Data Hiding in classes:

Setting the values of members in C++ classes is one difference between classes and structures. For the purpose of hiding data in classes, C++ differentiates between private and public members of a class. Public members of a class are equivalent to those of structures. That is, they can be accessed and changed by external functions in the program (the "." operator for variable, and the "->" operators for pointers). Private members, however, cannot be so changed. By default, all members of a class instance are considered private. Try changing (actually, setting) the values of the today or tomorrow objects within the main() program done in exercise #2 above through the following statements: (Use a print statement (cout) to see what the results of the statements are).

today.day = 28;

today.month = 11;

today.year = 1995;

Do the same thing to the C structure yesterday that you created in exercise #2. You should be able to do it successfully for yesterday, but not for today or tomorrow.

4) Public Access Specifier:

One way to get around this obstacle is to purposely declare the members of a class to be public. Replace the definition for the class date with the following:

class date

{

public:

int day;

int month;

int year;

};

and re-instantiate objects today and tomorrow. Set their values using the

same procedure as in exercise #3 above. You will find that it can now be

done successfully. These are referred to as class access specifiers.

5. Member Functions:

Your question at this point surely is "How do I get to the private members' values?". This is the point where classes and structures start looking very, very different. C++ allows classes to have member functions, which are functions that are members of a class. While structures only allow data members, classes allow function members. Member functions allow the outside world to access these private data members. Furthermore, these member

functions allow objects in C++ to manipulate themselves, and this is the essence of object-oriented programming. Instead of being a passive data structure, classes contain all that is necessary to carry out some task. Member functions are also declared in the class definition, and are typically (although not always as we will soon see) declared as public.

Please code the following short program which includes the member function declaration and function definition. Note the there are four member functions: get_day(), get_month(), get_year(), and set(const int d, const int m, const int y), and they are considered public. Also note how they are defined and used.

//Filename: DATE1.cpp

// Uses the Date class

#include

class Date

{

int day; // private data member

int month; // private data member

int year; // private data member

public:

int bonus_flag; // public data member

int get_day(void) // public member function

{

return (day);

}

int get_month(void) // public member function

{

return (month);

}

int get_year(void) // public member function

{

return (year);

}

void set(const int d, const int m, const int y)

{

day = d; // public member function

month = m; // assigns all values

year = y; // to private data members

return;

}

}; // always end classes with semicolons

void main( )

{

Date date1, date2; // instantiate 2 Date objects

date1.set(4,7,1996); // sets 1st variable's members

date2.set(31,12,1996); // sets 2nd variable's members

// print out the dates

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download