9/10: ISA basics, bit width, color depth, and MATLAB fundamentals
- Instruction format runs through an ISA: fixed-length vs variable-length instructions. Example: a 16-bit instruction on a 16-bit system is a single unit of code to be loaded into the CPU and executed. extInstructionlength=16extbits
- Addressing mode flag: a single bit can indicate how an address should be interpreted (direct vs indirect addressing).
- Address space: memory is byte-addressable; each byte has an index (0, 1, 2, …).
- Operations combine an opcode with addresses or immediates; sometimes an operation like ADD uses two operands (addresses or immediates).
- Simpler designs favor fixed-length instructions for predictability; more complex designs can use variable-length instructions but are harder to implement efficiently.
- IC design goal: balance between simplicity (RISC) and expressiveness/power (CISC). CISC can have long, powerful instructions but higher energy; RISC emphasizes energy efficiency and simpler decoding.
- Modern chips: manufacturing nodes (e.g., 3 nm) enable more cores and efficiency; bit width (e.g., 64-bit) is common for modern CPUs.
- Color depth and display impact: true color commonly uses 24 bits (8 bits per channel for RGB).
- Color math: with 8 bits per channel, per-channel max is 28−1=255; total color combinations for RGB is 2563=224=16,777,216.
- Backward compatibility: newer software/hardware often supports older instruction sets or software through compatibility modes; a 64-bit system can often run 32-bit software, sometimes by padding or emulation. For example, older 32-bit programs can run on a 64-bit OS, with higher bits set to zero where needed. If software is 64-bit, a 32-bit program may still be supported via compatibility layers.
- MATLAB and software alignment: MATLAB on modern machines typically runs as a 64-bit application; 32-bit versions exist but are increasingly uncommon.
- Example bit/byte scales: 16-bit, 32-bit, 64-bit representations are common; max unsigned values include 216−1=65535 and, for 8-bit color channel, 28−1=255.
- Data width and performance trade-offs influence system design (e.g., fixed-length ISA simplifies CPU design and energy management; longer, variable-length instructions can be more powerful but cost more energy).
MATLAB Basics: Workspace, Scripts, and Command Window
- MATLAB workspace shows all current variables; you can inspect with a command like WHO or WHOS.
- Files: MATLAB scripts use the extension ext.m.
- Command window: quick line-by-line execution; assignments are simple, e.g., a=1+2; creates a and stores the value 3.
- MATLAB is case sensitive for variable names.
- Variables do not require explicit type declarations; MATLAB uses dynamic typing (type checking is not forced at compile time).
- Common data types: double (default numeric type, 64-bit real), 64-bit storage; for a numeric array, storage is shown by WHOS; 64-bit means eight bytes per element.
- Basic commands:
- Create variable: a=10;
- Check workspace: who; or whos;
- Suppress output with a semicolon: b=[1,2,3];
- MATLAB documentation and community help are widely used; MATLAB Central and forums provide examples and shared code.
- Important concepts:
- Variables are named with first letter, then letters, digits, or underscores; names are case sensitive.
- A line of code is called a statement or instruction and can end with a semicolon to suppress output.
- A line of code is text; a program is a collection of such lines (scripts).
- For numeric formatting, MATLAB uses formatting verbs similar to C printf via fprintf.
- Format specifiers in MATLAB-like fprintf:
- %d for integer
- %f for floating point
- %c for a single character
- %s for a string
- You can also specify width and precision, e.g., , which controls minimum width and decimal places.
- Example: printing an integer and a float in one statement:
- fprintf(′A=
- Flags and formatting options:
- '-' left-justify within the field
- '+' always show sign
- ' ' leading space for positive numbers
- '0' pad with leading zeros
- Width controls total field width; precision controls number of decimals
- Printing multiple values without hard-coding each line:
- Use a for loop: for i = 1:10, fprintf('i = %d\n', i); end
- This avoids repeating code (vs hard-coding ten lines).
- Printing matrices or strings can be controlled to show subsets or sub-strings as needed.
- Numeric prefixes and base representations (e.g., decimal, hex) can be used with appropriate formatting, though d, f, c, s are the core ones for common use.
Control Flow and Practical Tips
- Loops (for, while) are essential for repetitive tasks; use them to avoid hard coding.
- Use semicolons to suppress intermediate outputs in scripts to keep the workspace clean.
- Be mindful of type behavior when mixing integers and real numbers; MATLAB implicitly casts numbers as needed but no strict type checking like some other languages.
- Understanding bit width and data representation helps reasoning about performance, memory, and compatibility across platforms.
Quick Recall Cards
- 24-bit color depth means 8 bits per channel; max per channel is 255; total colors = 2563=224=16,777,216.
- A 16-bit instruction is 16extbits; higher bit widths enable larger address spaces and more complex instructions.
- In fixed-length ISA, every instruction has the same length; simplifies decoding and energy usage.
- In MATLAB: variables are created by assignment; no explicit type declaration; use WHOS to inspect variables.
- printf/fprintf formatting basics: %d, %f, %c, %s; width/precision control via e.g. %4.1f; use -/+ and 0 padding as needed.