1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Little Man Computer- Find Max / Multiply two Numbers

  1. Oct 23, 2012 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant 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



    3. 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
     
  2. jcsd
  3. Oct 23, 2012 #2
    Is that self-modifying code in the find min function?


     
    Last edited: Oct 24, 2012
  4. Oct 25, 2012 #3
     
  5. Oct 26, 2012 #4
    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!
     
  6. Oct 26, 2012 #5
    ....
    SUB MIN
    BRP KEEPMIN
    ...

    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).

    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.

    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.
     
  7. Oct 26, 2012 #6
    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.
     
  8. Oct 30, 2012 #7
    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
     
  9. Oct 30, 2012 #8
    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.
     
  10. Oct 30, 2012 #9
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Little Man Computer- Find Max / Multiply two Numbers
Loading...