The Student Room Group

Little Man computer problem

hello.

I am struggling on little man computer question.

Input two numbers, x and y. If x is exactly 1 larger than y, then it outputs 11 and if it isn't then it outputs 22.


I would appreciate if you can explain how you did it so i understand it.

Thank you! :smile:
(edited 6 years ago)
What exactly is confusing you about this?

It sounds quite simple. Create two inputs for your variables and store them in memory.

You could then take your Y value away from your X value, then branch if(the result is > 1) and output the numbers accordingly.

E.G.

X = 20
Y = 19

20 - 19 = 1
X is exactly 1 > Y

If X = 21
Y = 22

21 - 22
X is not exactly 1 > Y
Reply 2
yes I know but i am new to little man computer and so i don't know how to implement it into lmc code. I only know branch if zero and branch always. I don't know how to do if the value is > or < something.
Original post by Spazum
yes I know but i am new to little man computer and so i don't know how to implement it into lmc code. I only know branch if zero and branch always. I don't know how to do if the value is > or < something.


Unsure of the exact syntax for your language. Do you mind sending me a picture of the syntax or the program name and I'll go write it up for you now.
Reply 4
IMG_20170524_211637 (1).jpg

Sorry about the pic, im having trouble with the print screen function. feel free to delete and add code. :smile:
Original post by Spazum
IMG_20170524_211637 (1).jpg

Sorry about the pic, im having trouble with the print screen function. feel free to delete and add code. :smile:

Pssh looks like a nightmare man.

Here's what I'd say.

You could go for the same method I just described.

Ask user for input Y (more efficient this way first)
Store input y somewhere
Ask user for input X (no need to store)
Subtract Y (using memory location)
sub dat 1 (subtract the decimal value one)
then use branch if zero

The only way the number can equal zero is if X is exactly 1 > Y
Reply 6
Original post by luciferhf
Pssh looks like a nightmare man.

Here's what I'd say.

You could go for the same method I just described.

Ask user for input Y (more efficient this way first)
Store input y somewhere
Ask user for input X (no need to store)
Subtract Y (using memory location)
sub dat 1 (subtract the decimal value one)
then use branch if zero

The only way the number can equal zero is if X is exactly 1 > Y


How would I tell it to print 11 or 22. i don't know how to do if functions. IMG_20170524_211637 (1).jpg
Reply 7
how do i do an if statement to tell it to print 11 or 22
Original post by Spazum
how do i do an if statement to tell it to print 11 or 22


You could just load a number into the accumulator to represent a print.
Reply 9
Original post by luciferhf
You could just load a number into the accumulator to represent a print.


INP
Y STA 99
INP
X STA 98
SUB Y
loop SUB Y
BRZ
OUT 11
HLT

why does it output -792?
Original post by Spazum
INP
Y STA 99
INP
X STA 98
SUB Y
loop SUB Y
BRZ
OUT 11
HLT

why does it output -792?


Looks like you have your loop with the SUB Y! Think you should have a quick look at a Youtube video to get a quick idea of how loops work in LMC.
Reply 11
It only prints -1



INP Y STA 99 INP X STA 98loop LDA X BRZ OUT SUB Y11 OUT 11
This should work. It will subtract the first input from the second input.
e.g. If you input 5 then input 6, it will do 6 - 5

INP
STA NUM_Y
INP
SUB NUM_Y
SUB ONE
BRZ TRUE
LDA ELEVEN
TRUE ADD ELEVEN
OUT
HLT

NUM_Y DAT
TRUE DAT
ONE DAT 1
ELEVEN DAT 11
(edited 6 years ago)

Quick Reply

Latest

Trending

Trending