Getting Started with LMC Simulator for 3-Digit 10's Complement Calculation

AI Thread Summary
The discussion focuses on creating an LMC program for three-digit ten's complement calculations based on user inputs for sign and magnitude. The program should validate inputs, providing outputs that indicate validity and the correct result or an error code. Users are encouraged to improve code readability by using comments, proper labeling, and formatting for better debugging. Suggestions include avoiding numeric labels and ensuring that branch instructions are clearly labeled to enhance understanding. The conversation highlights the importance of clarity in coding to facilitate troubleshooting and effective communication among programmers.
DanjoJojo
Messages
8
Reaction score
2
New poster has been reminded to post schoolwork problems in the Homework Help forums
Summary:: So I'm trying to get more advanced with the lmc simulator, https://www.101computing.net/LMC/
But it's kind of hard to understand and keep track when it gets long.

Provide an LMC program that will provide a value in three digits ten's complement. The program will take two inputs, a sign and a magnitude.

A valid first input may be zero (0) if the input is positive or one (1) if the input is negative.
The second input is a value from 0 to 500. The program will provide two outputs. The first output is zero (0) if the inputs are invalid and one (1) if the inputs are valid. The second output will be 999 if the input is invalid or the correct result if the input is valid.

Hence, the inputs "0" and "123" will result in the outputs of "1" and "123". The inputs of "1" and "123" will result in the outputs of "1" and "877". The inputs of "2" and "123" will result in the outputs of "0" and "999" (since '2' is an invalid input).

Note: You may assume that 2 inputs will always be provided (i.e. you don't need to worry about a "lost" input ... also known as "an omission error").

This is what I've tried so far...
Code:
INP
    STA 99
    INP
    STA 98

    LDA 99
    BRZ POS
    SUB ONE
    BRZ 14
    OUT ZERO
    LDA 38
    OUT
    HLT
POS    OUT ONE
    BRA 16
    OUT ONE
    BRA 26
    LDA 98
    SUB 39
    BRP 24
    LDA 98
    SUB 38
    ADD ONE
    OUT
    HLT
    OUT 38
    HLT
    LDA 98
    SUB 40
    BRP 34
    LDA 98
    SUB 38
    ADD ONE
    OUT
    HLT
    OUT 38
    HLT
  

ONE  DAT 1
ZERO DAT 0
38 DAT 999
39 DAT 499
40 DAT 500

I'm not able to get the 1 or 0 output though, what am I doing wrong?

[Code tags added by a Mentor]
 
Last edited by a moderator:
Physics news on Phys.org
  1. Wrap your code in [code]...[/code] tags when you post it here so it is a bit easier to read (it doesn't work as well with assembler as some other languages but it is still better than otherwise).
  2. It also helps if you use spaces to line up your code instead of tabs; a good programmers text editor will help here (I recommend Visual Studio Code). I also like to have a tab between the instruction and the arguments instead of a space: this is less important when all instruction mnemonics are the same length.
  3. Always use labels for branch instructions otherwise it is very difficult to know where BRZ 14 is going.
  4. It is slightly less important to labels for data addresses like (ONE and ZERO), but still a good idea.
  5. Use prefixes for labels so you never use a memory location for a branch or vice versa: I have used a colon ':' for branch locations and left memory labels unprefixed.
  6. Never use numeric labels (you have 38, 38 and 40: have these been inserted by a disassembler?)
  7. Write comments so you (and we) can see what you intend to do with each statement: this mkes it much easier to tell when a statement is not doing what it should.
    1. Oh - the version of the LMC you linked to does not seem to allow comments. That is not good.
So here is my version of your code:
Code:
// Input the sign flag into location 99 and the number into loc 98.
        INP
        STA     99
        INP
        STA     98

// Skip the next bit if the sign flag is zero (why are we doing this?).
        LDA     99
        BRZ     :POS
        SUB     ONE
        BRZ     :L14
        OUT     ZERO
        LDA     M38
        OUT
        HLT
:POS    OUT     ONE
        BRA     :L16
:L14    OUT     ONE
        BRA     :L26
:L16    LDA     98
        SUB     M39
        BRP     :L24
        LDA     98
        SUB     M38
        ADD     ONE
        OUT
        HLT
:L24    OUT     M38
        HLT
:L26    LDA     98
        SUB     M40
        BRP     :L34
        LDA     98
        SUB     M38
        ADD     ONE
        OUT
        HLT
:L34    OUT     M38
        HLT

ONE     DAT     1
ZERO    DAT     0
M38     DAT     999
M39     DAT     499
M40     DAT     500

Given that you can't input comments I suggest you rewrite your code using more explanatory labels like the below; this will make it easier to spot where you are going wrong.

Code:
        INP
        STA     SIGN
        INP
        STA     INPUT

        LDA     SIGN
        BRZ     :ISPOS
        SUB     ONE
        BRZ     :ISNEG
        OUT     ZERO
// If I get here then the sign that was entered is neither 0 nor 1: what should I do next?
 
Last edited:
Back
Top