The Student Room Group

Computer Science - LMC (OCR A Level

Hello,
Can anyone explain this LMC code?
Its meant to square the input (1,2,3) but whenever i do it i get: 1,3,6 as opposed to 1,4,9
Sorry you've not had any responses about this. :frown: Are you sure you've posted in the right place? :smile: Here's a link to our subject forum which should help get you more responses if you post there. :redface:
Reply 2
Original post by TSR Jessica
Sorry you've not had any responses about this. :frown: Are you sure you've posted in the right place? :smile: Here's a link to our subject forum which should help get you more responses if you post there. :redface:

i think i did thanks for the help
Original post by jougan
Hello,
Can anyone explain this LMC code?
Its meant to square the input (1,2,3) but whenever i do it i get: 1,3,6 as opposed to 1,4,9

Hey,

When I went through the code, I did get 1,4,9! Okay I will try to go through this using two examples:

Example 1 - Simple - Input = 1:
num = 1
one = 1
count = 0
total = 0

The loop gets executed: In the ACC, total (0) is loaded. We add num (1) to the ACC and we store the result in Total.
num = 1
one = 1
count = 0
total = 1


We then load count (0) and ADD 1 to it. We then store this in count!
num = 1
one = 1
count = 1
total = 1

We the subtract num from what is currently in the ACC (which is count).
Count - num = 1 - 1 = 0

BRZ - because the previous subtraction resulted in a 0, the next line of execution is the end selection block. Here, total (1) is loaded and is printed, the program halts!!
OUTPUT = total = 1

1 x 1 = 1 so it worked!!


Example Two - Harder - Input = 2:

num = 2
one = 1
count = 0
total = 0

The loop gets executed: In the ACC, total (0) is loaded. We add num (2) to the ACC and we store the result in Total.
num = 2
one = 1
count = 0
total = 2

We then load count (0) and ADD 1 to it. We then store this in count!
num = 2
one = 1
count = 1
total = 2

We then subtract num from what is currently in the ACC (which is count).
Count - num = 1 - 2 = -1

The ACC content is NOT 0. So BRZ wont happen! The next line is BRA to loop so the next line to be executed will be line 3.

We once again load total (2) and add num (2) to it. Store this in total!
num = 2
one = 1
count = 1
total = 4

Count (1) is loaded and One (1) is added and is stored in count which is now 1 + 1=2
num = 2
one = 1
count = 2
total = 4

Subtract num from the ACC content (which contains the result to the previous addition). In the ACC, count is currently stored. Subtract num (2) from the ACC. 2 - 2 = 0

BRZ - because the previous subtraction resulted in a 0, the next line of execution is the end selection block. Here, total (4) is loaded and is printed, the program halts!!

OUTPUT = total = 4

2 X 2 = 4 so it worked!!


Hope this helped!!!

Have a great month ahead :smile:
(edited 3 years ago)
Reply 4
Original post by #தமிழன்டா
Hey,

When I went through the code, I did get 1,4,9! Okay I will try to go through this using two examples:

Example 1 - Simple - Input = 1:
num = 1
one = 1
count = 0
total = 0

The loop gets executed: In the ACC, total (0) is loaded. We add num (1) to the ACC and we store the result in Total.
num = 1
one = 1
count = 0
total = 1


We then load count (0) and ADD 1 to it. We then store this in count!
num = 1
one = 1
count = 1
total = 1

We the subtract num from what is currently in the ACC (which is count).
Count - num = 1 - 1 = 0

BRZ - because the previous subtraction resulted in a 0, the next line of execution is the end selection block. Here, total (1) is loaded and is printed, the program halts!!
OUTPUT = total = 1

1 x 1 = 1 so it worked!!


Example Two - Harder - Input = 2:

num = 2
one = 1
count = 0
total = 0

The loop gets executed: In the ACC, total (0) is loaded. We add num (2) to the ACC and we store the result in Total.
num = 2
one = 1
count = 0
total = 2

We then load count (0) and ADD 1 to it. We then store this in count!
num = 2
one = 1
count = 1
total = 2

We then subtract num from what is currently in the ACC (which is count).
Count - num = 1 - 2 = -1

The ACC content is NOT 0. So BRZ wont happen! The next line is BRA to loop so the next line to be executed will be line 3.

We once again load total (2) and add num (2) to it. Store this in total!
num = 2
one = 1
count = 1
total = 4

Count (1) is loaded and One (1) is added and is stored in count which is now 1 + 1=2
num = 2
one = 1
count = 2
total = 4

Subtract num from the ACC content (which contains the result to the previous addition). In the ACC, count is currently stored. Subtract num (2) from the ACC. 2 - 2 = 0

BRZ - because the previous subtraction resulted in a 0, the next line of execution is the end selection block. Here, total (4) is loaded and is printed, the program halts!!

OUTPUT = total = 4

2 X 2 = 4 so it worked!!


Hope this helped!!!

Have a great month ahead :smile:

thank you!
im sorry for the late reply!
the issue i made was after the HLT i didnt clear the ACC!

thanks again!
Original post by jougan
thank you!
im sorry for the late reply!
the issue i made was after the HLT i didnt clear the ACC!

thanks again!

Np

Quick Reply

Latest