Debugging LC3 Programs

For debugging, consider using:

  • %pc ADDRESS
  • %step
  • %cont
  • %bp ADDRESS
In [13]:
.ORIG x3000
0101 000 000 1 00000 ; R0 <- R0 and 0
0000 010 000000101   ; BR if zero to next, next statement
0001 000 000 1 00001 ; R0 <- R0 + 1
0001 000 000 1 00010 ; R0 <- R0 + 2
0001 000 000 1 00011 ; R0 <- R0 + 3
0001 000 000 1 00100 ; R0 <- R0 + 4
0001 000 000 1 00101 ; R0 <- R0 + 5
1111 0000 00100101   ; HALT
.END
============================================================
Memory dump:
============================================================
           x3000: x5020
           x3001: x0405
           x3002: x1021
           x3003: x1022
           x3004: x1023
           x3005: x1024
           x3006: x1025
           x3007: xF025

============================================================
Registers:
============================================================
PC: x3008
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [14]:
%exe
============================================================
Computation completed
============================================================
Instructions: 3
Cycles: 21 (0.000010 milliseconds)

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3008 
In [2]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3006
(1/6) BR x3006 (or 0) (x3006*: x0000)
Attempting to execute NOOP at x3005

For help on the meta commands of LC3 in Jupyter, issue a question mark in a cell:

?

A window will show these contents:

This is the Calysto LC3 Jupyter kernel.

LC3 Interactive Magic Directives: 

 %bp [clear | SUSPENDHEX]           - show, clear, or set breakpoints
 %cont                              - continue running
 %dis [STARTHEX [STOPHEX]]          - dump memory as program
 %dump [STARTHEX [STOPHEX]]         - list memory in hex
 %exe                               - execute the program
 %mem HEXLOCATION HEXVALUE          - set memory
 %pc HEXVALUE                       - set PC
 %reg REG HEXVALUE                  - set register REG to HEXVALUE
 %regs                              - show registers
 %reset                             - reset LC3 to start state
 %step                              - execute the next instruction, increment PC

HEX values begin with an 'x' and are composed of 4 0-F digits or letters.

To get additional help on these items, use '%help %item'.

To see additional magics, use %lsmagic, and put a question mark after a magic 
name.
In [3]:
%pc x3000
    PC <= x3000

============================================================
Registers:
============================================================
PC: x3000
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [4]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3001
(1/6) AND R0, R0, #0 [1] (x3001*: x5020)
    R0 <= x0000
    NZP <= (0, 1, 0)

============================================================
Registers:
============================================================
PC: x3001
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [5]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3002
(2/12) BRz x3003 (or 1) [2] (x3002*: x0401)
    PC <= x3003
    True - branching to x3003

============================================================
Registers:
============================================================
PC: x3003
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [6]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3004
(3/18) ADD R0, R0, #2 [4] (x3004*: x1022)
    R0 <= x0002
    NZP <= (0, 0, 1)

============================================================
Registers:
============================================================
PC: x3004
N: 0 Z: 0 P: 1 
R0: x0002 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [7]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3005
(4/27) HALT [5] (x3005*: xF025)
    R7 <= x3005
    PC <= x048E

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1 
R0: x0002 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3005 
In [9]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x0490
(6/42) PUTS (x0490*: xF022)
    R7 <= x0490
    PC <= x0456

============================================================
Registers:
============================================================
PC: x0456
N: 0 Z: 0 P: 1 
R0: x04B0 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0490 

Trying a breakpoint.

In [10]:
%bp x3004
============================================================
Breakpoints
============================================================
    1)            x3004: xF025
In [11]:
%exe
...breakpoint hit at x3004
============================================================
Computation SUSPENDED
============================================================
Instructions: 3
Cycles: 18 (0.000009 milliseconds)

============================================================
Registers:
============================================================
PC: x3004
N: 0 Z: 0 P: 1 
R0: x0002 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [12]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3005
(4/27) HALT [5] (x3005*: xF025)
    R7 <= x3005
    PC <= x048E

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1 
R0: x0002 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3005 
In [13]:
%cont

--- halting the LC-3 ---

============================================================
Computation completed
============================================================
Instructions: 329
Cycles: 2723 (0.001362 milliseconds)

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 1 P: 0 
R0: x0000 R1: x7FFF R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0495