Writing a Little Man Computer program to compare 2 integers

In summary: if the result is equal to 0, then the program terminates. . . .
  • #1
DanjoJojo
8
2
Homework Statement
Write an LMC program (with instructions) which does the following.
The program will input two integers (ranging from 0 to 999). The program will output 0 if the first input is strictly greater than the second number, it will output 1 if the first input is equal to the second input, and output 2 if the first number is strictly less than the second number. The program will run continuously in that it will continue to accept pairs of inputs, provide outputs, and never halt.
Relevant Equations
https://www.101computing.net/LMC/
I've attempted and I can retrieve a 0 when the first input is greater than the second using BRP. I can't seem to get the output of 1 if the first input is equal to the second input. I also don't get a 2 output with the first input being less than the second. I also have no idea how to loop a program continuously. Any suggestions or comments would help? I'm a beginner.
INP
STA num1
INP
STA num2

LDA num1
SUB num2
BRP pos

LDA num2
SUB num1
BRZ equal
BRP lessthan
OUT
BRA exit

pos LDA 99
OUT
exit HLT

num1 DAT
num2 DAT
equal DAT 1
lessthan DAT 2
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
DanjoJojo said:
Homework Statement:: Write an LMC program (with instructions) which does the following.
The program will input two integers (ranging from 0 to 999). The program will output 0 if the first input is strictly greater than the second number, it will output 1 if the first input is equal to the second input, and output 2 if the first number is strictly less than the second number. The program will run continuously in that it will continue to accept pairs of inputs, provide outputs, and never halt.
Relevant Equations:: https://www.101computing.net/LMC/

I've attempted and I can retrieve a 0 when the first input is greater than the second using BRP. I can't seem to get the output of 1 if the first input is equal to the second input. I also don't get a 2 output with the first input being less than the second. I also have no idea how to loop a program continuously. Any suggestions or comments would help? I'm a beginner.
Code:
               INP                     get the first number
               STA num1         store it at address num1
               INP                    get the second number
               STA num2        store it at address num2

               LDA num1        load num1 into the accumulator
               SUB num2        subtract num2 from the accumulator
               BRP pos            if the result is positive, then                                                                 num1>num2

               LDA num2         load num2 into the accumulator                            SUB num1         subtract num1 from the                                                                        accumulator
               BRZ equal           if the result is zero, then num1=num2
               BRP lessthan      if the result is positive, then                                                                 num1<num2
               OUT                      output accumulator content
               BRA exit               unconditional branch to label exit

pos         LDA 99                  if num1>num2, then load 99 into the                                                 accumulator
                OUT                      output the 99
exit          HLT                      stop

constant and storage definitions below

num1      DAT                         storage for num1
num2      DAT                         storage for num2
equal      DAT 1                       storage labeled 'equal', and having                                                    value 1
lessthan DAT 2                      storage labeled lessthan, and                                                              having value 2
Do the comments help you to see what's wrong? What is the proper target of a branch instruction? Label pos and label exit reference executable instructions, but label equal and label lessthan reference storage areas.

Sorry about the misalignment in the code and comments ##-## I trust that it's at least marginally readable now ##-## something went wrong with the editor/parser/renderer . . .
 
Last edited:
  • Like
Likes Mark44, pbuk and DanjoJojo
  • #3
sysprog said:
Do the comments help you to see what's wrong? What is the proper target of a branch instruction? Label pos and label exit reference executable instructions, but label equal and label lessthan reference storage areas.

Sorry about the misalignment in the code and comments ##-## I trust that it's at least marginally readable now ##-## something went wrong with the editor/parser/renderer . . .
Yes, the comments help me see where I was stuck. I had trouble with showing whether or not num1=num2 or rather I didn't know which BR to use. Thank you too for providing the comments, I was able to follow along very easily.
 
  • Like
Likes sysprog
  • #4
Commenting your code can help you to understand it.

I think that until you get so that the meaning of the instuction is 'second nature' to you, you can take advantage of the fact that you only have to fully understand once what the instructrion is doing in order to write the comment, and then when you're later trying to follow your own logic, the comment can remind you, instead of your having to think it through again.

I think that the first problem with your program was not which BR to use, but that no BR should target something that isn't an instruction.
 
  • Like
