New Stuff!

New Assembly Directives

  • .BLKW - block of memory; takes decimal or hex only
  • .FILL - a memory location; decimal or hex only
  • .STRINGZ - a zero-padded, zero-terminated ASCII "string"
In [11]:
.ORIG x3000
.BLKW 1                     ; simple variable (or .FILL 0)
.FILL x1234                 ; simple initialized variable
.BLKW 10                    ; array of ten ints (initialized to 0)
.FILL xFFFF                 ; simple initialized variable
.END
Assembled! Use %dis or %dump to examine; use %exe to run.
In [12]:
%dump x3000 x300C
============================================================
Memory dump:
============================================================
           x3000: x0000
           x3001: x1234
           x3002: x0000
           x3003: x0000
           x3004: x0000
           x3005: x0000
           x3006: x0000
           x3007: x0000
           x3008: x0000
           x3009: x0000
           x300A: x0000
           x300B: x0000
           x300C: xFFFF
In [13]:
.ORIG x3100
.STRINGZ "abcdefg"
.END
Assembled! Use %dis or %dump to examine; use %exe to run.
In [14]:
%dump x3100 x310A
============================================================
Memory dump:
============================================================
           x3100: x0061
           x3101: x0062
           x3102: x0063
           x3103: x0064
           x3104: x0065
           x3105: x0066
           x3106: x0067
           x3107: x0000
           x3108: x0037
           x3109: x0038
           x310A: x0000
In [15]:
.ORIG x3100
.STRINGZ "012345"
.STRINGZ "678"
.END
Assembled! Use %dis or %dump to examine; use %exe to run.
In [16]:
%dump x3100 x310A
============================================================
Memory dump:
============================================================
           x3100: x0030
           x3101: x0031
           x3102: x0032
           x3103: x0033
           x3104: x0034
           x3105: x0035
           x3106: x0000
           x3107: x0036
           x3108: x0037
           x3109: x0038
           x310A: x0000

New TRAP Instructions

TRAP instructions are called "service calls" because they "call" a service of the Operating System. Generally, thay are composed of:

Opcode 0000 Trapvector
1111 0000 0000 0000

The Trapvector is 8 bits that indicate a location in memory. Of course the LC-3 has 16 bit addresses, but we only specify 8 here.

  • 1111 0000 0010 0011 - x23 is IN
  • 1111 0000 0010 0001 - x21 is OUT
In [25]:
%dump x0000
============================================================
Memory dump:
============================================================
           x0000: x0495
           x0001: x0495
           x0002: x0495
           x0003: x0495
           x0004: x0495
           x0005: x0495
           x0006: x0495
           x0007: x0495
           x0008: x0495
           x0009: x0495
           x000A: x0495
           x000B: x0495
           x000C: x0495
           x000D: x0495
           x000E: x0495
           x000F: x0495
           x0010: x0495
           x0011: x0495
           x0012: x0495
           x0013: x0495
           x0014: x0495
           x0015: x0495
           x0016: x0495
           x0017: x0495
           x0018: x0495
           x0019: x0495
           x001A: x0495
           x001B: x0495
           x001C: x0495
           x001D: x0495
           x001E: x0495
           x001F: x0495
           x0020: x044C
           x0021: x0450
           x0022: x0456
           x0023: x0463
           x0024: x046F
           x0025: x048E
           x0026: x0495
           x0027: x0495
           x0028: x0495
           x0029: x0495
           x002A: x0495
           x002B: x0495
           x002C: x0495
           x002D: x0495
           x002E: x0495
           x002F: x0495
           x0030: x0495
           x0031: x0495
           x0032: x0495
           x0033: x0495
           x0034: x0495
           x0035: x0495
           x0036: x0495
           x0037: x0495
           x0038: x0495
           x0039: x0495
           x003A: x0495
           x003B: x0495
           x003C: x0495
           x003D: x0495
           x003E: x0495
           x003F: x0495
           x0040: x0495
           x0041: x0495
           x0042: x0495
           x0043: x0495
           x0044: x0495
           x0045: x0495
           x0046: x0495
           x0047: x0495
           x0048: x0495
           x0049: x0495
           x004A: x0495
           x004B: x0495
           x004C: x0495
           x004D: x0495
           x004E: x0495
           x004F: x0495
           x0050: x0495
           x0051: x0495
           x0052: x0495
           x0053: x0495
           x0054: x0495
           x0055: x0495
           x0056: x0495
           x0057: x0495
           x0058: x0495
           x0059: x0495
           x005A: x0495
           x005B: x0495
           x005C: x0495
           x005D: x0495
           x005E: x0495
           x005F: x0495
           x0060: x0495
           x0061: x0495
           x0062: x0495
           x0063: x0495
In [23]:
.ORIG x3200
1111 0000 0010 0011
1111 0000 0010 0101
.END
    PC <= x3200
    memory[x3200] <= xF023
    PC <= x3201
    memory[x3201] <= xF025
    PC <= x3202
