LABORATORY MANUAL

24 downloads 111 Views 193KB Size Report
Schedule of Assignments: Lab # Week Due Points Title. 1. Next lab. 1% The tasm and tlink commands. 2. Next lab. 1% Hello World in Reverse in Assembly. 3.
EEL 3801 Introduction to Computer Engineering Summer 2005

LABORATORY MANUAL

Schedule of Assignments: Lab # 1 2 3 4 5 6 7 8 9 10

Week Due Next lab Next lab Next lab Next lab Next lab Next lab Next lab Next lab Next lab Next lab

Points 1% 1% 1% 1% 1% 1% 1% 1% 1% 1%

Title The tasm and tlink commands Hello World in Reverse in Assembly Bubble Sort in Assembly Loops, Control Structures, formatted I/O and Files Objects and Classes Constructors, Destructors and Friend Functions Inheritance Inheritance and Constructor Functions Pointers, Dynamic Memory, Pass by Reference, and Arrays Polymorphism

All homework must include: • A Cover page with the following information Course number (EEL 3801) Your name Lab number • 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 mov

ax,@data ds,ax

mov mov int

ax,0900h dx,offset msg 21h ax,4C00h 21h

main

mov int endp

.data msg end

main

db

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

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, '$' endnumb db '9', 0dh, 0ah, '$' incrnumb db 2, '$'

; the start number ; the stop number ; 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: Loops, Control Structures, formatted I/O and Files Due: Beginning of next week's lab time. Loops, Control Structures, formatted I/O and Files The following four labs are to review the C programming language. This should be a review since programming knowledge is a prerequisite for this class (EEL 3801). Familiarization with the Borland C++ compiler: 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 may 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. 3) Member Functions: In most C++ projects member functions are contained in a separate node. In our case the bodies of the functions may be included here in the main program. If you prefer to include your member functions in a separate .CPP file, you must also include the new header file in double quotes. Name the file anything other than the project name with .CPP extension. Then use the right mouse button to add this file to the project. 4) Main Program:

This part of the project contains the main function. Include your main body 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. 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. b. Trace Into: A trace on a program will go into each individual function and stop after evaluating each line. c. Step Over: A step will only stop at each line in main. d. Evaluate: During a trace or step it is also possible to evaluate the values of any variable. 6) Files 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. The Assignment For this lab, 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. b. c. d. e. f.

Display the ASCII table in a nice formatted table. Display the ACSII code given the ASCII character. Display the ASCII character given an ASCII code. Print the ASCII table to a file in a nice formatted table. Display the menu. 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 word BLANK. The table must be aligned into columns. Task b and c get the input from the user and display the corresponding output. Task d prints the ASCII table in a nice column format to a file. Ask the user for a file name, open the file for writing and using the for loop from task a print the data to the file. Finally close the file. Task e redisplays the menu in case the user forgot. Task f 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. 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 #5 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 objectoriented 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 objectoriented 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; int month; int year; public:

// private data member // private data member // private data member

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; date1.set(4,7,1996); date2.set(31,12,1996);

// instantiate 2 Date objects // sets 1st variable's members // sets 2nd variable's members // print out the dates cout