assign1 - Monash University

advertisement
MONASH UNIVERSITY
FACULTY OF INFORMATION TECHNOLOGY
SCHOOL OF BUSINESS SYSTEMS
BUS2120 Computer Concepts and Software Systems
Assignment 1: Programming the LMC
This assignment carries 6% of the assessment for the subject BUS2120.
Due Date: Written work is to be handed in to the tutor, at the tutorial of Week 8, beginning Monday
25th April. Your working programs are to be demonstrated and discussed with your tutor (be prepared to
explain what you have done). There are two assessable components.
For this assignment you are required to program the Little Man Computer (LMC). You should have read
Englander, Chapters 6 and 17, and the course handout “The Little Man Computer”.
In the WebCT directory you will find the program LMC.EXE, which simulates the LMC, and also
provides an assembler. You will also find several example files with an .LMM extension (these are
demonstration programs written in LMC Machine code), and example files with an .LMA extension
(these are programs written in LMC Assembly language).
Assignment 1 Part 1
Choose one of the following, and write the program in LMC machine language, i.e. 3-digit numerical
instructions.
1. Write a program, which inputs numbers and computes a running total. The program should halt and
display the total as soon as it exceeds 500. Only one 901 instruction is allowed. The program must
incorporate a loop, using at least one branch instruction.
2. Write a program which first inputs a large number. It should then continue to input numbers, and
subtract them from the first value, stopping when the running subtraction goes negative. The program
must incorporate a loop, using at least one branch instruction.
3. Write a program for Question 6.11, Englander Page 145 (P. 161 in second edition).
4. Write a program, which inputs a number and finds its square. (One approach here is to simplify the
given program Multiply.LMM).
5. Write a program that inputs 2 numbers and adds them. It should then determine whether a Carry has
occurred, i.e., whether the sum exceeds 999. (For example, 600 + 700 results in 300 [the 1000 carry is
lost]; also note that 300 < 600 and 300 < 700). If so, the program should first display “1” to signify
1000, before displaying the sum delivered by LMC.
6. Choose your own project. This must have your tutor’s consent.
7. Alternatively, choose one of the programs on Page 2.
Your working program should be demonstrated to your tutor.
A printed copy of your program, annotated with explanatory comments, is to be given to your tutor.
BUS2120
Assignment 1
Page 1 of 2
Assignment 1 Part 2
Choose one of the following, and write the program in LMC assembly language, i.e. using LMC
mnemonics, labels, constants and variables. If you have already chosen one of these in Part 1, you must
choose a different one for Part 2. The two published programs Multiply.LMA and Divide.LMA may be
freely adapted here.
1. Write a program which first inputs a number, which will specify how many more numbers are to be
input. The program should then input that many numbers, add them together, and display the final
total, e.g. first input 4, then the four numbers 101, 202, 303, 20, giving a final answer 626.
2. Write a program, which displays successive terms of the Fibonacci series 1, 1, 2, 3, 5, 8, 13, 21, …
Beginning with 1, 1, each subsequent term in the series is obtained by adding the two previous terms.
The program should halt when the term exceeds 980.
3. Write a program which inputs a number (e.g., 475), and then displays the digits one at a time (e.g. 4,
followed by 7, followed by 5). Divide.LMA can be adapted for this task.
4. Write a program to convert a decimal number to a different base, (similarly to your program in tutorials
1 and 2). The program should input 2 numbers, where the second number is the required base.
Divide.LMA can be adapted for this task.
5. Write a program which inputs a number and displays all of its factors (except 1). For example, input
the value 12, and display 2, 3, 4, 6.
6. Write a program which finds the sum of an arithmetical progression (AP) consisting of (n+1) terms:
Sum = a + (a+d) + (a+2d) + (a+3d) + … + (a+nd). The first term is a and the common
difference is d. For example, the AP 1 + 4 + 7 + 10 + 13 has first term a = 1, common difference d =
3, n = 4 (there are 5 terms), and sum = 35. The program should input the values a, d, and n and deliver
the sum.
7. The Highest Common Factor (HCF) of two numbers A and B is found as follows:
Subtract B from A until the remainder R is less than 0, then add B back once. If R = 0, B is the
HCF. Otherwise, replace B into A, R into B and repeat. Stop when R = 0.
e.g., for 77 and 28,
77 – 28 – 28 – 28 = -7, -7 + 28 = 21
28 – 21 - 21 = -14, -14 + 21 = 7
21 – 7 – 7 – 7 – 7 = -7, -7 + 7 = 0. The HCF is 7.
Write a program to find the HCF of two numbers.
8. Write a program which test whether a number is prime (i.e., has no factors except itself and 1).
9. (Extension to 5; more complicated. Worth bonus marks.) Greek mathematicians were particularly
interested in “perfect numbers”. A perfect number is equal to the sum of its factors. The first perfect
number is 6 = 1 + 2 + 3. The second is 28 = 1 + 2 + 4 + 7 + 14. The third is slightly less than 500.
Little Man cannot cope with the fourth, 8128. Write a program to test for perfect numbers.
10. Choose your own project; for example, a program involving indexing, as shown on page 8 of the
LMC document, which you may adapt if you wish. This must have your tutor’s consent.
Your working program should be demonstrated to your tutor.
A printed copy of your program, with plenty of explanatory comments, is to be given to your tutor.
BUS2120
Assignment 1
Page 2 of 2
Download