0

Writing a program in assembly to print two characters from input, by first comparing them and printing the one with the larger ASCII code first. I wrote what makes sense to me, but the assembler is giving me the error "Duplicate label(R2)". Does anyone have any idea why this wouldn't work?

.orig 3000 GETC LD R1,R0 GETC LD R2,R0 NOT R0,R2 ADD R0,R0,#1 ADD R0,R1,R0 BRN #3 OUT R1 OUT R2 BRNZP #2 OUT R2 OUT R1 HALT .end

Would also add that when I break the code and just take out the lines with R2, it spits out errors for the other registers. It's like the assembler is recognizing the registers as labels.

Evan Jones
  • 43
  • 7

1 Answers1

1

LD R2,R0

You don't use LD this way.

The syntax is LD DR, Label or LD DR, PCOffset9

Also OUT R1

OUT is a trap that prints out the character in R0 and only R0

Perhaps the assembler has a bug that isn't marking these mistakes as syntax errors?

Brandon
  • 438
  • 5
  • 4
  • Is `R0` also a valid label/symbol name? That would explain why it's not a syntax error, but only eventually a link error (if other errors don't stop the assembler first). Some assemblers (like NASM for x86) [let you use `$eax` to refer to the label `eax`](https://stackoverflow.com/questions/45890645/symbol-name-conflicts-with-new-register-names-in-new-nasm-versions) in contexts where it's ambiguous between the label and the register. – Peter Cordes Feb 09 '18 at 07:55
  • And BTW, for future readers: the LC-3 load instruction that takes an address in a register is `LDI` (load indirect). – Peter Cordes Feb 09 '18 at 07:56
  • R0 is not a valid label name. Also I believe you meant LDR. LDI still takes a Label or PCOffset. The difference there is that it will load from memory twice. LDR takes a Base Register and an Offset and treats both as an address to load from. – Brandon Feb 09 '18 at 08:06
  • Oops yes, my brief skimming was apparently too brief. But yeah, if register names are reserved even in contexts where you can't use a register name, then the assembler should catch that error (unless it treats `ld` as a pseudo-op that can assemble to LD or LDR). – Peter Cordes Feb 09 '18 at 08:16
  • 1
    Yeah Instructions are case insensitive/ ld would be interpreted as the load instruction. AFAIK there are no pseudo-ops in the ISA officially other than the traps OUT, PUTS, GETC, PUTSP, IN, and HALT And yeah at least with the book's tools the assembler reports the following error "Expected label or 9 bit signed PC offset, but found 'R0' instead." And just noticed my own lc3 assembler/simulator will incorrect report undefined symbol R0... – Brandon Feb 09 '18 at 08:30