0

I have a program I'm doing and I want to input two numbers so that the LMC can output the smallest one. MY code is giving me the biggest number, help me fix it.

INP
STA first
INP
STA second

SUB second
BRP secondBig
LDA second
OUT
BRA endProgram
secondBig LDA second
OUT
endProgram HLT
first DAT
second DAT
Jeff
  • 13
  • 1
  • 3
  • 1
    Fixing simple problems like this yourself is a much better way of *learning* than just copying someone else's solution. If your goal is to learn. – rici Oct 20 '17 at 18:58
  • Or at least find someone better to copy from :) – Scott Hunter Oct 20 '17 at 19:07
  • As Scott Hunter already pointed out, you're printing the second value in both branches. Also note that you're testing **second - second**, which will not give you any useful information. Please give us the pseudo-code you are trying to implement. – Prune Oct 20 '17 at 20:27

3 Answers3

0

Invert your conditional branch: use BRN (branch negative) instead of BRP. Alternately, switch the logic branches that decide which value will be available for output.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

You're storing both input values in first, which means you overwrite the first one with the second.

Both before and after fixing the above, your code isn't outputting the biggest, it is outputting the second (regardless of relative size). Single-stepping, or actually reading the code, should tell you why.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

The problem was that you compared the second input with itself. You could insert LDA first just before SUB second, but if you invert the output blocks, you can avoid that extra LDA

It is also confusing that you have a label "secondBig", when in fact you are not looking for the biggest number, but the smallest

Correction:

#input: 4 7
           INP
           STA first
           INP
           STA second

           SUB first
           BRP firstLeast

           LDA second     
           OUT
           BRA endProgram
           
firstLeast LDA first
           OUT
           
endProgram HLT

first      DAT
second     DAT

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