TECHNOLOGY 

Published on
KembaraXtra-Computer Science - Programming Without an Operating System
Core Concept: Direct Hardware Access
  • Definition: In a system without an operating system (OS), software (like a game) directly interacts with the hardware. There's no intermediary layer managing resources or providing services.
Example: Early Video Game Consoles
  • Examples: Atari 2600, Nintendo Entertainment System (NES), Sega Genesis
  • How they worked:
    • Game code resided on a cartridge.
    • Inserting the cartridge and turning on the console started the game.
    • The console executed the game's code directly, with no OS intervention.
    • Only one program (the game) ran at a time.
    • Switching games required turning off the system, swapping cartridges, and turning it back on.
Programmer's Responsibilities (Without an OS)
  • Total Control: The game developer was responsible for:
    • Game logic
    • Initializing the system (setting up hardware components)
    • Controlling video hardware
    • Reading controller inputs
    • Managing all hardware interactions
  • Hardware Specificity: Deep understanding of the target console's hardware was crucial. Different consoles had significantly different hardware designs.
  • Challenges:
    • Porting to different consoles required rewriting substantial portions of code due to hardware differences.
    • Redundant code: Every game cartridge needed code for basic tasks (hardware initialization, etc.), leading to duplicated effort across different developers and games.
Advantages (of this approach)
  • Maximum Performance: Knowing the exact hardware specifications allowed developers to optimize their code to squeeze the maximum possible performance from the system.
  • Stable environment: The hardware design was consistent during the manufacturing years, which allowed developers to target their code to that specific hardware
Disadvantages (of this approach)
  • Porting: It was hard to port the games, so they often had to rewrite a substantial portion of their code.
  • Duplicated work: Every game cartridge had to include similar code to accomplish fundamental tasks, such as initializing the hardware.



Picture
Published on

Study Guide: Factorial Calculation in C

1. C Implementation of Factorial

Code:

// Calculate the factorial of n.
int factorial(int n)
{
  int result = n;

  while(--n > 0)
  {
    result = result * n;
  }

  return result;
}
  

Functionality:

- Takes an integer n as input.

- Calculates the factorial of n.

- Uses a while loop and a local variable result to compute the factorial.

- Returns the calculated factorial value.

Key Aspects:

- Uses a while loop to iterate.

- The --n operator decrements n before its value is compared to 0.

- More readable compared to assembly language.

- Portable and can be compiled for different processor types.

2. Exercise: Running the C Code

Goal: Understand the step-by-step execution of the C factorial function.

Method:

- Trace the code's execution with a specific input value (e.g., n = 4).

- Keep track of variable values (n and result) before and after each step.

Expected Outcome: Verify that the function returns the correct factorial (e.g., 24 for n = 4).

3. Compilation and Disassembly

Process:

- C code is compiled into machine code.

- Machine code can be disassembled into assembly language.

Implication: Compiled programs can be analyzed even without the original source code.

ARM Assembly Output:

Address     Assembly
0001051c    sub     r3, r0, #1
00010520    cmp     r3, #0
00010524    bxle    lr
00010528    mul     r0, r3, r0
0001052c    subs    r3, r3, #1
00010530    bne     00010528
00010534    bx      lr
  

Compiled C code outputs similar logic to the ARM assembly above.

Note: A compiler translates high-level language to machine code.

4. Cross-Platform Compilation

Advantage of High-Level Languages: Code portability.

Concept: The same C code can be compiled for different processors (e.g., ARM, x86) and operating systems (e.g., Linux, Windows), as long as it doesn’t depend on OS-specific features.

x86 Assembly Output (Windows):

Address     Assembly
00406c35    mov     ecx,dword ptr [esp+4]
00406c39    mov     eax,ecx
00406c3b    jmp     00406c40
00406c3d    imul    eax,ecx
00406c40    dec     ecx
00406c41    test    ecx,ecx
00406c43    jg      00406c3d
00406c45    ret
  

5. Key Takeaways

- High-level languages simplify development compared to assembly.

- Compilers handle the translation to machine code.

- High-level languages enable portability across processors and operating systems.

- Compiled code can be disassembled to study its underlying structure.

