Little Man Computer Task 2x

Download Report

Transcript Little Man Computer Task 2x

Little Man Computer Task 1
Last lesson you were asked to write a program to multiply two
numbers together.
The next slide has a working program to do this.
You will need to adapt this program this lesson to perform a
different task.
Program to multiply 2 numbers
500
291
500
292
193
391
293
192
499
292
801
905
193
600
700
-
Input x
Store acc. as x
Input y
Store acc. as y
Load z in to acc.
Add x to acc. value
Store acc. as z
Load y in to acc.
Minus one from acc.
Store acc. as y
Skip next if acc. is zero
Jump to instruction 5
Load z in to acc.
Output acc.
Halt
1xx
2xx
3xx
4xx
500
600
700
800
801
802
9xx
Load
Store
Add
Subtract
Input
Output
Halt
Skip If Negative
Skip If Zero
Skip If Positive
Jump
Little Man Computer Task 2
Write a program to divide two numbers.
Write the program in word, or on paper, before using the LMC
program.
The program should output the quotient, and then the
remainder.
e.g. for the calculation 10/3 the program would output 3 and 1.
If you finish the program this lesson, use the LMC program and
the ‘Registers’ Slide, to help you answer the exam question on
the last slide of this file.
The addition method
take the divisor and add it to itself.
update a counter.
Check if it equals the number that is being divided.
If not, do the same addition, and update your counter again.
keep going till you see that the sum is equal to or greater than the
number that is to be divided.
Then the value of your counter is the quotient.
To get the remainder, (divisor - (final sum - divident))
for example. take 10/3
3 + 3 = 6 counter = 1
6 + 3 = 9 counter = 2
9 + 3 = 12 counter = 3 (now 12 > 10 hence quotient = counter = 3)
remainder = 3 - (12 - 10) = 1
The addition method
take 22/5
5 + 5 = 10 counter = 1
10 + 5 = 15 counter = 2
15 + 5 = 20 counter = 3
20 + 5 = 25 counter = 4
(now 25 > 22 hence quotient = counter = 4)
remainder = 5 - (25 - 22) = 2
The subtraction method
take the divident and subtract the divisor from it.
update a counter.
Check if it equals the number that is being divided.
If not, do the same addition, and update your counter again. keep
going till you see that the sum is equal to or greater than the number
that is to be divided.
Then the value of your counter is the quotient.
To get the remainder, (divisor - (final sum - divident))
for example. take 10/3
10 - 3 = 7 counter = 1
7 - 3 = 4 counter = 2
4 - 3 = 1 counter = 3 (now 1 < 3 hence quotient = counter = 3)
remainder = final value = 1
The subtraction method
take 22/5
22 - 5 = 17 counter = 1
17 - 5 = 12 counter = 2
12 - 5 = 7 counter = 3
7 - 5 = 2 counter = 4
(now 4 < 5 hence quotient = counter = 4)
remainder = final value = 2
LMC Instruction Set
1
Load
2
Store
Reverse of Store: walks to the mbox
specified, copies the instruction on the
slip of paper and takes it to the
calculator and punches the number in.
walk to calculator, reads the number
there, and writes the number on a slip
of paper, walks to the mbox specified
in the instruction, and puts this slip of
paper there (replacing any paper
already there
LMC Instruction Set
3
4
5
Add
walk to mailbox address specified and
read the number there; walk to
calculator and adds it to the number
already in the calculator
Subtract similar to add but subtracts
Input
walks to the in box and picks up the
slip of paper having a 3-digit number
on it; walks over to the calculator and
enters that number
LMS Instruction Set
6
Output
7
Halt
800 Skip if
Negative
walk to calculator, reads the
number there, and writes the
number on a slip of paper, walks
to the out box and puts this slip of
paper there
stop
walk to calculator; if the value is
less than zero then walk to prog.
counter and add one to the current
value
LMS Instruction Set
801 Skip If Zero walk to calculator; if the value is
zero then walk to prog. counter
and add one to the current value
802 Skip If
walk to calculator; if the value is
Positive
greater than zero then walk to
prog. counter and add one to the
current value
9
Jump
Tells LMC to change the program
counter to the number shown on
the address portion
Registers
•
Different CPU designs have different numbers and types of registers. The following four types of
registers, however, are found in all designs:
•
PC, the program counter. The PC tells the CPU which memory location contains the next instruction
to be executed. Typically, the PC is incremented by 1 so that it points to the next instruction in
memory. But branch instructions can set the PC to another value.
–
•
IR, the instruction register. This register is important because it holds the instruction that is
currently being executed.
–
•
In the LMC, the "instruction register" is shown as the Instruction Display. To visualize the LMC operation, we
can picture the Little Man holding the instruction written on a slip of paper he has taken from a mailbox,
and reading what needs to be done.
MAR, the memory address register. This register tells the CPU which location in memory is to be
accessed.
–
•
In the LMC, the "program counter" is represented as the Location Counter.
The MAR is not shown in the LMC.
MDR, the memory data register. This register holds the data being put into or taken out of the
memory location identified by the MAR. It is sometimes called the memory buffer register, or MBR.
Exam Question