Assembly -(LMC) I'm supposed to make a program that takes 2 inputs of number but outputs the larger number

58 Views Asked by At
         INP
        STA NUM1
        INP
        STA NUM2

      
        SUB NUM1
        BRP NUM2SHOW
        LDA NUM1
        SUB NUM2
        BRP NUM1SHOW

#NUM1   > NUM2
NUM1SHOW LDA NUM1 
        OUT
        HLT
#NUM2   > NUM1
NUM2SHOW LDA NUM2
        OUT
        HLT
NUM1    DAT
NUM2    DAT

Here is the code block, just looking at it, it seems like it's correct but for some reason it does not show anything when I ran the code.

I'm expecting for it to show the larger number. I've been using 4 as the first number at 5 as the second number, though like I said, it does not show anything at all.

1

There are 1 best solutions below

2
Erik Eidt On

Your program works in several different simulators, so your problem is probably a misunderstanding in using the tooling.

There's at least 5 LMC simulators out there, mostly web-based / online.  I like https://blog.paulhankin.net/lmc/lmc.html for an LMC simulator as a simple and effective LMC simulator.


To critique your program, there's more logic than needed for this.

Your doing something like:

if x < y
    print y;
if x > y
    print x;

This test for x < y and then x > y is redundant.  We would normally write something like this using an if-then-else, which chooses to run the then-part or the else-part with a single condition test1:

if x < y
    print y;
else
    print x;

While relevant, the above doesn't fully capture the redundancy.  Let's switch to if-goto-label to see more:

    if x < y goto print_y;
    if x > y goto print_x;
print_x:
    print x;
    (halt)
print_y:
    print y;
    (halt)

Here you can see that the second if statement either does the goto print_x or doesn't.  But where does it go next when it doesn't run the goto in the then-part?  It goes to print_x: by default as the next statement.  So, that means the second test either does goto print_x or doesn't but either way still ends up executing print_x: as next statement after — so, that if statement doesn't do anything to inform the program.


1  To do an if-then-else in assembly we test a condition, and goto the else part if the condition isn't met.  The then-part will execute when the condition is met, and then then-part must, when completed, skip the else-part.  In other words, the control flow either executes the then-part or the else-part, but only one of them — the other one is skipped one way or the other.

if x > y
    print x;
else
    print y;
(halt) // stop here whether the then or the else executed
    if x < y goto else1;
    print x;
    goto endif1;  // if we get here, we ran the then-part, so skip over the else-part
else1:
    print y;
endif1:
    (halt)