-1

I need a bit of help to code how to divide a sum of numbers, divided by n numbers put in. For instance, the sum is 10, with 2 numbers, the answer should be 5. But if the sum is 14 and the total input (n numbers) are 3, it should write out 4, ignoring the 2 remainders, og for example, sum equals to 16 and total inputs equal to 3, the output should be 5, ignoring the 1 in remainder. But I don't know how to have these two in one code without interfering with each other. This is my current code

START   INP 
        BRZ SLUTT 
        ADD sum 
        STA sum 
        LDA antall 
        ADD en 
        STA antall 
        BRP START 
SLUTT   LDA sum 
        OUT 
        LDA linjeskift 
        OTC 
        OTC
        OTC
        LDA n 
        OTC 
        LDA likhetstegn 
        OTC 
        LDA antall 
        OUT 
        LDA linjeskift 
        OTC 
        OTC
        OTC
        LDA d 
        OTC 
        LDA likhetstegn 
        OTC 
start   LDA sum 
        BRZ slutt
        SUB antall 
        STA sum 
        LDA resultat 
        ADD en 
        STA resultat 
        BRP start
slutt   LDA resultat 
        OUT 
        HLT 
sum     DAT 
resultat DAT 0 
antall  DAT 0 
en      DAT 1 
a       DAT 97 
d       DAT 100 
n       DAT 110 
linjeskift DAT 13 
likhetstegn DAT 61 
ali03
  • 11
  • 2
  • Which LMC simulator are you using for this? – trincot Sep 23 '22 at 16:51
  • Using the Peter Higginson one, we are not allowed to use any other unfortunately. Here's the link if necessary; https://peterhigginson.co.uk/lmc/ – ali03 Sep 23 '22 at 17:06

1 Answers1

1

The main problem is that your BRP instruction, in the division part of the code, comes too late. It is supposed to check whether the subtraction of antall from sum still was non-negative, but as your code continues first by loading the resultat into the accumulator, the detection of the negative result can no longer happen... BRP will find that the latest operation (the update of resultat) was non-negative.

In short, that second part of the code should perform a BRP right after SUB. In LMC this is a general rule for doing it right: always have a BRP immediately after a SUB.

Some other comments:

  • Once you make the above correction, the BRZ slutt isn't really necessary anymore, as surely the next SUB will then return a negative result and trigger the output anyway. (but see next point)
  • On the other hand, if the number of inputs (antall) is zero, we would be dividing by zero, which comes down to an infinite loop. This should be avoided by making an extra check.
  • I did not quite get why your code has the middle part, where you print characters using OTC. It seems unrelated to the goal of the challenge.
  • Labels should be unique. Although they work fine on Peter Higginson's simulator, some LMC simulators do not distinguish between uppercase and lowercase labels, so it is better practice to use really distinctive labels, irrespective of case.
  • LMC provides a way to reset the program counter, which does not clear the mailboxes to their initial values. So for instance, a reset would not set sum or resultat to zero. It is therefore good practice to start the program with the initialisation of such "variables", so that the program will still behave correctly when the user uses the reset feature.
  • The first part of the code has a BRP which really should always branch, so it would be more natural to use BRA. It also works with BRP, as antall will not get negative, but it seems more intuitive to use the unconditional jump instruction here.

Here is the updated code, with the above remarks taken into account:

init     LDA zero    # Initialise variables before starting
         STA resultat
         STA antall
         STA sum
start    INP 
         BRZ validate 
         ADD sum 
         STA sum 
         LDA antall 
         ADD en 
         STA antall 
         BRA start   # use BRA instead of BRP 
validate LDA antall  # check against division by zero
         BRZ slutt
divide   LDA sum
         SUB antall   
         BRP continue # Important: SUB followed by BRP!
slutt    LDA resultat 
         OUT 
zero     HLT 
continue STA sum 
         LDA resultat 
         ADD en 
         STA resultat 
         BRA divide
sum      DAT
resultat DAT 
antall   DAT 
en       DAT 1 


<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.816/lmc.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286