SYSTEMS LECTURE MON OCT 12th
Course Administration and Assessment Feedback
Test Grades and Access:
Test grades are posted online in the grade book.
Grade feedback will provide specific details on questions missed, point weight per question, submitted student answers, and correct answers.
Graded tests will be returned and reviewed on Wednesday after all remaining students finish their exams.
LC-3 Assembly: String Manipulation (Programming Assignment 2)
General String Representation:
Strings in LC-3 are declared with
.STRINGZdirectives and are terminated by a null character (\0, ASCII value ).
Switch Case Subroutine:
Purpose: Traverses a string and toggles the case of alphabetical characters while leaving non-alphabetical characters unchanged.
Implementation Steps:
Load Base Pointer: Load the start address of the string into a base register (e.g.,
R2) usingLEA R2, StringLabel.Load Character: Load individual characters using base-relative indexing with a zero offset (
LDR R1, R2, #0).Null Check: Branch to terminate when the character equals .
Range Check and Transformation:
Lowercase range (
'a'to'z'): Subtract to convert to uppercase.Uppercase range (
'A'to'Z'): Add to convert to lowercase.The difference between uppercase and lowercase ASCII values is determined by bit ().
Writeback: Store the converted character back into the same memory location using
STR R1, R2, #0.Increment Pointer: Increment the address register (
ADD R2, R2, #1) and repeat the loop.
Count Characters Subroutine (
count_char):Purpose: Counts occurrences of a target character within a null-terminated string.
Implementation Steps:
Receives search character in
R0and the string base address inR1.Move the target character to a temporary register (e.g.,
R2) and clearR0to to serve as the match counter.Sequentially load each character into a register (e.g.,
R3) usingLDR, incrementingR1after each check.Compare loaded characters against the target character in
R2; incrementR0upon a match.Loop until reading the null terminator (), then return with the count stored in
R0.Testing Caution: These subroutines are executed once per test case because they modify memory in place.
Transition to C Programming (C vs. Java)
Key Similarities:
Standard control flow constructs (
if,while,for) and mathematical/logical operators function identically.Program execution begins in the
mainfunction, of which there can only be one per program.
Key Differences:
C contains no class constructs, objects, or access control modifiers (
public,private).Strings: C does not have a native primitive or built-in
Stringobject type. Strings are null-terminated character arrays (char[]).
Variable Scopes and Best Practices
Local Variables:
Scoped strictly to the enclosing function and deallocated upon return, matching local variable mechanics in Java.
Global Variables vs. Java Fields:
In C, variables declared outside functions are globally accessible across all compiled files in the project.
Potential Hazards: In multi-developer projects, globally shared names cause collisions and inadvertent state mutation.
Best Practice: Avoid global variables. Encapsulate data inside
mainor local functions, pass variables as function arguments, and receive modifications via return values.Always initialize variables upon declaration to prevent reading garbage memory values.
Data Types and Memory Sizes in C
Integer Types:
char: (). Unsigned range is to ; signed range is to .short: (). Signed range is to .int: (). Signed range is to .long: (). Typically used for large quantities such as millisecond timestamps.
Signed vs. Unsigned Operations:
Variables are signed by default unless specified with
unsigned.Arithmetic addition functions identically at the bit level regardless of signedness.
Bit Shifting:
Unsigned right shifts shift in zeros from the left (logical shift).
Signed right shifts replicate the sign bit to preserve the negative/positive sign (arithmetic shift).
Floating-Point Types:
float: ().double: ().long double: ().C uses
doubleas the standard floating-point type for standard math libraries to eliminate the need for overloaded functions.
printfFormatting and Interpretation:C prints memory according to the formatting specifier rather than strictly enforcing data types:
%d: Formats the value as a signed base- integer.%u: Formats the value as an unsigned base- integer.%c: Formats the stored integer as an ASCII character.Assigning values outside standard signed ranges (e.g., assigning or
0xFFto a signedchar) loads the bit pattern into the byte without compiler rejection, but printing via%dinterprets the sign bit, displaying or .
Preprocessor Directives (#define)
Mechanism:
Handled prior to compilation by the C preprocessor, which performs a direct textual search-and-replace across the source code.
Purpose:
Eliminates numeric literals ("magic numbers") in source files to allow centralized updates.
Syntax Rules:
Does not define a data type.
Does not end with a semicolon (a trailing semicolon will be pasted directly into expressions, causing syntax errors).