Likes pbuk
  • #5
sysprog said:
Commenting your code can help you to understand it.
Absolutely!
I teach a class in computer architecture that uses MIPS assembly. In the demo programs I show my class, probably 80% of the lines of code have comments. Assembly instructions tend to be so terse, that it's really helpful to include comments to explain what the code is doing and why.
 
  • Like
Likes DanjoJojo and sysprog
  • #6
Mark44 said:
Absolutely!
I teach a class in computer architecture that uses MIPS assembly. In the demo programs I show my class, probably 80% of the lines of code have comments. Assembly instructions tend to be so terse, that it's really helpful to include comments to explain what the code is doing and why.
My normal practice in assembly language coding is to put a comment on every line, and an explanatory comment block preceding every discernable 'section', and sometimes I'll continue a comment on the next line ##-## that's pretty much standard in the IBM mainframe arena ##\dots##
 
  • #7
sysprog said:
My normal practice in assembly language coding is to put a comment on every line, and an explanatory comment block preceding every discernable 'section', and sometimes I'll continue a comment on the next line ##-## that's pretty much standard in the IBM mainframe arena ##\dots##
The early examples I show have comments on every line, but in the later examples, I'll use just one comment for a boilerplate output block, where one register gets set with the value to print, another register gets set with the service code for the type of thing being printed, and finally syscall, which calls the OS to perform a service.
 
  • Like
Likes sysprog
  • #8
Mark44 said:
The early examples I show have comments on every line, but in the later examples, I'll use just one comment for a boilerplate output block, where one register gets set with the value to print, another register gets set with the service code for the type of thing being printed, and finally syscall, which calls the OS to perform a service.
That makes sense to me ##-## I do that in PL/I code ##-## in old (all caps even in the comments) IBM mainframe assembly code it's not uncommon to see lines like LA R10,1(R10) BUMP R10 ##-## I think that anyone who needs that instruction explained should have to look it it up in the manual, but the practice of commenting every line is there partly to help non-coders who might be reading, to get an idea of what the code is doing.
 
  • #9
