Uploaded by Sanchit

Lab7

advertisement
Bishop's University
CS201 Foundations of Computer Science
LAB 7
In this Lab, you will work with the Assembler and the Von Neumann machine on your Simulator.
Assembly Language Programming
This lab experience will introduce you to programming in assembly language. You will load some sample
programs provided with the lab software, assemble and run them, and make some simple changes to them
to become familiar with the language. Then you will solve some problems by writing some assembly
language programs from scratch.
To run the assembler, click Assembler on the main software menu. You will be presented with
two panels. The left panel is the text editor for editing assembly language code.
Exercise 1. Adding two numbers and displaying the result.
Select Open from the File menu and select the file Example1.asm in the Examples folder. The text of an
assembly language source program should appear in the source program panel. You will recall that you
loaded an equivalent machine language program Lab 6. Study the code to verify that the program will do
what your machine language program did (if it ran correctly!). Then choose Assemble from the Assembler
menu. You will see a program listing appear in the utility panel. The listing is just a copy of the source
program, with each line numbered for your reference. At the end of the listing, there should be a message
that assembly has been completed without syntax errors.
Choose View Object Code from the Assembler menu, you will see the source program translated into
machine code: the object code. Choose View Symbol Table from the Assembler menu and you will see the
table containing the memory locations for the 3 symbols x, y and z. Finally, run the program by selecting
Execute from the Assembler menu. A von Neumann machine simulator pops up, with the machine code of
our program loaded into memory and the source code displayed in the source pane. When you run the
program in the simulator (by selecting either Run or Step), the current instruction is selected in both forms of
the program. Run the program. What is the output?
Exercise 2. Some syntax errors
You will typically begin by entering a source program by hand or opening it from a file. Assemble is the only
option available from the Assembler menu at this point. During assembly, the system displays a program
listing and any syntax error messages in the utility pane. Each line of source text is numbered in the listing
for easy reference, and an error message appears more or less on the line following the line of code in which
the error occurs. Assembly halts at the first error. Error detection is pretty thorough: errors are caught even at
the lexical level, such as illegal labels and integer literals that are too large for sixteen-bit sign magnitude
representation.
The assembler generates the following syntax error messages:
.begin expected.
.end expected.
Illegal opcode: <symbol>
Halt does not take operand.
Wrong number of fields in instruction.
Label must end with:
Label already used: <symbol>
Undeclared data label: <symbol>
Undeclared instruction label: <symbol.>
Data label already used as instruction label: <symbol>
Data directive already exists for label: <symbol>
Value must be an integer.
Number is too large.
Not enough memory for instructions.
Not enough memory for data.
The program that we just loaded from a file had no syntax errors. Let's put some syntax errors
into it so that we can see how the assembler detects them.
a. Replace the operand x in the first instruction, load x, with the operand a, and reassemble the program.
The assembler should discover that you forgot to provide a data declaration for a. The assembler forces you
to keep track of all of your data and their initial values by declaring them at the bottom of the program.
Correct this error before going on.
b. Replace the operator add with the operator multiply in the second instruction. The assembler should
discover an unrecognized opcode. Correct this error before going on.
c. Replace the number 2 with the number 40000 in the first data declaration. Note the error detected by the
assembler. Correct this error before going on.
d. Delete the space between add and y in the second instruction. Note the error detected. There must be at
least one blank space or tab between an operator and its operand. Correct this error before going on.
e. Replace load x with load x y . Note the error detected. Now insert a label in that instruction (for example,
start: load x y). Note the new error.
f. Insert a carriage return after the second instruction. Note the error detected. Assembly language typically
has a fixed format, which requires an instruction to appear on each line of a program. Correct this error
before going on.
Exercise 3. Making decisions in assembly language
Open the file Example2.asm and study the program. What does it do? Assemble and execute the program.
Does it behave as expected? Modify the program so that it displays the larger of the two numbers, and test it
until it behaves correctly.
Exercise 4. Loops in assembly language 1
Open the file Example3.asm and study the program. What does it do? Assemble and execute the program.
Does it behave as expected? Modify the program so that it checks to see that the input data is greater than
zero before it starts the loop. If that's not the case, the program should halt with no output. Show your
program to the lab instructor.
Exercise 5. Larger/Smaller
Write pseudocode for a program which allows the user to input two numbers (X and Y) and a code
C. If the code has value 1, the program should output the larger of X and Y and otherwise output the smaller.
Now convert your pseudocode to an assembly language program. Enter the program into the software lab
assembler. Assemble the program and execute it with different input data. Show your program to the lab
instructor.
Name: __________________________
Lab #7: Assembly Language Programming
7.1
Output from Example1.asm:
7.3
What does the program in Example2.asm do?
Program to display the larger of the 2 numbers:
7.4
What does the program in Example3.asm do?
Modified program to check that input is greater than 0:
7.5
Pseudo-code for program:
Assembly program:
Download