print keyin2 Displays contents of the store location keyin2 on the screen
stop Stops program execution
lower print keyin2 Displays contents of the store location keyin2 on the screen
print keyin1 Displays contents of the store location keyin1 on the screen
stop Stops program execution
keyin1 data 0 Initialises a store location keyin1 with the value 0 in it
keyin2 data 0 Initialises a store location keyin2 with the value 0 in it
4. Write a TOM program that reads a number N from the keyboard and displays the sum of all integers from 1 to N i.e.1+2+3+…+N.
read keyin Reads data inputted by keyboard and stores in the store location keyin
loop load sofar Loads data from the store location sofar in to the accumulator
add one Adds the store location one to the accumulator
store sofar Stores value in accumulator in the store location sofar
add total Adds the store location total to the accumulator
store total Stores value in accumulator in the store location total
load sofar Loads data from the store location sofar in to the accumulator
sub keyin Subtracts the store location keyin from the accumulator
jifn loop Transfers control to the instruction loop if the sign flag is set
print total Displays contents of the store location total on the screen
stop Stops program execution
keyin data 0 Initialises a store location keyin with the value 0 in it
one data 1 Initialises a store location one with the value 1 in it
sofar data 0 Initialises a store location sofar with the value 0 in it
total data 0 Initialises a store location total with the value 0 in it
Alternatively, a more mathematical approach would be to use the below program. Observing the numbers inputted and outputted from the above program, I was able to find a relationship between the two numbers, this can be summarised by the below formula:
(N x 0.5) + 0.5 x N = TOTAL
The program using the above formula is simpler to write, uses far less processor cycles, and therefore far more efficient.
read keyin Reads data inputted by keyboard and stores in the store location keyin
load keyin Loads data from the store location keyin in to the accumulator
mult val Multiplies the accumulator by the store location val
add val Adds the store location val to the accumulator
mult keyin Multiplies the accumulator by the store location keyin
store total Stores value in accumulator in the store location total
print total Displays contents of the store location total on the screen
stop Stops program execution
keyin data 0 Initialises a store location keyin with the value 0 in it
val data .5 Initialises a store location val with the value 0.5 in it
total data 0 Initialises a store location total with the value 0 in it
TOM2
1. A mobile telephone company, Odear, makes a monthly standing charge of £12.50 and charges 5 pence per local call. Write a TOM program that reads the amount of calls made and displays the total monthly bill.
read calls Reads data inputted by keyboard and stores in the store location calls
load calls Loads data from the store location calls in to the accumulator
mult rate Multiplies the accumulator by the store location rate
add standing Adds the store location standing to the accumulator
store total Stores value in accumulator in the store location total
print total Displays contents of the store location total on the screen
stop Stops program execution
total data 0 Initialises a store location total with the value 0 in it
calls data 0 Initialises a store location calls with the value 0 in it
standing data 12.50 Initialises a store location standing with the value 12.50 in it
rate data .05 Initialises a store location rate with the value .05 in it
2. Expand your program of (1) so that the program jumps back to the beginning, ready to calculate another bill instead of ending.
start read calls Reads data inputted by keyboard and stores in the store location calls
load calls Loads data from the store location calls in to the accumulator
mult rate Multiplies the accumulator by the store location rate
add standing Adds the store location standing to the accumulator
store total Stores value in accumulator in the store location total
print total Displays contents of the store location total on the screen
jump start Transfers control to the instruction start
stop Stops program execution
total data 0 Initialises a store location total with the value 0 in it
calls data 0 Initialises a store location calls with the value 0 in it
standing data 12.50 Initialises a store location standing with the value 12.50 in it
rate data .05 Initialises a store location rate with the value .05 in it
3. What’s wrong with the program in (2)?
The program has no way of ending (normally), and will therefore loop continuously.
4. Modify (2) so that if the user enters 0 for the number of units the program terminates.
start read calls Reads data inputted by keyboard and stores in the store location calls
load calls Loads data from the store location calls in to the accumulator sub check Subtracts the store location check from the accumulator
jifz end Transfers control to the instruction end if the zero flag is set
mult rate Multiplies the accumulator by the store location rate
add standing Adds the store location standing to the accumulator
store total Stores value in accumulator in the store location total
print total Displays contents of the store location total on the screen
jump start Transfers control to the instruction start
end stop Stops program execution
total data 0 Initialises a store location total with the value 0 in it
calls data 0 Initialises a store location calls with the value 0 in it
standing data 12.50 Initialises a store location standing with the value 12.50 in it
rate data .05 Initialises a store location rate with the value .05 in it
check data 0 Initialises a store location check with the value 0 in it
- Now modify (4) so that the user can tell the system how many bills to calculate and the program terminates after running that many times.
read billnum Reads data inputted by keyboard and stores in the store location billnum
start read calls Reads data inputted by keyboard and stores in the store location calls
load calls Loads data from the store location calls in to the accumulator mult rate Multiplies the accumulator by the store location rate
add standing Adds the store location standing to the accumulator
store total Stores value in accumulator in the store location total
print total Displays contents of the store location total on the screen
load billnum Loads data from the store location billnum in to the accumulator
sub billsub Subtracts the store location billsub from the accumulator
store billnum Stores value in accumulator in the store location billnum
jifz end Transfers control to the instruction end if the zero flag is set
jump start Transfers control to the instruction start
end stop Stops program execution
total data 0 Initialises a store location total with the value 0 in it
calls data 0 Initialises a store location calls with the value 0 in it
standing data 12.50 Initialises a store location standing with the value 12.50 in it
rate data .05 Initialises a store location rate with the value .05 in it
billnum data 0 Initialises a store location billnum with the value 0 in it
billsub data 1 Initialises a store location billsub with the value 1 in it
- Finally, modify the program of (5) so that the user can first enter the price per unit, and the standing charge.
read rate Reads data inputted by keyboard and stores in the store location rate
read standing Reads data inputted by keyboard and stores in the store location standing
read billnum Reads data inputted by keyboard and stores in the store location billnum
start read calls Reads data inputted by keyboard and stores in the store location calls
load calls Loads data from the store location calls in to the accumulator mult rate Multiplies the accumulator by the store location rate
add standing Adds the store location standing to the accumulator
store total Stores value in accumulator in the store location total
print total Displays contents of the store location total on the screen
load billnum Loads data from the store location billnum in to the accumulator
sub billsub Subtracts the store location billsub from the accumulator
store billnum Stores value in accumulator in the store location billnum
jifz end Transfers control to the instruction end if the zero flag is set
jump start Transfers control to the instruction start
end stop Stops program execution
total data 0 Initialises a store location total with the value 0 in it
calls data 0 Initialises a store location calls with the value 0 in it
standing data 0 Initialises a store location standing with the value 0 in it
rate data 0 Initialises a store location rate with the value 0 in it
billnum data 0 Initialises a store location billnum with the value 0 in it
billsub data 1 Initialises a store location billsub with the value 1 in it
Modifications in TOM2
In question 1, the program initialises four store locations; rate to store the standard call rate of 0.5, standing to store the standing charge of 12.50, calls to store the number of calls made and total to store the total bill. The programs reads a value inputted by the user (number of calls), multiplies this value by the call rate, adds the standing order and displays it.
Question 2 introduces a loop after the total has been displayed to the start of the program so that user may calculate another bill, this however is not ideal as there is no correct way to terminate the program normally.
Question 4 combats this problem by allowing the user to enter 0 to terminate the program. This is done by introducing an additional store location called check with the value 0 assigned to it. The program subtracts check from the number of calls entered, if the result is 0 (0 – 0 = 0) then the zero flag is set, the jifz statement then transfers control to the end of the program, where it terminates normally.
Question 5, in addition to the store location used in question 1 introduces two more; billnum to store the number of bills required and billsub, a store location containing the value 1. The user initially enters the number of bills required, this is stored in billnum, the program then calculates the bill in same way as question 1. After the bill has been displayed, the program subtracts billsub (1) from the number of bills, if the result is zero (ie no more bill to calculate) the zero flag is set, and using the jifz statement jumps to the end of the program. If the zero flag is not set (more bills to calculate) the program is looped back to enter more bill details.
Question 6, allows the user to enter the standing charge, rate of calls and number of bills before the bills are calculated, these are stored in their respective locations (standing, rate and billnum) before the program continues to execute in the same way as question 5.
CSO Tutorial 4
Exercise 2.1
We wish to compare the performance of two different machines: M1 and M2. The following measurements have been made on these machines:
Which machine is faster for each program and by how much?
For program 1, M2 is 5 seconds(or 100%) faster than M1.
For program 2, M1 is 1 second (or 25%) faster than M2.
Exercise 2.2
Consider the two machines and programs in Exercise 2.1. The following additional measurements were made:
Find the instruction execution rate (instructions per second) for each machine running program 1.
Instructions executed = Instructions per second (instruction execution rate)
time(seconds)
M1
200000000 = 20000000
10
= 20 x 106 Instructions per second or 20 Million Instructions per second
M2
160000000 = 32000000
5
= 32 x 106 Instructions per second or 32 Million Instructions per second
Exercise 2.3
If the clock rates of machines M1 and M2 in Ex 2.1 are 200 MHz and 300 MHz respectively, find the clock cycles per instruction (CPI) for program 1 on both machines using the data in Ex 2.1 & 2.2.
Clock rate = clock cycles per instruction (CPI)
Instruction execution rate
M1
200000000 = 10 clock cycles per instruction (CPI)
20000000
M2
300000000 = 9.375 clock cycles per instruction (CPI)
32000000
Question 4
Draw a full flowchart of the final TOM program produced at the end of exercise TOM2. This should include all the instructions, loops and all the program labels in the appropriate places.