Little Man Computer- Find Max / Multiply two Numbers

AI Thread Summary
The discussion focuses on modifying Little Man Computer (LMC) code to find the maximum value in an array and correctly multiply two integers. To find the maximum, the existing code needs adjustments to the branching logic, specifically changing the condition from "BRP" to ensure it retains the maximum value instead of defaulting to the last input. The multiplication code is based on repeated addition and should check for zero to return zero correctly. Additionally, issues with infinite loops arise from not resetting the LENGTH variable after multiple runs, leading to incorrect termination conditions. Overall, understanding and modifying the LMC code requires careful attention to branching and variable management.
L1lGh0sT
Messages
3
Reaction score
0

Homework Statement



I'm currently learning the little man computer and Currently I'm working on two programs. On one of them, I have the code to find the minimum value in an array, however I have to modify that code in order to find the maximum value. What would I have to change? And for the other program; that's supposed to multiply two integers. What would I need to change to make it work? Please advise.

Homework Equations



Find Min Code (Need to modify this code in order to find the maximum int)
BRA START
ARRAY0 DAT 5
DAT 4
DAT 3
DAT 7
DAT 5
DAT 2
DAT 1
DAT 7
ARRAYINDEX DAT 1
LENGTH DAT 8
MIN DAT 0
ONE DAT 1
LOADINST DAT 501
START LDA ARRAY0
STA MIN
LOOP LDA LOADINST
ADD ARRAYINDEX
STA LOADNEXT
LDA ARRAYINDEX
ADD ONE
STA ARRAYINDEX
LOADNEXT DAT 0
SUB MIN
BRP KEEPMIN
ADD MIN
STA MIN
KEEPMIN LDA LENGTH
SUB ARRAYINDEX
BRZ DONE
BRA LOOP
DONE LDA MIN
OUT
HLT



The Attempt at a Solution



//Need to fix this code to make it work.

LDA MULTIPLIC
BRZ DONE
LOOP LDA MULTIPLIER
BRZ DONE
SUB INCREMENT
BRZ DONE
LDA TOTAL
ADD MULTIPLIC
STA TOTAL
LDA INCREMENT
ADD ONE
STA INCREMENT
BRA LOOP
DONE LDA TOTAL
OUT TOTAL
HLT
INCREMENT DAT 0
ONE DAT 1
TOTAL DAT 0
MULTIPLIER DAT 5
MULTIPLIC DAT 3
 
Physics news on Phys.org
Is that self-modifying code in the find min function?
L1lGh0sT said:
Find Min Code (Need to modify this code in order to find the maximum int)

...
SUB MIN
BRP KEEPMIN
...

Here you have the current number from the array you are examining in the accumulator A. You subtract what you have as the current minimum and if the result is positive you keep the minimum you have already. Fix this up and you can find the maximum instead.

The Attempt at a Solution



//Need to fix this code to make it work.

(multiply)

The code is checking for multiply by zero first and should return zero as the result if that case is found.

Otherwise it performs the multiplication with repeated addition. I think you should have another go at this as anything I write here will be the answer.
 
Last edited:
aralbrec said:
Is that self-modifying code in the find min function?


L1lGh0sT said:
Find Min Code (Need to modify this code in order to find the maximum int)

...
SUB MIN
BRP KEEPMIN
...

Here you have the current number from the array you are examining in the accumulator A. You subtract what you have as the current minimum and if the result is positive you keep the minimum you have already. Fix this up and you can find the maximum instead.

I thought of changing BRP KEEPMIN to BRZ. Would that make sense in order to find the maximum? I tried it, but it gave me the value of the last input. Would I have to modify the entire code?

Also, I was looking around for Little Man Computer Programs to get practice in understanding how the code works and came across this code:

INP
STA DIVISOR
INP
STA DIVIDEND
LOOPSTART LDA DIVISOR
SUB DIVIDEND
BRP LOOP2
LDA DIVIDEND
SUB DIVISOR
OUT
HLT
LOOP2 STA DIVISOR
BRP LOOPSTART
BRA LOOPSTART
BRA LOOP2
DIVISOR DAT
DIVIDEND DAT

It is supposed to calculate and output the remainder. However, I'm not understanding why this guy used two loops instead of one? Also, I tried it and doesn't always work. Any help trying to understand those codes will be highly appreciate it.

Thanks!
 
This is one of the codes we came up with:

BRA START
ARRAY0 DAT 5
DAT 4
DAT 3
DAT 7
DAT 5
DAT 2
DAT 1
DAT 7
ARRAYINDEX DAT 1
LENGTH DAT 8
MIN DAT 0
ONE DAT 1
LOADINST DAT 501
START LDA ARRAY0
STA MIN
LOOP LDA LOADINST
ADD ARRAYINDEX
STA LOADNEXT
LDA ARRAYINDEX
ADD ONE
STA ARRAYINDEX
LOADNEXT DAT 0
SUB MIN
BRP BIGGER
BRA LOOP
BIGGER ADD MIN
STA MIN
LDA LENGTH
SUB ARRAYINDEX
BRZ DONE
BRA LOOP
DONE LDA MIN
OUT
HLT

But for some reason when I try to change the value, for let's say DAT 3, for DAT 8 in order to output this value, it runs into an infinite loop. I would like to find out why.

Thanks once again for your help!
 
