LC-3 Assembly Code Error detection

Click For Summary
SUMMARY

The discussion focuses on an error in a fragment of LC-3 assembly code. The code initializes registers, performs arithmetic operations, and utilizes the TRAP x21 instruction, which outputs to the monitor. The main issue arises from the manipulation of the R7 register, which is used for the return address but has an unknown initial value. This uncertainty can lead to unpredictable behavior when the program returns from the subroutine.

PREREQUISITES
  • Understanding of LC-3 assembly language syntax and structure
  • Familiarity with LC-3 instruction set, specifically TRAP instructions
  • Knowledge of register operations in assembly programming
  • Basic concepts of subroutine calls and return addresses
NEXT STEPS
  • Review the LC-3 instruction set documentation for detailed command functions
  • Learn about the proper initialization of registers in assembly programming
  • Study the behavior of TRAP x21 and its implications on program flow
  • Explore debugging techniques for assembly code to identify runtime errors
USEFUL FOR

Students learning assembly language, educators teaching computer architecture, and developers working with low-level programming in LC-3.

elec_is_gross
Messages
1
Reaction score
0

Homework Statement



What is wrong with the code fragment:
.ORIG x3000
AND R0,R0, #0
AND R2,R2, #0
ADD R2,R2, #7
JSR SUB
ADD R2,R2, ASCII
ADD R0,R2,#0
TRAP x21
SUB ADD R2,R2,#9
ADD R7,R7,#1
RET
ASCII .FILL x0030
.END

The Attempt at a Solution



Ok, so first we ckear some register values. Then we set R2 to 7. Next we go into a subroutine that adds 9 to R2, so now R2 is 16. Then we add 1 to R7, which has an unknown value at the start. Then we return to the command after JSR. This adds the value in ASCII to R2, making R2 64. Then we set R0 to 64 as well, and finish by executing Trap x21.

I don't know what the problem here is, nor what the output is. I think Trap x21 does something with the keyboard to monitor, but I'm not sure.
 
Physics news on Phys.org
elec_is_gross said:
I think Trap x21 does something with the keyboard to monitor, but I'm not sure.
Trap x21 outputs to the monitor, then returns. R7 is used for the return address. What is going to happen with your program when trap x21 returns?

link to lc-3 instruction set pdf:

http://classes.soe.ucsc.edu/cmpe012/Summer09/notes/06_LC3_ISA_and_Addressing_Modes.pdf

link to assembly language pdf:

http://classes.soe.ucsc.edu/cmpe012/Winter09/lectures/06_LC3_Assembly.pdf

link to lc-3 trap pdf:

http://classes.soe.ucsc.edu/cmpe012/Summer08/notes/11_LC3_TRAP_markup.pdf

link to another description of the instruction set:

http://www.scribd.com/doc/4596293/LC3-Instruction-Details
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
10
Views
10K
Replies
3
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
11
Views
6K