Download L A B OBJECT ORIENTED PROGRAMMING IN JAVA Machine

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
J A V A
L A B
M A N U A L
LAB
2
OBJECT ORIENTED PROGRAMMING IN JAVA
Machine Language and
Its Relationship to Java
LAB 02 TABLE OF CONTENTS
2.1
2.2
2.3
2.4
2.5
Machine Language and Java
Brookshear's Machine Language Summary
Running the Machine Simulator
Writing a First Java Application
Post Lab Exercises
PURPOSE OF LAB 02
This lab uses the Machine Language Simulator that is part of the text Computer Science: An Overview, by
J. Glenn Brookshear, Professor Emeritus at Marquette University. You are not expected to memorize the
basic operations of this machine language. Rather, you will use this lab to enhance your understanding of
the machine cycle and basic machine instructions. In addition, you will write a first Java program that is
similar to some of the sample programs written for the Simulator.
TO PREPARE FOR LAB 02
Read Wu: Chapter 0
Read through this laboratory session
Using your memory device, create a directory called lab02 and copy the file Simulator.java, from
http://www.mscs.mu.edu/~marian/60/Labs/lab02/
TO COMPLETE
LAB 02
Work in groups of two people. If there are an odd number of people in the lab, there may be one
group of three people. You are expected to be on time so that appropriate groups may be formed.
Each person in the group should keep his/her own answers to the questions. To get the most of this
shared experience, you should individually answer a question and then compare the answer with
your partner. If one of you does not understand a concept, the other should explain until the
concept or process is understood. If you can explain, you truly do understand. If you are unsure of
your answers, check with the lab tutor.
When you have finished this lab, see the lab tutor, who will give you an open
note, fifteen point quiz. For five points, hand in your printout of Lab02.txt.
This will be your grade for Lab 02
SESSION 2
PAGE 2.1
J A V A
L A B
M A N U A L
1.1 MACHINE LANGUAGE AND JAVA
The Central Processing Unit, or CPU, of a computer contains the circuitry that defines the instructions
that the machine understands. These machine instructions are referred to as the machine language .
Developing complex programs in machine language is a tedious process. A high – level language is a
programming language that is more compatible with human language and, therefore, easier for the
programmer to use.
In general, once a program has been written in a high-level language, it is translated into a machine
language by a program called a translator, or compiler. The translated version of the program can then be
executed by the machine. However, the translated version can be executed only by a machine that
understands that particular machine language. This constraint is not compatible with the need to execute
programs on different machines throughout the Internet. For example, a program that controls the
animation of a Web page must be able to execute on any machine viewing the page.
To overcome this obstacle, compilers for the Java language translate programs into a "generic machine
language" called bytecode , which was developed by Sun Microsystems. Bytecode was designed so that
different machines could "understand" programs in bytecode efficiently via a program called an interpreter,
which interprets a single bytecode instruction into machine language instructions one instruction at a
time. Today, most browsers used to surf the Web contain a bytecode interpreter. These browsers are said
to be "Java enabled." This means that once a Java program has been translated into bytecode, the
bytecode version can be transferred over the Internet to a variety of machines, each of which can execute
the program efficiently by means of the Java interpreter. In short, a Java program is normally translated
into a bytecode program and then this bytecode version is converted into machine-level instructions by
means of an interpreter.
The Java code is called source code and is stored in a file with a .java extension, called a source file .
The compiled file contains bytecode and is stored in a file with a .class extension, called a bytecode
file .
2.2 BROOKSHEAR'S MACHINE LANGUAGE SUMMARY
The following table summarizes the machine language presented in Computer Science: An Overview. Each
instruction is 16 bits, or two bytes, long and thus is represented by four hexadecimal digits.
Op – code
Operand
Description
1
RXY
LOAD register R with the contents of the memory cell at address XY .
2
RXY
LOAD register R with the value XY .
3
RXY
STORE the contents of register R at memory location XY .
4
0RS
MOVE the contents of register R to register S .
5
RST
ADD the integer contents of registers S and T and leave the result in
register R. Integers are stored using two's complement notation.
6
RST
ADD the floating-point contents of registers S and T and leave the result in
register R .
7
RST
OR the contents of registers S and T and place the result in register R .
8
RST
AND the contents of registers S and T and place the result in register R .
SESSION 2
PAGE 2.2
J A V A
L A B
9
M A N U A L
RST
EXCLUSIVE OR the contents of registers S and T and leave the result in
register R .
A
R0X
ROTATE the contents of register R one bit to the right X times.
B
RXY
JUMP to the instruction located at memory address XY if the contents of
register R equals that of register 0 .
C
000
HALT
Examples
The machine instruction 1234 has op – code 1 and means:
LOAD register R1 with the contents of the memory cell at address 34
The machine instruction 20FF has op – code 2 and means:
LOAD register R0 with the value FF
The machine instruction 34B0 has op – code 3 and means:
STORE the contents of register R4 at memory location B0
The machine instruction 4602 has op – code 4 and means:
ADD the contents of registers R0 and R2 and place the sum in register R6
The machine instruction B6A4 has op – code B and means:
JUMP to the instruction in memory location A4 if the contents of register R6 equals that of
register R0.
2.3 RUNNING THE MACHINE SIMULATOR
The machine simulator program that we will run is a Java program written by Dr. Glenn Brookshear and
Dr. Michael Slattery of Marquette University. All Java programs are stored in a file with a .java
extension. This program should be on your memory device in the directory lab02 and is called
Simulator.java . To run the program, open it inside of the development environment software TextPad .
You should
1. Double click on the desktop icon for TextPad
2. Click on File – Open Change directories to the lab02 directory of your memory device.
The source file Simulator.java should be opened. You are not expected to read or
understand this source code.
3. To compile the source file into a bytecode file click on Tools – Compile Java. The result
should be several files with .class extentions that contain Java bytecode. If you would like
to see the listing of these files, again go to Files – Open and choose Files of Type: All Files
(*.*) and you should see the names of the bytecode files. Do not need to open these files.
4. To run the simulator , click on Tools – Run Java Application . When the program runs you
should see:
SESSION 2
PAGE 2.3
J A V A
L A B
M A N U A L
Across the bottom of the display window are buttons whose actions are:
Button
Clear Memory
Load Data
Run
Single Step
Halt
Help
Action
Change the contents of all memory cells to 00.
Transfer data from the input window into the machine.
Begin executing the machine cycle.
Execute a single machine cycle.
Stop executing the machine cycle
Display a help package describing how to use the simulator.
How to correctly use the program
The machine is programmed by first typing data in the data input window and then transferring it to the
machine by clicking the Load Data button. The syntax for typing data is as follows:
[ P C ] followed by a two-digit, hexadecimal value assigns a value to the program counter.
F o r e x a m p l e [PC] 80 would change the program counter to 8016.
[Rn]
followed by a two-digit, hexadecimal value assigns a value to register number n.
SESSION 2
PAGE 2.4
J A V A
[nn]
L A B
M A N U A L
F o r e x a m p l e [R7] 23 would change the contents of register 7 to 2316.
followed by a sequence of two-digit, hexadecimal values assigns the values to consecutive memory
cells starting at address nn .
F o r e x a m p l e [30] 40 56 C0 00 assigns the value 4016 to the memory cell at address 30 and the
values 56, C0, 00 to the following cells at addresses 31, 32, and 33.
Once changes have been entered into the data input window they can be transferred into the machine by
clicking the Load Data button. Thus, entering
[PC] 00
[00] 20 FF 40 02 C0 00
in the data input window and then clicking the Load Data button sets the program counter to 00 and the
memory cells starting at address 00 to 20, FF, 40, 02, C0, and 00.
Experiment 2.1
Familiarize yourself with the simulation program by following the steps below to enter and execute the
following machine language program.
Address Content
10
23
11
1F
12
12
13
20
14
50
15
23
16
30
17
40
18
C0
19
00
231F is the first instruction
Step 1: Translate the program in this experiment into English.
231F ___________________________________________________________________________________________
1220 ___________________________________________________________________________________________
5023 ___________________________________________________________________________________________
3040 ___________________________________________________________________________________________
C000 ___________________________________________________________________________________________
Predict the final result. That is, explain what happens and what changes you expect to occur in
memory.
Predict: ________________________________________________________________________________________
________________________________________________________________________________________________
________________________________________________________________________________________________
________________________________________________________________________________________________
Step 2: Run the simulation program and enter these three lines by typing this data in the input window.
[10] 23 1F 12 20 50 23 30 40 C0 00
[20] 03
[PC] 10
SESSION 2
PAGE 2.5
program begins at memory cell 10
value at memory cell 20 is 03
set Program Counter to 10
J A V A
L A B
M A N U A L
Step 3: Transfer the data from the data input window to the simulated machine by clicking the Load Data
button. Inspect the main display to confirm that the program has been properly placed in main memory
starting at location 10 and that the program counter is set to 10. Then, execute a single machine cycle,
fetch – decode – execute, by clicking the Single Step button. At this point, what is the value stored in :
program counter ? _______
instruction register ? _______
register 3 ? _______
Step 4: Execute a single machine cycle again, Record the changes in the registers.
program counter ? _______
instruction register ? _______
register 2 ? _______
Step 5: Execute a single machine cycle again, Record the changes in the registers.
program counter ? _______
instruction register ? _______
register 0 ? _______
Step 6: Execute a single machine cycle again, Record the changes in the registers.
program counter ? _______
instruction register ? _______
memory location 40 ? _______
Step 7: Execute a single machine cycle again, Record the changes in the registers.
program counter ? _______
instruction register ? _______
Writing it in Java: A single Java instruction that performs the same operation as the above program is
y = 31 + x;
which loads 31 ( 1F16), loads the value stored in a memory location that we call x, adds the two values, and
stores the result in a memory location that we call y.
In algebra, the character '=' is read as "equals" and indicates that what is to the left of the '=' is equal to
what is to its right. In Java, the character '=' is called the assignment operator. The statement y = 31 + x
is read as " add 31 and x and assign the result to y ".
Step 8: Change the contents of the memory cell at address 14 to the hexadecimal value 90, instead of 50.
Then, execute this modified program by changing the program counter back to 10 and clicking the Run
button. Explain the difference in the actions of this program from those of the original program and in the
results.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Experiment 2.2
While the following looks like an illegal algebraic statement, it is a valid Java statement.
x = x + 5;
This instruction assigns the sum of 5 and x to x . Write a machine language program for the Simulator,
that changes a value stored in memory by adding 5 onto it. Store the program beginning at memory
location C2. Assume that x refers to memory location C0. The original value you store in x is up to you.
SESSION 2
PAGE 2.6
J A V A
L A B
M A N U A L
Step 1: Record your program instructions and the statements that explain what they do.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Step 2: Using the machine simulator, click on the Clear Memory button. Enter your program into the
input box of the machine simulator and set the program counter. Click the Load Data button. Run the
program. Record the results, specifically record the value stored in C0 before and after the program is run.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Step 3: Modify the program to accomplish the following:
x = x + x;
Run the program. Record your modification and the results, specifically record the value stored in C0
before and after the program is run.
Modification:_________________________________________________________________________________
_____________________________________________________________________________________________
Results: _____________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Experiment 2.3 (Optional)
This experiment assumes that you have learned about the storage of integers using Two's Complement
notation. You will be asked to decode the numbers that are computed and stored in the machine simulator
using two's complement.
Step 1: Write a program to find the sum of 7F16 and 0116, storing the sum at memory location 00.
Record your program instructions.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
SESSION 2
PAGE 2.7
J A V A
L A B
M A N U A L
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Step 2: Using the machine simulator, click on the Clear Memory button. Enter your program into the
input box of the machine simulator and set the program counter. Click the Load Data button. Run the
program. Record the results, specifically record the value stored in 00 before and after the program is run.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Step 3: Record the addition problem in base 16, and base 2. Given that integers are stored using Two's
Complement, interpret and explain these results.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Experiment 2.4
In this experiment you will investigate the branch instruction, with op – code B, found in the machine
language for the machine simulator. This program has two branch instructions, which produce a loop.
Recall the loops that were used in Lab 01 to describe the algorithms for converting a decimal whole number
and a decimal fraction to binary.
Step 1: Start the simulation program and clear the simulated machine's main memory. Then, place the
program below in the memory cells from address F0 to FD.
[F0] 20 00 21 01 23 05 B3 FC 50 01 B0 F6 C0 00
[PC] F0
Step 2: Execute the program one step at a time by clicking the Single Step button. Record the values of
the program counter after each step. Stop when the HALT instruction is loaded.
SESSION 2
PAGE 2.8
J A V A
L A B
M A N U A L
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Step 3: Follow this program on paper to explain the results:
Addresses
Instruction
F0 – F1
2000
F2 – F3
2101
F4 – F5
2304
Use an arrow
F6 – F7
B3FC
F8 – F9
5001
to show the instructions that are in the loop.
FA – FB
B0F6
FC – FD
C000
This program uses three registers, R0, R1 and R3. Record on paper the values that are stored in these
registers as the program is executed. When the value in a register changes, do not erase it. Instead,
lightly cross out an overwritten value and then record the new value.
R0
R1
0
0
R3
0
What condition determines when the loop ends?
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
What instruction is executed when the loop ends?
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
Step 4: What changes should be made to the program in Step 1 if it is to be placed in memory starting at
location A0? Explain your answer.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
W r i t i n g i t i n J a v a Java has three looping structures, one of which is called the while loop. This is
how the loop in this experiment could be written in a high – level language such as Java.
Note: count != 4 is read as " count is not equal to 4 "
count = 0;
while (count != 4)
count = count + 1;
SESSION 2
PAGE 2.9
J A V A
L A B
M A N U A L
2.4 WRITING A FIRST JAVA APPLICATION
USING TEXTPAD TO EDIT, COMPILE AND RUN A JAVA PROGRAM
This portion of the lab will take you through the step-by-step process for editing, compiling and running
your first Java application. . The program that we will mimics the first programs you wrote in Machine
Language and is called First. The discussion of the design and implementation phases of a more complex
program will be dealt with in class and future Labs.
S t e p 1 : Understand what problem the program is supposed to solve.
P r o b l e m : Write a program that adds five onto a value stored in memory.
S t e p 2 : Hand write the program.
Once you understand what a program is supposed to do, you should write the program using pencil
and paper. This should always be done before you begin to enter the code in a text editor. Since I am
supplying the code, you can bypass this step for now.
//First Java Program by Tom Jones
class First
{
public static void main(String[] args)
{
int number;
number = 31;
number = number + 5;
}
}
A brief explanation of the Java code above:
//First Java Program by Tom Jones
The two forward slashes define an in-line comment.
Comments are not part of the program, but instead are for the benefit of the reader. You should
replace the name Tom Jones with your name.
class First - All Java code must be inside of a class. We begin with the Java reserved word
class and then choose an appropriate name for the class. I have chosen First. The class is
defined between a set of matching braces, {} , which define the beginning and end of the
class. We say the braces delimit , define the beginning and end of, the class. The braces
are called delimiters .
public static void main(String[ ] args) - all Java applications must have a main method.
This line is the header of the main method and must appear in all Java applications. A
method contains a set of statements that will be executed by the computer when the
program is run. Again, the body of a method is delimited by a set of matching braces.
int number; This statement is a variable declaration statement .
It reserves
memory that can be accessed with the name number instead of the actual memory address.
In addition, we declare that this memory location will store an int , an integer that is stored
in four bytes instead of the one byte that is used in the machine simulator. It is the
responsibility of the computer to keep a table of all variables used in a program along with
information about each variable that includes the memory address associated with the
variable and the type of data that is stored in this memory location.
number = 31; This statement assigns 31 (a decimal value) to the memory location that
has the name number .
SESSION 2
PAGE 2.10
J A V A
L A B
M A N U A L
This statement adds 5 to number and assigns the sum to
number. Once the memory location number is overwritten with 36, the original value, 31,
is lost.
number = number + 5;
You should notice that the three statements that define what the program does end with a
semi – colon. A program statement ends with a semicolon. These three statements
declare the variable number , assign an intial value to number and then calculate and
assign a new value to number .
S t e p 3 : Edit (Enter and save) the program
Use a text editor to implement the program. Open TextPad . You should have an empty document that
you will use to type the program. If you do not have a blank document, then either click on the New
Document icon or go to File – New . Type in the program. When done save the file in a file with a .java
extension. Click on File – Save As. When the Save As window opens, look at the bottom portion for the
Save as type: textfield. If Java(*.java) does not appear in the textfield click on the menu arrow and
choose Java(*.java) . The name of the file should match the name of the class and, therefore, must be
named First.java . Therefore, type First into the File name: textfield. Then, click the Save button.
S t e p 4 : Compile the program
To compile the source file into a bytecode file click on Tools – Compile Java.
In order for a program to compile, it must follow all of the rules of Java syntax. If your file contains any
kind of syntax error, such as a missing semi-colon or quote, the program will not compile. How can you
tell if the program did or did not compile?
If it does not compile, you will see one or more error messages in Textpad. Error messages
can be very informative. But, since this is a first program, any error message may be
difficult for you to read. You must go back to Step 2 and edit the existing file to remove any
syntax errors and then recompile the code. You must follow the sample code exactly.
If the file does compile, you will hear a sound and your file First.java will be displayed. The
result of the compilation process is the creation of a new file called First.class that contains
the Java bytecode version of the program. The result should be a file First.class . If you
would like to see the listing of this file, again go to Files – Open and choose Files of Type:
All Files (*.*) and you should see the name of the bytecode file. Do not open this file.
S t e p 5 : Run the program
Click on Tools – Run Java Application
Record the results.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
S t e p 6 : Maintain the program
At this point, you can make changes to your program by going back to Step 2 followed by Steps 3 & 4.
Modify your program by adding the highlighted lines.
SESSION 2
PAGE 2.11
J A V A
L A B
M A N U A L
//First Java Program by Tom Jones
class First
{
public static void main(String[] args)
{
int number;
number = 31;
System.out.println(number);
number = number + 5;
System.out.println(number);
}
}
System.out.println(number) ; prints the value stored in the memory location number to
the monitor. System.out is the monitor. println is a message to print a new line. What is
printed in this new line is determine by what is placed between the pair of parentheses. By
placing something between the parentheses is how information is passed to the println
message. In this case, the variable number is passed, so the value stored in number is
printed twice, after number is initialized to 31 and after 5 is added onto the value stored in
number.
Compile and run the modified program. Record the results.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
SESSION 2
PAGE 2.12
J A V A
L A B
M A N U A L
2.8 POST LAB EXERCISES
2.1
Write a program in machine language that finds the sum of 45, 36 and 29. Store the sum in
memory loacation 00. Write an explanation of what occurs when the program is run,
2.2
Write a program in machine language that finds the sum of 45, -36 and -29. Store the sum in
memory loacation 00. Write an explanation of what occurs when the program is run,
2.3
In Lab 01, Exercise 1.10 you were asked to AND, OR and XOR the binary numerals 10101010 and
11110000. Write one program that finds these values, storing the results in address A0, B0 and C0.
Write an explanation of what occurs when the program is run,
2.4
There is no SUBTRACT instruction in Brookshear's Machine Language. Using the available
operations, write a program that calculates 45 - 100 and stores the result in memory location 00.
Write an explanation of what occurs when the program is run,
2.5
There is no MULTIPLY instruction in Brookshear's Machine Language. Using the available
operations, write a program that multiplies 4 x 6 and stores the product in memory location 00.
Hint: A loop is needed. Write an explanation of what occurs when the program is run,
2.6
This assumes that you know how integers are stored using two's complement notation. Write a
program that stores in memory location A0 the opposite of the number stored in memory location B0.
For example, if B0 stores the integer 5, ( or 05) then, after the program is run, A0 stores the integer
-5 ( or FB).
2.7
What does the following machine language program do? Write an explanation of what occurs when
the program is run, To determine the purpose of the program, consider what happens if the contents
of memory location B4 is changed to 03, 06, 0A.
Address
A0 – A1
A2 – A3
A4 – A5
A6 – A7
A8 – A9
AA – AB
AC – AD
AE – AF
B0 – B1
B2 – B3
B4 – B5
Instruction
10B4
2100
2201
2300
B3B0
5323
5113
B0A8
31B5
C000
0500
[A0] 10 B4 21 00 22 01 23 00 B3 B0 53 23 51 13 B0 A8 31 B5 C0 00 05 00
[PC] A0
SESSION 2
PAGE 2.13