Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: (untagged)
Suppose that the following program is loaded in the LMC memory:
Memory address	Instruction	Address
00              Input           01
01              Store	        20
02              Input	        01
03              Store	        21
04              Input	        01
05              Store	        22
06              Subtract        21
07              Branch Positive	10
08              Load	        21
09              Branch	        11
10              Load	        22
11              Subtract        20
12              Output	        02
13              Halt	        00



Suppose that the program is supplied with the following decimal numbers: 23 , 20 and 15 (in this order). What will be the output in decimal? And what will be the output if the numbers 15, 18 and 32 are supplied (in this order)?
Posted
Updated 12-Oct-14 19:50pm
v2

1 solution

OK - just work it through
Fist instruction inputs a number - you're told that number is going to be 23. So our processor's accumulator has a value of 23
Next instruction stores the number in the accumulator (23) in memory location 20 - so, write that on a bit of paper...
A            23
20           23

Next instruction inputs a number
Next stores that in location 21 (you're told the number will be 20, so write that down on your bit of paper
A            20
20           23
21           20

Next instruction gets another number
Next instruction stores that in 22
Which you need to write down (you're told the number is 15)
A             15
20            23
21            20
22            15

Now you subtract the contents of 21 - here's the only tricky bit - you are subtracting 21 from the value in the Accumulator - i.e. the processor memory into which the number was input - which in our case is the value 15 (we know because we wrote it down!)

So - 15 - (the value at memory location 21)
which is 15 - 20
which is -5
A             -5
20            23
21            20
22            15




Next instruction says that, if the result is positive to go to memory location 10 - but it is not so we just carry on...
Next instruction says load the value at 21 - which is still 20 (we never stored the result of the subtraction)

A             20
20            23
21            20
22            15


Next instruction is an unconditional branch to 11 - so we branch to the instruction at 11 (subtract 20)
This instruction subtracts the contents of 20 (which is currently 23) from the value in the accumulator (which is currently 20)
20 - 23 = -3

A             -3
20            23
21            20
22            15


Now the output instruction is puzzling - but I assume it just outputs the value in the accumulator somewhere - in which case we will output -3

Hopefully you can follow along with the other values you have been given.

(Excuse formatting problems - the web site isn't being very responsive so I can't see what it will look like!)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900