Picture
Published on
KembaraXtra-Computer Science - Compiled vs. Interpreted Languages
I. Core Concepts
  • Source Code: Human-readable program instructions written by developers.
  • Machine Language: Instructions that a CPU can directly execute.
  • Compilation: The process of translating source code into machine code (or an intermediate form).
  • Interpretation: The process of reading and executing source code instructions line by line (or bytecode).
II. Compiled Languages
  • Definition: Languages where source code is converted into machine code before runtime.
  • Process:
    1. Developer writes source code (e.g., in C).
    2. Compiler translates the source code into an executable file (binary).
    3. End user runs the executable file directly.
  • Example: C (using gcc compiler)
  • Advantages:
  • Fast execution speed because the code is already in machine language.
  • Disadvantages:
  • Platform-dependent: Executables are specific to the architecture they were compiled for.
  • Requires a compilation step during development.
III. Interpreted Languages
  • Definition: Languages where source code is executed line by line by an interpreter during runtime.
  • Process:
    1. Developer writes source code (e.g., in Python).
    2. End user runs the source code using an interpreter.
    3. The interpreter reads and executes the code.
  • Example: Python
  • Advantages:
  • Platform-independent: As long as an interpreter exists for a platform, the code can run.
  • No compilation step required for the end user.
  • Disadvantages:
  • Slower execution speed due to the overhead of interpretation.
  • Requires the user to have an appropriate interpreter installed.
IV. Hybrid Approach: Bytecode
  • Definition: A combination of compilation and interpretation, using an intermediate language called bytecode.
  • Process:
    1. Developer writes source code.
    2. Compiler translates source code into bytecode.
    3. A virtual machine (VM) executes the bytecode.
  • Bytecode:
  • Similar to machine code but designed for a virtual machine, not a specific CPU.
  • Virtual Machine (VM):
  • A software platform that provides a virtual CPU and execution environment.
  • Abstracts away the details of the underlying hardware and OS.
  • Examples:
  • Java (Java bytecode runs on the Java Virtual Machine - JVM).
  • C# (CIL or Common Intermediate Language runs on the .NET Common Language Runtime - CLR).
  • Python (CPython implementation compiles to bytecode internally).
  • Advantages:
  • Combines platform independence (like interpreted languages) with some of the performance benefits of compiled code.
  • Disadvantages:
  • Still requires a VM to run.
  • Execution is generally slower than native compiled code.
Picture
Published on
KembaraXtra-Computer Science- Object-Oriented Programming (OOP)
I. Paradigms in Programming
  • Programming languages support different approaches to programming called paradigms.
  • Examples:
    • Procedural Programming
    • Functional Programming
    • Object-Oriented Programming (OOP)
  • Languages can support multiple paradigms.
II. Object-Oriented Programming (OOP)
  • Definition: A programming paradigm where code and data are grouped together into objects.
  • Objects: Logical groupings of data and functionality, designed to model real-world concepts.
III. Classes and Objects
  • Class-Based Approach: Common in OOP languages.
    • Class: A blueprint for an object. Defines the structure and behavior of a type of object.
    • Object: An instance of a class. A concrete realization of the class blueprint.
  • Methods: Functions defined within a class. They define the actions that an object can perform.
  • Fields: Variables declared within a class. They store the data associated with an object.
    • Instance Variables (Python): Fields that have different values for each object (instance) of the class. Each object has its own unique value for these fields.
    • Class Variables (Python): Fields that have the same value across all objects (instances) of the class. These variables are shared by all objects of the class.
IV. Example: Bank Account
  • Class: BankAccount (a blueprint)
    • Fields:
      • balance (instance variable - each account has a unique balance)
      • holder's name (instance variable - each account has a unique name)
    • Methods:
      • withdraw()
      • deposit()
  • Objects: Specific bank accounts created from the BankAccount class (instances). These are real accounts with specific names and balances.
  • Interaction: We can use the withdraw or deposit methods to modify the balance field of specific bank account objects.
V. Python Example
myAccount.deposit(25) # Increases myAccount's balance by 25
Key Concept: myAccount is an object (instance of BankAccount). deposit() is a method called on that specific object, modifying its balance field.


Picture
Published on

KembaraXtra-Computer Science - Functions

1. What are Functions?

Definition: A function is a reusable block of code that performs a specific task.

Purpose:

  • Avoid code duplication.
  • Promote code reusability.
  • Improve code organization and readability.

Terminology: Functions may also be called subroutines, procedures, or methods (slight differences in meaning may exist depending on the language).