Assembled! Use %dis or %dump to examine; use %exe to run.
In [24]:
%pc x3200
    PC <= x3200

============================================================
Registers:
============================================================
PC: x3200
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [25]:
%cont
Input a character> a
a
============================================================
Computation completed
============================================================
Instructions: 260
Cycles: 2166 (0.001083 milliseconds)

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1 
R0: x0061 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3202 
In [28]:
.ORIG x3200
1111 0000 0010 0011
1111 0000 0010 0001
1111 0000 0010 0101
.END
Assembled! Use %dis or %dump to examine; use %exe to run.
In [29]:
%exe
Input a character> %
%
%============================================================
Computation completed
============================================================
Instructions: 267
Cycles: 2230 (0.001115 milliseconds)

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

All of the TRAP Instructions

Detailed description on page 543.

  • x20: GETC - no echo
  • x21: OUT - ouput character
  • x22: PUTS - output string
  • x23: IN - get a character
  • x24: PUTSP - two chars per word, [7:0] right-first, then left [15:8]
  • x25: HALT - infinite loop

Practice

  • Q1: Write the standard "Hello, World!" program in machine language
  • Q2: Get a number (ASCII character) from the user and add 1 to it; show result
  • Q3: Get a number (ASCII character, 0 - 4) from the user and double it; show result
  • Q4: display a -1 on the screen

Q1:

In [10]:
.ORIG x3000
1110 000 000000010       ;; Put address of "Hello, World!" in R0
1111 0000 0010 0010      ;; PUTS
1111 0000 0010 0101      ;; HALT
.STRINGZ "Hello, world!" ;; DATA
.END
Assembled! Use %dis or %dump to examine; use %exe to run.
In [11]:
%exe
Hello, world!============================================================
Computation completed
============================================================
Instructions: 156
Cycles: 1291 (0.000646 milliseconds)

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

Q2:

In [12]:
.ORIG x3100
1111 0000 0010 0000  ;; GETC
0001 000 000 1 00001 ;; ADD
1111 0000 0010 0001  ;; OUT
1111 0000 0010 0101  ;; HALT
.END
Assembled! Use %dis or %dump to examine; use %exe to run.
In [13]:
%exe
2
3============================================================
Computation completed
============================================================
Instructions: 14
Cycles: 124 (0.000062 milliseconds)

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

Q3:

In [1]:
.ORIG x3100
1111 0000 0010 0000  ;; GETC into R0
0101 001 001 1 00000 ;; put 0 in R1
0001 001 001 1 1 0000 ;; put x30 in R1; ADD 
0001 001 001 1 1 0000 ;; put x30 in R1; ADD
0001 001 001 1 1 0000 ;; put x30 in R1; ADD
0001 010 001 1 00000 ;; put x30 in R2; ADD R2 <= R1 + 0
1001 001 001 111111   ;; NOT R1
0001 001 001 1 00001 ;; ADD 1
0001 000 001 0 00 000 ;; ADD -30; R0 <= R1 + 0
;; 1111 0000 0010 0001  ;; OUT
1111 0000 0010 0101  ;; HALT
.END
Assembled! Use %dis or %dump to examine; use %exe to run.
In [2]:
%pc x3100
============================================================
Registers:
============================================================
PC: x3100
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x0000 
In [3]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3101
(1/9) GETC [1] (x3101*: xF020)
    R7 <= x3101
    PC <= x044C
0
    memory[xFE02] <= x0030

============================================================
Registers:
============================================================
PC: x044C
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3101 
In [4]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x044D
(2/21) LDI R0, x043E (x044D*: xA1F1)
  Reading memory[x043e] (xfe00) =>
  Reading memory[xfe00] (x0000) =>
    R0 <= x0000
    NZP <= (0, 1, 0)

============================================================
Registers:
============================================================
PC: x044D
N: 0 Z: 1 P: 0 
R0: x0000 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3101 
In [5]:
%bp x3102
============================================================
Breakpoints
============================================================
    1)            x3102: x1270
In [10]:
%cont
============================================================
Computation completed
============================================================
Instructions: 2822118
Cycles: 25399038 (12.699519 milliseconds)

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1 
R0: x0060 R1: x0030 R2: xFFD0 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x310A 
In [9]:
%regs
============================================================
Registers:
============================================================
PC: x3102
N: 0 Z: 1 P: 0 
R0: x0030 R1: x0000 R2: x0000 R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3101 
In [58]:
%step
============================================================
Stepping...  => read, <= write, (Instructions/Cycles):
============================================================
    PC <= x3109
(13/93) ADD R0, R1, R0 [9] (x3109*: x1040)
    R0 <= x0012
    NZP <= (0, 0, 1)

============================================================
Registers:
============================================================
PC: x3109
N: 0 Z: 0 P: 1 
R0: x0012 R1: xFFE2 R2: x001E R3: x0000 
R4: x0000 R5: x0000 R6: x0000 R7: x3101