0

This is the code in JavaScript that I want to convert to LMC assembly code:

<!DOCTYPE html>
<html>
<body>
<script>
var temp = 14;
var y = 2;
temp <<= y;
document.write(temp);
</script>
</body>
</html>

My task is to write a program for LMC that would produce the same results to change the y value.

Here is what I have so far:

LOOP LDA COUNT
ADD ONE
STA COUNT
LDA TOTAL
ADD TEMP
STA TOTAL
LDA Y
SUB COUNT
BRZ ENDLOOP
BRA LOOP
ENDLOOP LDA TOTAL
STA TEMP
LDA TEMP
ADD TEMP
OUT
HLT
ONE DAT 001
COUNT DAT
TOTAL DAT
TEMP DAT 14
Y DAT 2

It works for a y value of 2 but not for any other value like 3,4 etc

Any thoughts?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
JACK13
  • 1
  • 1

2 Answers2

1

In words:

Read R0 and R1 from Input
while R1 > 0 {
    Subtract 1 from R1
    Add R0 to itself
}
Output R1

In LMC assembler:

     INP
     STA R0
     INP
     STA R1
LOOP LDA R1
     BRZ END
     SUB ONE
     STA R1
     LDA R0
     ADD R0
     STA R0
     BRA LOOP
END  LDA R0
     OUT

R1   DAT
R0   DAT
ONE  DAT 1

You can see this code running here: Shift left in LMC Emulator.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
0

What you are trying to produce is a bitshift program that shifts the values tot he left. The below code should work. (Do not include ellipsis or anything after ellipsis)

     inp 
     sta value 
     inp 
     sta shift 
     brz done ... do nothing 
     loop lda value .... return here to shift one bit to the left 
     add value 
     sta value 
     lda shift ... decrement and test bit shift counter 
     sub one 
     sta shift 
     brz done ... done if count is zero 
     bra loop ... else shift at least one more bit 
     done lda value .... arrive here when all shifts are done 
     ... output, halt and data definitions follow `enter code here`
The Boat
  • 27
  • 7