Ryan Hickman
Ryan Hickman

Reputation: 23

Assembly Language questions. Little Man Computer Program

I am trying to write two different programs in LMC using an old assembly language. The first as is follows:

Write a LMC program that takes two inputs, a number to count down from, and the step value (example: count down from 35 by 5), until it reaches zero. It should print each step, and the lastnumber before zero if the sequence doesn’t end on zero, but if the number goesto negative, that number should not be printed. Example output using 13 and 3 as inputs would be: 13, 10, 7, 4, 1. Example output using 12 and 3 as inputs wouldbe: 12, 9, 6, 3, 0.

I have tried something along the lines of this:

IN 
STO 91 
IN 
STO 92 
IN 
STO 93 
LDA 91 
ADD 92 
SUB 93 
OUT 
HLT 

Which does not work

The second LMC program I am trying to write is as follows:

Write the code to calculate the area and perimeter of a triangle. The program should take 3 inputs, which are, in order, the base, height, and the third side of the triangle. The code should produce 2 values in the output box, the first number will be the area and thesecond, the perimeter. (Hint: you’ll probably want to use DAT statements tostore some values to start).

I do not even know where to start on this one.

Any help would be appreciated.

I also have some example codes to help:

Calculate perimeter & area - takes L & W as inputs prints out perimeter first, then area.

 00 LDA #01;
 01 STA 99;
 02 IN;
 03 STA 98;
 04 LDA #00;
 05 STA 96;
 06 IN;
 07 STA 97;
 08 ADD 97;
 09 ADD 98;
 10 ADD 98;
 11 OUT;
 12 LDA 96;
 13 ADD 98;
 14 STA 96;
 15 LDA 97;
 16 SUB 99;
 17 STA 97;
 18 SKZ;
 19 JMP 12;
 20 LDA 96;
 21 OUT;
 22 HLT;

MULTIPLIES TWO NUMBERS.

 00 IN;
 01 STA 99;
 02 STA 97;
 03 IN;
 04 STA 98;
 05 SUB 90;
 06 STA 98;
 07 SKZ;
 08 JMP 12;
 09 LDA 97;
 10 OUT;
 11 HLT;
 12 LDA 97;
 13 ADD 99;
 14 STA 97;
 15 LDA 98;
 16 JMP 05;
 90 DAT 001;

ADDS THE FIRST NUMBER INPUT TO ITSELF, THEN SUBTRACTS THE SECOND INPUT FROM THE TOTAL

 00 IN;
 01 STA 90;
 02 IN;
 03 STA 91;
 04 LDA 90;
 05 ADD 90;
 06 SUB 91;
 07 OUT;
 08 HLT;

ONE WAY OF COUNTING FROM AN INPUT DOWN TO ZERO

 00 IN;
 01 OUT;
 02 SUB 98;
 03 SKP;
 04 JMP 06;
 05 JMP 01;
 06 HLT;
 98 DAT 002;

SQUARING A NUMBER

00 IN; take input
01 STA 99; save value as a mulitiplier
02 STA 97; save value as multiplicand
03 SUB 90; subtract one from counter
04 STA 98; copy value to counter
05 LDA 97; load our accumulator number
06 ADD 99; Add original number
07 STA 97; store intermediate sum
08 LDA 98; Load counter
09 SUB 90; subtract one from counter
10 STA 98; Store counter value
11 SKZ; skip if counter is at zero
12 JMP 05; othewise, loop back to 5 
13 LDA 97; load our squared number
14 OUT; Write output
15 HLT; stop 
90 DAT 01; countdown value 

Upvotes: 0

Views: 7523

Answers (1)

Paul Hankin
Paul Hankin

Reputation: 58271

In pseudocode:

Input R0 and R1
repeat {
    Output R0
    Subtract R1 from R0
} until the result goes negative

And in LMC assembler:

     INP
     STA R0
     INP
     STA R1
LOOP LDA R0
     OUT
     SUB R1
     STA R0
     BRP LOOP
     HLT
R0   DAT
R1   DAT

You can see the code working here: Decrement by steps on LMC Emulator.

Upvotes: 0

Related Questions