sysprog said:
That makes sense to me − I do that in PL/I code
A bit OT, but the first programming class I had (in fall of '74) was taught using PL/C, a compact subset of PL/I, at least that's what I believe the 'C' represents.
 
  • Like
Likes sysprog
  • #10
sysprog said:
Do the comments help you to see what's wrong? What is the proper target of a branch instruction? Label pos and label exit reference executable instructions, but label equal and label lessthan reference storage areas.

Sorry about the misalignment in the code and comments − I trust that it's at least marginally readable now − something went wrong with the editor/parser/renderer . . .
One thing I've noticed while doing the code in the lmc simulator is that there's no continuous loop. Or did I miss something? I tried to put loop with the input part but I'm not getting it to work. How do I have the program run continuously?
 
  • #11
DanjoJojo said:
One thing I've noticed while doing the code in the lmc simulator is that there's no continuous loop. Or did I miss something? I tried to put loop with the input part but I'm not getting it to work. How do I have the program run continuously?
This illustrates the concept:
Code:
start     OUT              ; output the value
          ADD one          ; add 1 to the accumulator
          BRA start        ; return to the start
one       DAT 1            ; define storage for value 1
That will output the ascending integers from 0 to the maximum value that the accumulator can hold, then wrap the accumulator to 0, and do it again, continuously.

The emulator at https://101computing.net/LMC/ seems to not like the comments ##-## without the comments, the code runs as anticipated.

Please note that the page at http://teaching.idallen.com/dat2343/10f/notes/380_LMC_coding_help.txt
deplores the kind of superfluous comments I've made here. I agree in principle with what's said there about that; this is in my view a reasonable exception.
 
Last edited:
  • #12
Mark44 said:
A bit OT, but the first programming class I had (in fall of '74) was taught using PL/C, a compact subset of PL/I, at least that's what I believe the 'C' represents.
Maybe the C stood for Cornell and for Conway ##-## https://en.wikipedia.org/wiki/PL/C
 
  • #13
sysprog said:
This illustrates the concept:
Code:
start     OUT              ; output the value
          ADD one          ; add 1 to the accumulator
          BRA start        ; return to the start
one       DAT 1            ; define storage for value 1
That will output the ascending integers from 0 to 999, then wrap the accumulator to 0, and do it again, continuously.

The emulator at https://101computing.net/LMC/ seems to not like the comments ##-## without the comments, the code runs as anticipated.

Please note that the page at http://teaching.idallen.com/dat2343/10f/notes/380_LMC_coding_help.txt
deplores the kind of superfluous comments I've made here. I agree in principle with what's said there about that; this is in my view a reasonable exception.
So when I put the code in using https://101computing.net/LMC/ it just keeps giving me a 0, when I input a smaller number for input 1 and a big number for input 2. I can never get the other outputs 2 or 1. I really don't understand why nothing is working except for getting a 0 when input 1 is greater than input 2.

num1 DAT
num2 DAT
equal DAT 1
lessthan DAT 2
 
  • #14
DanjoJojo said:
So when I put the code in using https://101computing.net/LMC/ it just keeps giving me a 0, when I input a smaller number for input 1 and a big number for input 2. I can never get the other outputs 2 or 1. I really don't understand why nothing is working except for getting a 0 when input 1 is greater than input 2.

num1 DAT ; storage for num1
num2 DAT ; storage for num2
equal DAT 1 ; define 'equal; as an alias for the literal '1'
lessthan DAT 2 ; define 'lessthan' as an alis for the literal '2'
A brief aside: the accumulator holds more than 3 digits.

Did you notice that at label 'pos' you have LDA 99 ? That means load whatever is in 'box' 99, which is zero. Let's as an illustration pretend that you wanted to output the literal 99 instead of the zero that's in location 99 ##-## here's how:

INP
STA num1
INP
STA num2

LDA num1
SUB num2
BRP pos

LDA num2
SUB num1
BRZ equal
BRP lessthan
OUT
BRA exit

pos LDA ninetynine
OUT
exit HLT

num1 DAT
num2 DAT
equal DAT 1
lessthan DAT 2
ninetynine DAT 99

Hint: Labels 'equal' and 'lessthan' are still not valid branch targets.
 
Last edited:
  • #15
Here's some code that works -- you'll still need to figure out how to make it loop.
I've added comments to explain what I'm doing, but per sysprog's post, you'll probably need to discard the comments if you try to run this.
One comment deserves further explanation. The BRP instruction branches if the value in the accumulator is positive or zero. By checking first for a zero value, using BRZ, I know that the BRP instruction will branch only if the accumulator value is strictly positive.
Code:
       INP
       STA num1
       INP
       STA num2
       LDA num1  
       SUB num2        ; Calculate num1 (in Accum) - num2
       BRZ eq          ; If Accum - num2 == 0 branch to eq label
       BRP gt          ; If Accum - num2 > 0 branch to gt
                       ;   Note that BRP branches if accum >= 0, I checked for equality first
       LDA two
       OUT             ; num1 < num2, so display 2
       BRA end         ; Jump to end
eq  LDA one
       OUT             ; num1 == num2, so display 1
       BRA end
gt  LDA zero           ; num1 > num2, so display 0
       OUT
end HLT

num1 DAT
num2 DAT
zero DAT 0
one  DAT 1
two  DAT 2
First time I've written any Little Man Computer code. This is an amazingly small instruction set!
 
Last edited:
  • Like
Likes sysprog
  • #16
Mark44 said:
Here's some code that works -- you'll still need to figure out how to make it loop.
I've added comments to explain what I'm doing, but per sysprog's post, you'll probably need to discard the comments if you try to run this.
One comment deserves further explanation. The BRP instruction branches if the value in the accumulator is positive or zero. By checking first for a zero value, using BRZ, I know that the BRP instruction will branch only if the accumulator value is strictly positive.
Code:
       INP
       STA num1
       INP
       STA num2
       LDA num1 
       SUB num2        ; Calculate num1 (in Accum) - num2
       BRZ eq          ; If Accum - num2 == 0 branch to eq label
       BRP gt          ; If Accum - num2 > 0 branch to gt
                       ;   Note that BRP branches if accum >= 0, I checked for equality first
       LDA two
       OUT             ; num1 < num2, so display 2
       BRA end         ; Jump to end
eq  LDA one
       OUT             ; num1 == num2, so display 1
       BRA end
gt  LDA zero           ; num1 > num2, so display 0
       OUT
end HLT

num1 DAT
num2 DAT
zero DAT 0
one  DAT 1
two  DAT 2
First time I've written any Little Man Computer code. This is an amazingly small instruction set!
I will try this too, I'm sorry to keep asking for help but this program is confusing due to the branching. I've been doing a lot of research about branching and unfortunately none resemble a program like this. But I thank everyone for their input, I'm still working towards a resolution!
 
Last edited by a moderator:
  • #17
DanjoJojo said:
One thing I've noticed while doing the code in the lmc simulator is that there's no continuous loop. Or did I miss something?
Many assembly languages have no loop control structures or if or switch decision structures that are present in high-level languages like C, C++, Java, and so on. In their places, assembly languages usually have unconditional branch or jump instructions that always transfer control to the label that is part of the instruction, or conditional branch instructions that transfer control to a label if some condition is met.
All of the high-level language control structures can be implemented in assembly using branch instructions. In @sysprog's code of post #11, start is a label; the target of a BRA instruction. In my code of post #15, the labels are eq, gt, and end. They are targets of various branch instructions.

Other labels I used are for variables such as num1 and num2, or for constants -- zero, one, and two.
DanjoJojo said:
I tried to put loop with the input part but I'm not getting it to work.
As far as I know there is no LOOP instruction in LMC.
 
  • Like
Likes sysprog
  • #18
Mark44 said:
As far as I know there is no LOOP instruction in LMC.
There are 8 machine instructions and the DAT assembler instruction:

InstructionMnemonicMachineCode
LoadLDA5xx
StoreSTA3xx
AddADD1xx
SubtractSUB2xx
InputINP901
OutputOUT902
EndHLT000
Branch if zeroBRZ7xx
Branch if zero or positiveBRP8xx
Branch alwaysBRA6xx
Data storageDAT

http://yorku.ca/sychen/research/LMC/LMCInstructionSummary.html
 
  • #19
DanjoJojo said:
I will try this too, I'm sorry to keep asking for help but this program is confusing due to the branching. I've been doing a lot of research about branching and unfortunately none resemble a program like this. But I thank everyone for their input, I'm still working towards a resolution!
I finally figured out how to make it loop without halting. I started with the first INP making it loop then where you put BRA end I changed it to a BRA loop, after each output for one, two and zero I used a BRA loop. This code worked and ran just as I was trying to get. Thank you everyone for the help!

loop INP
STA NUM1
INP
STA NUM2

LDA NUM1
SUB NUM2
BRZ eq
BRP gt

LDA two
OUT
BRA loop
eq LDA one
OUT
BRA loop
gt LDA zero
OUT
BRA loop

NUM1 DAT
NUM2 DAT
zero DAT 0
one DAT 1
two DAT 2
 
  • Like
Likes sysprog

1. What is a Little Man Computer program?

A Little Man Computer program is a simplified computer architecture used for teaching and learning basic computer programming concepts. It consists of a memory, accumulator, and a simple instruction set.

2. How do I write a Little Man Computer program to compare 2 integers?

To compare 2 integers using a Little Man Computer program, you will need to use the "branch if zero" instruction. This instruction will check if the two numbers are equal and branch to a specific instruction if they are. You can also use the "branch if positive" and "branch if negative" instructions to compare if one number is greater than the other.

3. What is the syntax for writing a Little Man Computer program?

The syntax for writing a Little Man Computer program is similar to assembly language. It consists of a label, an instruction, and an address. For example, "INP" is the instruction to input a number, and "STA" is the instruction to store a number in a specific address in memory.

4. How do I test and debug my Little Man Computer program?

You can test and debug your Little Man Computer program by using a Little Man Computer simulator. This simulator will allow you to step through your program and see the values in memory and the accumulator at each step. You can also use print statements to display the values and make sure they are correct.

5. Are there any resources available to help me learn how to write a Little Man Computer program?

Yes, there are many resources available online to help you learn how to write a Little Man Computer program. You can find tutorials, step-by-step guides, and even online courses to help you understand the basics of this simplified computer architecture.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
14
Views
31K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
27
Views
7K
Back
Top