...
SUB MIN
BRP KEEPMIN
...

L1lGh0sT said:
I thought of changing BRP KEEPMIN to BRZ. Would that make sense in order to find the maximum? I tried it, but it gave me the value of the last input.

Because BRZ means branch if the result is zero (ie the numbers are equal). So you only keep the number you have stored if the number you have is equal, in other words you always change it and end up with the last number you looked at stored.

Start with this: you have a candidate for max in the accumulator A. You have the largest number you've seen so far in MIN (I must keep the same label names here). The "SUB MIN" instruction is doing A-MAX. If the result is positive, A is bigger so you want to change what is stored at MIN. If the result is negative, A is smaller so you want to keep MIN the same. The way your code is structured you are making a branch to keep MIN the same ("BRP KEEPMIN"). That corresponds to the condition A is small (the result is negative).

Would I have to modify the entire code?

No, but all the label names are misleading so anyone looking at code computing a max, with all the label names containing 'min' is going to get confused. Some of the thinking seems a bit scattered in the program too, particularly the bit with self-modifying code and computation of array indices. Was this program supplied to you this way? I am not familiar with the LittleMan so I can't really suggest alternate code.

Also, I was looking around for Little Man Computer Programs to get practice in understanding how the code works and came across this code:

INP
STA DIVISOR
INP
STA DIVIDEND
LOOPSTART LDA DIVISOR
SUB DIVIDEND
BRP LOOP2
LDA DIVIDEND
SUB DIVISOR
OUT
HLT
LOOP2 STA DIVISOR
BRP LOOPSTART
BRA LOOPSTART
BRA LOOP2
DIVISOR DAT
DIVIDEND DAT

It is supposed to calculate and output the remainder. However, I'm not understanding why this guy used two loops instead of one? Also, I tried it and doesn't always work. Any help trying to understand those codes will be highly appreciate it.

This code is wrong. Where are you finding these things? :P

Take a look at this for example:

BRP LOOPSTART
BRA LOOPSTART
BRA LOOP2

The "BRA LOOPSTART" means always branch. The following "BRA LOOP2" is never executed.

The remarks also seem to have DIVISOR and DIVIDEND mixed up. The idea in this one is to repeatedly subtract DIVISOR from DIVIDEND until the result is negative. Once negative, you've done one too many subtractions so you should ADD DIVISOR to get the remainder.

You should consider if negative numbers are allowed as input (no if you want to keep it simple).

If you are trying to learn from code like this, you may be better off writing from scratch.
 
L1lGh0sT said:
LOOP LDA LOADINST
ADD ARRAYINDEX
STA LOADNEXT

LDA ARRAYINDEX
ADD ONE
STA ARRAYINDEX

LOADNEXT DAT 0
SUB MIN
BRP BIGGER

BRA LOOP ;*****************

BIGGER ADD MIN
STA MIN

; how do you know if you're done yet?

LDA LENGTH
SUB ARRAYINDEX
BRZ DONE
BRA LOOP

DONE LDA MIN
OUT
HLT

But for some reason when I try to change the value, for let's say DAT 3, for DAT 8 in order to output this value, it runs into an infinite loop. I would like to find out why.

Notice that your program modifies ARRAYINDEX and LENGTH when it runs. If you run it again without reinitializing (I don't know if you can do that), they will not have the correct values. Particularly, LENGTH will be zero after the first run. For the next run, LENGTH would start at zero and be decreased through all numbers until it reached 0 again. If this computer is 16-bit, that would be 65536 iterations.

But there is also a bug in your program. How does the program know it has looked at all the values in the array? It is checking against LENGTH. Each time a value is taken from the array, LENGTH is reduced by one. Once LENGTH is zero, all array values have been checked.

Take a look where I put the ******* up there. At that point in the program, you've decided to keep the value you had and return to the start of the loop to check the next array value. How do you know you're done? You skipped that part and should instead be branching down to the commented part I marked "; how do you know if you're done yet?"

This is actually probably why your program loops forever. You find a max value, never find one smaller so continue to loop without checking if you have run off the end of the array yet.
 
BRA START
ARRAY0 DAT 5
DAT 4
DAT 3
DAT 7
DAT 5
DAT 2
DAT 1
DAT 7
ARRAYINDEX DAT 1
LENGTH DAT 8
MIN DAT 0
ONE DAT 1
LOADINST DAT 501
START LDA ARRAY0
STA MIN
LOOP LDA LOADINST
ADD ARRAYINDEX
STA LOADNEXT
LDA ARRAYINDEX
ADD ONE
STA ARRAYINDEX
LOADNEXT DAT 0
STA TEMP
LDA MIN
SUB TEMP
BRP KEEPMIN
LDA TEMP
STA MIN
KEEPMIN LDA LENGTH
SUB ARRAYINDEX
BRZ DONE
BRA LOOP
DONE LDA MIN
OUT
HLT
TEMP DAT 0

Heres your working code. made very simple changes. took me 1 minute
 
Besides, I'm pretty sure the take home quiz for CMPT-280 was already due last friday...so much for agreeing to not seek help. Ha. Let me know if you need anything else tho.
 
Also, the multiper code she gave us (the one you posted) works 100%. turns out she made a mistake and we get a free point on that one. I spent several hours and couldn't find anything wrong with it. lol.
 
Back
Top