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 .STRINGZ directives and are terminated by a null character (\0, ASCII value 00).

  • Switch Case Subroutine:

    • Purpose: Traverses a string and toggles the case of alphabetical characters while leaving non-alphabetical characters unchanged.

    • Implementation Steps:

    1. Load Base Pointer: Load the start address of the string into a base register (e.g., R2) using LEA R2, StringLabel.

    2. Load Character: Load individual characters using base-relative indexing with a zero offset (LDR R1, R2, #0).

    3. Null Check: Branch to terminate when the character equals 00.

    4. Range Check and Transformation:

      • Lowercase range ('a' to 'z'): Subtract 3232 to convert to uppercase.

      • Uppercase range ('A' to 'Z'): Add 3232 to convert to lowercase.

      • The difference between uppercase and lowercase ASCII values is determined by bit 55 (25=322^5 = 32).

    5. Writeback: Store the converted character back into the same memory location using STR R1, R2, #0.

    6. 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 R0 and the string base address in R1.

    • Move the target character to a temporary register (e.g., R2) and clear R0 to 00 to serve as the match counter.

    • Sequentially load each character into a register (e.g., R3) using LDR, incrementing R1 after each check.

    • Compare loaded characters against the target character in R2; increment R0 upon a match.

    • Loop until reading the null terminator (00), 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 main function, 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 String object 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 main or 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: 1 byte1\text{ byte} (8 bits8\text{ bits}). Unsigned range is 00 to 255255; signed range is −128-128 to 127127.

    • short: 2 bytes2\text{ bytes} (16 bits16\text{ bits}). Signed range is −32,768-32{,}768 to 32,76732{,}767.

    • int: 4 bytes4\text{ bytes} (32 bits32\text{ bits}). Signed range is −2,147,483,648-2{,}147{,}483{,}648 to 2,147,483,6472{,}147{,}483{,}647.

    • long: 8 bytes8\text{ bytes} (64 bits64\text{ bits}). 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: 4 bytes4\text{ bytes} (32 bits32\text{ bits}).

    • double: 8 bytes8\text{ bytes} (64 bits64\text{ bits}).

    • long double: 16 bytes16\text{ bytes} (128 bits128\text{ bits}).

    • C uses double as the standard floating-point type for standard math libraries to eliminate the need for overloaded functions.

  • printf Formatting and Interpretation:

    • C prints memory according to the formatting specifier rather than strictly enforcing data types:

    • %d: Formats the value as a signed base-1010 integer.

    • %u: Formats the value as an unsigned base-1010 integer.

    • %c: Formats the stored integer as an ASCII character.

    • Assigning values outside standard signed ranges (e.g., assigning 128128 or 0xFF to a signed char) loads the bit pattern into the byte without compiler rejection, but printing via %d interprets the sign bit, displaying −128-128 or −1-1.

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).