














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Mr. Gurpreet Verma at Cochin University of Science and Technology for Assembly Language Programming course. It includes: Assembler, Correspondence, Symbols, Loop, Syntax, Prompt, Character, Occurrences, Match, Halt, Convert
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Computers like ones and zeros…
Humans like symbols…
Assembler is a program that turns symbols into machine instructions.
ISA-specific: close correspondence between symbols and instruction set mnemonics for opcodes labels for memory locations
additional operations for allocating storage and initializing data
ADD R6,R2,R6 ; increment index reg.
0001110010000110
Each line of a program is one of the following:
an instruction
an assember directive (or pseudo-op)
a comment
Whitespace (between symbols) and case are ignored.
Comments (beginning with “;”) are also ignored.
An instruction has the following format:
LABEL OPCODE OPERANDS ; COMMENTS
optional mandatory
Opcodes
reserved symbols that correspond to LC-3 instructions
listed in Appendix A
ex: ADD, AND, LD, LDR, …
Operands
registers -- specified by Rn, where n is the register number
numbers -- indicated by # (decimal) or x (hex)
label -- symbolic name of memory location
separated by comma
number, order, and type correspond to instruction format
ex: ADD R1,R1,R ADD R1,R1,# LD R6,NUMBER BRz LOOP
Pseudo-operations
do not refer to operations executed by program
used by assembler
look like instruction, but “opcode” starts with dot
Opcode Operand Meaning
.ORIG (^) address starting address of program
.END (^) end of program
.BLKW (^) n allocate n words of storage
.FILL (^) n allocate one word, initialize with
value n
.STRINGZ (^) n-character string
allocate n+1 locations, initialize w/characters and null terminator
LC-3 assembler provides “pseudo-instructions” for each trap code, so you don’t have to remember them.
Code Equivalent Description
HALT TRAP x25 (^) Halt execution and print message to
console.
IN TRAP x23 (^) Print prompt on console,
read (and echo) one character from keybd. Character stored in R0[7:0].
OUT TRAP x21 (^) Write one character (in R0[7:0]) to console.
GETC TRAP x20 (^) Read one character from keyboard.
Character stored in R0[7:0].
PUTS TRAP x22 (^) Write null-terminated string to console.
Address of string is in R0.
Count the occurrences of a character in a file. Remember this?
Count = 0 (R2 = 0)
Ptr = 1st file character (R3 = M[x3012])
Input char from keybd (TRAP x23)
Done? (R1 ?= EOT)
Load char from file (R1 = M[R3])
Match? (R1 ?= R0)
Incr Count (R2 = R2 + 1)
Load next char from file (R3 = R3 + 1, R1 = M[R3])
Convert count to ASCII character (R0 = x30, R0 = R2 + R0)
Print count (TRAP x21)
HALT (TRAP x25)
NO
NO
YES
YES
; ; Program to count occurrences of a character in a file. ; Character to be input from the keyboard. ; Result to be displayed on the monitor. ; Program only works if no more than 9 occurrences are found. ; ; ; Initialization ; .ORIG x AND R2, R2, #0 ; R2 is counter, initially 0 LD R3, PTR ; R3 is pointer to characters GETC ; R0 gets character input LDR R1, R3, #0 ; R1 gets first character ; ; Test character for end of file ; TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04) BRz OUTPUT ; If done, prepare the output (^) 7-
; ; Storage for pointer and ASCII template ; ASCII .FILL x PTR .FILL x .END
Convert assembly language file (.asm)
into an executable file (.obj) for the LC-3 simulator.
First Pass:
scan program file
find all labels and calculate the corresponding addresses;
this is called the symbol table
Second Pass:
convert instructions to machine language,
using information from symbol table
Construct the symbol table for the program in Figure 7.
(Slides 7-11 through 7-13).
For each executable assembly language statement, generate the corresponding machine language instruction.
If operand is a label, look up the address from the symbol table.
Potential problems:
Improper number or type of arguments
ex: NOT R1,# ADD R1,R ADD R3,R3,NUMBER
Immediate argument too large
ex: ADD R1,R2,#
Address (associated with label) more than 256 from instruction
can’t use PC-relative addressing mode
Using “assemble” (Unix) or LC3Edit (Windows),
generates several different output files.
This one gets loaded into the simulator.
LC-3 object file contains
Starting address (location where program must be loaded),
followed by…
Machine instructions
Example
Beginning of “count character” object file looks like this:
0011000000000000
0101010010100000
0010011000010001
1111000000100011
.
.
.
.ORIG x
AND R2, R2, #
LD R3, PTR
TRAP x