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

In summary: LDA SIGN BRZ :BRA SUB ONE BRZ :BRA OUT ZERO// If the sign is 1 then the number must be positive, so subtract 1 from the input number and store the result in the variable "ONE". LDA SIGN BRZ :SUB SUB ONE
  • #1
DanjoJojo
8
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
  • #2
  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:

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

What is the LMC Simulator?

The LMC Simulator is a virtual tool used to simulate the functionality of the Little Man Computer (LMC), a simplified computer architecture used for educational purposes. It allows users to write and execute programs in the LMC language, which is based on the concept of a 3-digit 10's complement calculation.

How do I access the LMC Simulator?

The LMC Simulator can be accessed through various online platforms or can be downloaded as a standalone application. Some popular online platforms include Little Man Computer and LMC Simulator. Alternatively, you can download the application from its official website or other trusted sources.

What is a 3-digit 10's complement calculation?

A 3-digit 10's complement calculation is a mathematical operation used in the LMC language to perform addition and subtraction. It involves representing numbers in a 3-digit format and using the 10's complement method to perform calculations. This method is based on the concept of representing negative numbers using their complement to 10.

How do I write a program in the LMC language?

To write a program in the LMC language, you need to have a basic understanding of its syntax and commands. You can refer to the LMC Simulator's documentation or online tutorials for guidance. Once you have written your program, you can input it into the simulator and execute it to see the results.

Can the LMC Simulator be used for other types of calculations?

Yes, the LMC Simulator can be used for other types of calculations besides the 3-digit 10's complement calculation. It supports various arithmetic operations, such as addition, subtraction, multiplication, and division, as well as input/output functions. However, its primary purpose is to teach the fundamentals of computer architecture and programming, so it may not be suitable for complex calculations.

Similar threads

Replies
18
Views
6K
Replies
8
Views
10K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
0
Views
1K
Back
Top