SYSTEMS LECTURE WED SEP 4th
LC-3 Subroutine Architecture: JSR and JSRR Instructions
Memory Code and Data Organization:
In LC-3 programs, code and data are placed continuously in memory without skipping spaces between them.
A single block of memory may contain data fields immediately followed by subroutine code.
Jump to Subroutine (
JSR) Mechanics:JSRis an instruction used to jump from the main code to a subroutine location.JSRmodifies execution by placing the target address of the subroutine directly into the Program Counter ().To calculate the target subroutine address,
JSRuses a offset embedded directly within the instruction word.The offset is added to the current value of the Program Counter () during instruction execution.
Bit Allocation and Range of
JSR:Unlike load instructions (
LD) which require a destination register field,JSRdoes not specify a register to load data into.Because no destination register bits are needed,
JSRgains additional bits for its offset field compared to a standard -bit load instruction offset.JSRutilizes an -bit offset field.An -bit field allows addressing across total memory locations.
In 's complement representation, the -bit signed range spans from (positive forward offset) down to (negative backward offset).
Unlike standard load instructions which always branch forward using a positive offset,
JSRsupports both positive (forward) and negative (backward) offsets.JSRcan jump down to a subroutine located lower in memory, or jump up to a subroutine located higher in memory.
Subroutine Return Mechanism and Register:
When
JSRexecutes, the Program Counter () already points to the next instruction in memory immediately followingJSR. This address is the designated return location.JSRautomatically copies and saves this return address into register () before changing to the subroutine's address.The subroutine executes its sequence of instructions until it reaches the return instruction (
RET).RETrestores execution back to the caller by taking the saved address in and copying it back into the Program Counter ().Execution resumes seamlessly at the instruction following the initial
JSRcall.Alternative names for
JSRinclude "Jump to Subroutine" and "Jump and Save Return".
Critical Warning Regarding Register :
Calling
JSRautomatically overwrites whatever value is currently stored in with the return address.Any pre-existing data in will be permanently lost.
Rule: When implementing subroutines, do not use to store general data anywhere in the program to avoid accidental data destruction.
Jump to Subroutine Register (
JSRR) Mechanics:JSRRis a variant ofJSRthat uses a base register instead of an -bit offset (analogous to howLDRrelates toLD).The target subroutine address must be pre-loaded into a base register (e.g.,
JSRR R3).JSRRalso automatically saves the return address into register soRETcan function correctly.
Control Flow Constructs and Register-Based Jumps
Range Characteristics of
JSRRvs.JSR:JSRis restricted to target subroutines within an -bit offset range ( locations forward or locations backward relative to ).JSRRuses a register holding a full -bit address, enabling it to jump to a subroutine located anywhere in the LC-3 memory space.Course Constraint: Programs assigned in this course will not exceed combined instructions, data items, and subroutine lines. Therefore,
JSRRwill not be strictly required in programming assignments, though understanding its operational mechanics is required for exams.
Jump Instruction (
JMP) Mechanics:JMPuses a base register containing the target memory address.Using register-based addresses requires loading literal address values into registers.
Hardcoding fixed address literals is functional in single-threaded processors like LC-3, but causes severe errors in multi-threaded or multi-programmed computing environments.
JMPwill not be used in course assignments; conditional/unconditional branches (BR) are used instead.
Relationship Between
JMPandRET:RETis a specialized form of theJMPinstruction.RETspecifically executes a jump to the address contained in register ().In the LC-3 instruction set architecture (Appendix A),
JMPandRETshare the exact same binary opcode.Writing
JMP R7generates the exact same machine code instruction internally as writingRET.
Control Flow Constructs Using Branch (
BRnzp):Branch instructions (
BR) utilize a -bit offset.If-ElseConstruct:An unconditional branch (
BRnzp) is placed at the end of theifblock to jump execution over theelseblock.
WhileLoop Construct:Evaluates its condition at the top of the block prior to executing body code.
A conditional branch (e.g.,
BRn,BRz,BRp) jumps out of the loop block if the condition is false.An unconditional branch (
BRnzp) is placed at the bottom of the body code to jump back up to the condition evaluation at the top.A standard
whileloop requires separate branch instructions.
Do-While/Do-UntilConstruct:Executes the body code first before evaluating the condition at the bottom.
Always executes the loop body at least once.
Uses a conditional branch at the bottom to jump back up to the top if the condition holds true.
Does not require an unconditional branch (
BRnzp) at the bottom.
LC-3 Trap Routines and Memory String Representation
Standard Input/Output Traps (
GETCandOUT):GETC: Halts execution and waits for a single character to be pressed on the keyboard. Stores the ASCII value of the character into register .OUT: Takes the ASCII character stored in register and outputs/prints it to the display terminal.
String Printing Trap (
PUTS) and String Memory Layout:A string in LC-3 is defined as a contiguous sequence of ASCII character words in memory ended by a terminating literal zero (
x0000).Example memory array for string
"HELLO": ASCII values for'H','E','L','L','O', followed by numeric value0(x0000).Distinction between Character Zero and Literal Zero:
Character
'0'has an ASCII decimal value of (hexadecimalx30).Literal zero is
x0000(ASCIINULL).Java Analogy: Null object references in Java (
p == null) point to memory address0(null).
LC-3 strings are referred to as null-terminated strings.
Operating Mechanism of
PUTSvs.OUT:Printing a single character requires loading the character's ASCII value into and invoking
OUT.Printing a string via
PUTSrequires loading the starting memory address of the string into register usingLEA(Load Effective Address).Internal Execution Steps of
PUTS:Reads the memory value at the address provided in .
Checks if the value is equal to literal zero (
x0000).If non-zero, copies the character to , calls
OUT, advances the internal memory pointer to the next consecutive address, and repeats.If zero (
x0000) is encountered, execution terminates.
Code Example Comparison (Character vs. String Output):
Character printing pattern:
LDR R0, R2, #2(Loads character ASCII value48/x30into )OUTHALT
String printing pattern:
LEA R0, STRING_LABEL(Calculates offset to place starting addressx3003into )PUTSHALT
Opcodes, Assembly Process, and Disallowed Instructions
LC-3 Opcode Usage Rules:
Allowed Opcodes: Standard arithmetic, logical, load, store, branch, trap (
GETC,OUT,PUTS,IN,HALT),JSR, andRETinstructions.Forbidden/Disallowed Instructions for Course Programs:
LDI(Load Indirect)STI(Store Indirect)JMP(Jump — useBRorRETinstead)JSRR(Jump Subroutine Register — useJSRinstead)RTI(Return from Interrupt — used in specialized hardware interrupt handlers during the final week of class)
Software Roles: Assembler vs. Simulator:
Assembler: A software tool that translates human-readable assembly language source code (
.asm) into binary/hexadecimal machine code (.hex).Simulator: A software environment that loads and executes hexadecimal machine code.
Hand-conversion to Hex:
Homeworks and require manually converting assembly instructions to -digit hexadecimal values.
Homework and subsequent assignments require writing raw assembly source files (
.asm). Hand conversion to hex is not performed for Homework onward.
Grading Pipeline:
.asmfiles uploaded to WebCat are automatically assembled into machine code and executed against test cases.
Assembler Directives (Pseudo-ops)
Definition of Pseudo-ops:
Pseudo-ops (assembler directives) are instructions written to give directions directly to the assembler software during compilation.
Pseudo-ops always begin with a period / dot (
.).They do not correspond directly to CPU hardware opcodes executed at runtime.
Origin Directive (
.ORIG):Syntax:
.ORIG x3000Specifies the starting memory location where the assembled machine code will be loaded.
Must be the very first meaningful statement in the source file.
Only blank lines and comment lines are permitted above
.ORIG.Must appear exactly once per program file.
If omitted, the assembler will fail, issue errors, or incorrectly misinterpret the first program instruction as the origin address.
Data Allocation Directives:
.STRINGZ:Syntax: `.STRINGZ