DRY Principle: Functions adhere to the "Don't Repeat Yourself" (DRY) principle by reducing duplicative code.

Encapsulation: Functions encapsulate internal details, providing an interface (inputs and outputs) for usage. Users don't need to know the inner workings.

2. Defining Functions

Definition: Creating a function and specifying its behavior.

Elements of a Function Definition:

  • Name: A unique identifier for the function.
  • Parameters (Inputs): Values passed to the function when it is called.
  • Body: The code block containing the instructions the function executes.
  • Return Value (Output): The value the function sends back to the caller (optional).

Example (C):

double areaOfCircle(double radius) {
    double area = 3.14 * radius * radius;
    return area;
}
  • double: Return type (floating-point number).
  • areaOfCircle: Function name.
  • double radius: Input parameter (radius of the circle).
  • { ... }: Function body.
  • return area: Returns the calculated area.

Example (Python):

def area_of_circle(radius):
    area = 3.14 * radius * radius
    return area
  • def: Keyword to define a function.
  • Type declarations are not required in Python.

3. Calling Functions

Definition: Executing a defined function.

Process:

  1. The calling code invokes the function.
  2. Parameters are passed (if any).
  3. Control is transferred to the function.
  4. The function executes its code.
  5. The function returns control (and a return value, if any) to the caller.

Example (C):

double area1 = areaOfCircle(2.0);
double area2 = areaOfCircle(38.6);

Example (Python):

area1 = area_of_circle(2.0)
area2 = area_of_circle(38.6)

The returned value can be stored in a variable or ignored (though ignoring the return value might not be useful, depending on the function).

4. Using Libraries

Definition: Leveraging pre-written functions provided by programming languages or third-party developers.

Standard Library: A set of functions included with a programming language.

Examples: Printing to the console, file operations, text processing.

C and Python both have standard libraries. Python's is known to be extensive.

Additional Libraries: Libraries created by developers and shared for others to use.

Package Managers: Tools for managing and distributing libraries (packages).

  • Python: pip (widely used).
  • C: Multiple package managers exist, but none are universally adopted.
Picture
Published on

Program Flow: Controlling What Your Code Does

Core Idea: Program flow (or control flow) lets your program make decisions and repeat actions based on conditions.

1. If Statements: Doing Something Based on a Condition

Purpose: Executes a block of code only if a specified condition is true. Optionally includes else to execute a different block if the condition is false.

Basic Structure:

Python:

if condition:
    # Code to execute if condition is true
else:
    # Code to execute if condition is false

C:

if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

Key Differences (Python vs. C):

  • Code Blocks:
    • Python: Uses indentation to define code blocks (lines with the same indentation level belong to the same block).
    • C: Uses curly braces {} to define code blocks. Braces can be omitted for single-line blocks.
  • elif (Python) / else if (C): Used to check multiple conditions in sequence. The elif/else if condition is only evaluated if the preceding if or elif/else if condition was false.

Python:

if condition1:
    # Code for condition1
elif condition2:
    # Code for condition2
else:
    # Code if neither condition1 nor condition2 is true

C:

if (condition1) {
    // Code for condition1
} else if (condition2) {
    // Code for condition2
} else {
    // Code if neither condition1 nor condition2 is true
}

2. Looping: Repeating Actions

Purpose: Executes a block of code repeatedly.

Types:

  • while loop: Repeats as long as a condition is true.

Python:

while condition:
    # Code to repeat

C:

while (condition) {
    // Code to repeat
}

Important: Make sure the condition eventually becomes false to avoid an infinite loop!

  • for loop: Iterates over a sequence of values (numbers, items in a list, etc.).

C Style (for with numeric range):

for (initialization; condition; increment/decrement) {
    // Code to execute in each iteration
}

  • initialization: Executed once at the beginning of the loop (e.g., int x = 1;).
  • condition: Checked before each iteration. The loop continues as long as the condition is true (e.g., x <= 10;).
  • increment/decrement: Executed after each iteration (e.g., x++;).

Python Style (for with collection):

for item in collection:
    # Code to execute for each item in the collection

  • collection: A list, tuple, string, or other iterable object.
  • item: A variable that takes on the value of each item in the collection during each loop iteration.
Picture
Published on

Study Guide: Logic in Programming

I. Introduction

• Processors excel at logical operations, which are fundamental to digital circuits.

• Programming languages provide operators for handling logic.

• Two main types of logical operators:

  • Bitwise Operators: Operate on individual bits of integers.
  • Boolean Operators: Operate on Boolean (true/false) values.

• Terminology varies across languages (e.g., C uses "logical" instead of "Boolean"). This guide will use "bitwise" and "Boolean."

II. Bitwise Operators

• Act on individual bits of integers.

• Result: an integer value.

• Perform logical operations (AND, OR, XOR, NOT) on the bits of integers in parallel.

• Operators (common in C and Python):

  • AND: &
  • OR: |
  • XOR: ^
  • NOT (complement): ~

Example (Python):

x = 5  # Binary: 0101
y = 3  # Binary: 0011
a = x & y  # Bitwise AND
b = x | y  # Bitwise OR

# a is 1 (0001 in binary)
# b is 7 (0111 in binary)

Explanation of Example

• AND: The result is 1 only when both corresponding bits are 1.

• 5 & 3:

0101
0011
----
0001 (1 in decimal)

• OR: The result is 1 if either corresponding bit (or both) is 1.

• 5 | 3:

0101
0011
----
0111 (7 in decimal)

III. Boolean Operators

• Work on Boolean values (true or false).

• Result: a Boolean value.

• Boolean values: represented differently in different languages (e.g., True or False in Python).

• Boolean variable: a named memory address holding a true or false value.

• Expressions can evaluate to true or false (e.g., item_cost > 5).

Boolean Operators:

• Perform logical operations (AND, OR, NOT) on Boolean values.

• Example: item_on_sale and item_cost > 5 (checks if both conditions are true).

Boolean Operation C Operator Python Operator
AND && and
OR || or
NOT ! not

Comparison Operators:

• Compare two values and evaluate to true or false.

Comparison Operation Operator
EQUALITY ==
NOT EQUAL !=
GREATER THAN >
LESS THAN <
GREATER THAN OR EQUAL >=
LESS THAN OR EQUAL <=

Important:

  • == (double equals) is for equality comparison (returns true/false).
  • = (single equals) is for assignment (sets a variable's value).
  • Example: x == 5 (is x equal to 5?) vs. x = 5 (set x to 5).
Published on
Computer Science- The Internet of Things (IoT)
1. Introduction to IoT
  • Traditional Internet Model: Servers provide services, and users interact with them via devices like PCs, laptops, and smartphones.
  • IoT Definition: Extending internet connectivity to everyday devices (speakers, TVs, thermostats, cars, etc.).
  • Driving Forces:
    • Decreasing costs and size of electronic components.
    • Widespread Wi-Fi and cellular internet access.
    • Consumer demand for "smarter" devices.
    • Growth of cloud computing (as IoT devices often rely on web services).
  • Applications:
    • Consumers: "Smart homes" (monitoring and controlling appliances).
    • Business: Manufacturing, healthcare, transportation, etc.
2. Risks Associated with IoT
  • Security:
    • IoT devices often have weak security measures.
    • Compromised devices can be used as entry points into secure networks.
    • They can also be used to launch remote attacks.
    • Consumers often overlook security when connecting devices.
  • Privacy:
    • IoT devices collect data, which is often sent to cloud services.
    • Concerns about how organizations handle personal data.
    • Risk of data breaches, even with well-intentioned organizations.
    • Devices like smart speakers pose risks of accidentally recording private conversations.
    • Trade-off between convenience and privacy.
  • Reliance on Cloud Services:
    • Functionality can be limited or lost if the internet connection is down.
    • Manufacturer may eventually discontinue service, rendering the device useless.



Picture
Published on
Computer Science - Virtual Reality (VR) and Augmented Reality (AR)
I. Introduction
  • VR and AR are technologies that change how we interact with computers.
  • XR is a general term referring to VR and AR technologies.
II. Virtual Reality (VR)
  • Definition: Immerses the user in a 3D virtual space, usually with a headset.
  • Interaction: Users interact with virtual objects using:
    • Gaze
    • Voice commands
    • Handheld controllers
  • History:
    • Attempts have been made for decades, but became mainstream in the 2010s.
    • Google Cardboard (2014): Popularized VR due to its low cost and accessibility.
Google Cardboard
  • Concept: Headset made from cardboard, lenses, and a smartphone.
  • How it Works:
    • Apps render content for each eye on separate halves of the smartphone screen.
    • Uses the smartphone's gyroscope to track head movement.
  • 3 Degrees of Freedom (3DoF):
    • Tracks head movement (looking around).
    • Cannot track physical movement in space (moving around).
    • Supports basic one-button input.
  • Impact: Introduced VR to a wide audience.
Degrees of Freedom (DoF)
  • 3DoF: Tracks rotational movement (looking around).
  • 6 Degrees of Freedom (6DoF): Tracks both rotational and positional movement (moving around in space).
    • More immersive experience.
    • VR headsets and controllers can have either 3DoF or 6DoF.
    • 6DoF controllers enable natural interactions in VR.
VR Solutions in the Market
  • Smartphone-based: (Samsung Gear VR, Google Daydream)
  • PC-connected: (Oculus Rift, HTC Vive, Windows Mixed Reality)
    • Provide the highest graphical fidelity.
    • Most expensive due to the cost of the PC.
  • Standalone: (Oculus Go, Oculus Quest, Lenovo Mirage Solo)
    • Do not require a smartphone or PC.
III. Augmented Reality (AR)
  • Definition: Overlays virtual elements onto the real world.
  • Implementation:
    • Mobile Devices: Uses the rear-facing camera to observe the real world and overlays virtual elements.
    • Dedicated Devices: (Google Glass, Magic Leap, Microsoft HoloLens) Worn on the head and superimpose computer-generated graphics.
  • Advanced AR: Software understands physical elements, allowing virtual elements to interact with the environment.
  • Interaction: Users interact using voice commands or hand tracking.

V. XR Development
  • Platforms: VR and AR technologies are platforms for software developers.
  • Game Engines: Many VR developers use game engines like Unity and Unreal.
    • Familiar to game developers.
    • Facilitate building software for multiple VR platforms.
  • Web Development: WebVR and WebXR (JavaScript APIs)
    • WebVR: Focused on VR specifically (older).
    • WebXR: Supports both AR and VR (newer).



Feature	Virtual Reality (VR)	Augmented Reality (AR) Environment	Immerses user in a virtual world	Overlays virtual elements onto the real world User Perception	Replaces the real world	Enhances the real world
Picture
Published on

Calculating a Factorial in C – Notes

1. Purpose of the C Factorial Example

  • Demonstrates the same logic previously used in ARM assembly.
  • Helps visualize how high-level C code becomes low-level machine code.

2. Why C Instead of Python?

  • C is a compiled language; Python is interpreted.
  • Compiled C code can be disassembled to view its machine-level instructions.

3. Example C Code


int factorial(int n)
{
  int result = n;

  while(--n > 0)
  {
    result = result * n;
  }

  return result;
}
    

4. How the Function Works

  • Receives n as input.
  • Initializes result with n.
  • Decrements n before multiplying.
  • Returns the computed factorial.

5. Walkthrough Example (n = 4)

Step n result Explanation
Init 4 4 Function starts
1 3 12 4 × 3
2 2 24 12 × 2
3 1 24 24 × 1
End - 24 Final return
✅ Final Output: factorial(4) = 24

6. Note on --n in the While Loop

  • --n decreases n before the condition check.
  • Ensures the loop multiplies from n-1 down to 1.

7. Advantages of the C Version

  • More readable than assembly code.
  • Not processor-specific — portable across systems.

8. Compiled ARM Assembly Output


0001051c  sub   r3, r0, #1
00010520  cmp   r3, #0
00010524  bxle  lr
00010528  mul   r0, r3, r0
0001052c  subs  r3, r3, #1
00010530  bne   00010528
00010534  bx    lr
    

9. Disassembly Insight

  • C source code → machine code → disassembled to readable assembly.
  • Even without the source, compiled programs can be analyzed.

10. Cross-Platform Compilation Example (x86)

The same C code compiled for a 32-bit x86 processor:


00406c35  mov   ecx, dword ptr [esp+4]
00406c39  mov   eax, ecx
00406c3b  jmp   00406c40
00406c3d  imul  eax, ecx
00406c40  dec   ecx
00406c41  test  ecx, ecx
00406c43  jg    00406c3d
00406c45  ret
    
Conclusion: High-level languages like C provide readability and portability, while compilers handle processor-specific translation.