Introduction to Computing

Week 3: Computer Architecture & Processing

Cybersecurity Engineering | University College of Applied Science

📚 Week 3 Lecture Structure

1

Introduction to Computer Architecture

Understanding how computers manipulate data through CPU architecture and bus systems.

2

CPU Components & Functions

Detailed examination of ALU, Control Unit, Registers, and the Bus System that connects all components.

3

Machine Language & Instruction Types

Understanding machine language, instruction encoding (op-code & operand), and instruction categories.

4

Program Execution & Machine Cycle

The fetch-decode-execute cycle and how programs are executed step by step by the CPU.

5

Arithmetic/Logic Instructions

Detailed examination of arithmetic operations, logical operations (AND, OR, XOR), and their applications.

6

Shift and Rotate Operations

Understanding bit manipulation through shift and rotate operations, including logical shifts.

7

Practical Applications & Exercises

Hands-on activities with machine language decoding and bit manipulation programming.

1. Data Manipulation & Computer Architecture

Overview of Data Manipulation

Data Manipulation: The process of how computers manipulate data including moving data from one location to another and performing operations such as arithmetic calculations and text editing.

The circuitry in the computer that controls the manipulation of data is called the CPU (Central Processing Unit). Modern CPUs are packaged as small flat squares called microprocessors that plug into sockets on the main board.

2. CPU Components & Functions

🔢 Arithmetic/Logic Unit (ALU)

Performs all mathematical and logical operations:

  • Arithmetic: Addition, subtraction, multiplication, division
  • Logical: AND, OR, XOR, NOT operations
  • Bit Shifting: Shift and rotate operations

🎮 Control Unit (CU)

Coordinates all CPU activities and manages data flow:

  • Coordinates the machine's activities
  • Transfers data from main memory into registers
  • Activates appropriate circuitry within ALU
  • Manages the machine cycle

💾 Registers

Data storage cells used for temporary storage within CPU:

  • Function: Hold inputs to ALU and provide storage for results
  • Speed: Much faster than main memory access
  • Types: General purpose and special purpose (PC, IR)

Bus System

The CPU and main memory are connected by a collection of wires called a bus. This bus transfers bits between components:

📥 CPU Read Operation

The CPU reads data from main memory by:

  • Supplying the address of the pertinent memory cell
  • Sending an electronic signal telling memory to retrieve data
  • Receiving the data through the bus

📤 CPU Write Operation

The CPU writes data to main memory by:

  • Providing the address of the destination cell
  • Sending the data to be stored
  • Sending an electronic signal telling memory to store the data

3. Machine Language & Instruction Types

Understanding Machine Language

Machine Language: The set (collection) of all instructions recognized by a machine (CPU). Each instruction (or command) is encoded as a bit pattern recognizable by the CPU.

Compilers and Interpreters: Translate from high-level language to machine language.

Instruction Encoding

Each machine instruction consists of two parts:

  • Op-code: Indicates which operation is requested (e.g., 3 = STORE, 1 = LOAD)
  • Operand: Provides more detailed information about the operation specified by the op-code

Illustrative Machine Language System

For our illustrative machine:

  • 16 registers labeled 0 through F (hexadecimal)
  • 256 main memory cells addressed 00 through FF
  • Each instruction encoded using 16 bits (4 hexadecimal digits)
  • Example: Instruction "35A7" translates to "STORE the bit pattern found in register 5 in the main memory cell whose address is A7"
Instruction Type Purpose Examples
Data Transfer Move data between memory and registers LOAD, STORE, MOVE, I/O Instructions
Arithmetic/Logic Perform calculations and logical operations ADD, SUB, AND, OR, XOR, SHIFT, ROTATE
Control Change program flow and execution JUMP, BRANCH, CALL, HALT

Appendix C: Simple Machine Language Reference

Op-code Operand Description
1 RXY LOAD register R with the bit pattern found in memory cell at address XY
2 RXY LOAD register R with the value XY
3 RXY STORE the bit pattern found in register R in memory cell at address XY
4 RST ADD the bit pattern in register S and T and leave result in register R
5 RST ADD the bit pattern in register S and T (2's complement) and leave result in register R
6 RST SUBTRACT the bit pattern in register T from the register S and leave result in register R
7 RST OR register S and T, leaving result in register R
8 R0S ROTATE register R one bit to the right S times
9 RST XOR register S and T, leaving result in register R
A R0S ROTATE register R one bit to the left S times
B RXY JUMP to address XY if the contents of register R equal the contents of register 0
C 000 HALT execution

🧩 In-Class Activity: Machine Language Exercises

Activity 1: Instruction Decoding Challenge

Objective: Decode the following machine instructions using Appendix C

Instructions to decode:

  • 368A
  • BADE
  • 803C
  • 40F4

Activity 1 Solutions

Instruction 1: 368A

Answer: STORE the contents of register 6 to memory address 8A

Explanation: Op-code 3 = STORE, R = 6, XY = 8A

Instruction 2: BADE

Answer: JUMP to address DE if the contents of register A equal the contents of register 0

Explanation: Op-code B = JUMP if equal, R = A, XY = DE

Instruction 3: 803C

Answer: ROTATE register 0 three bits to the right

Explanation: Op-code 8 = ROTATE right, R = 0, S = 3 (C in hexadecimal = 12 decimal, but in this context 3 bits)

Instruction 4: 40F4

Answer: AND the contents of register F with register 4, store result in register 0

Explanation: Op-code 4 = AND operation (based on full Appendix C), R = 0, S = F, T = 4

Activity 2: Instruction Encoding Challenge

Objective: Translate the following English instructions into machine language using Appendix C

Instructions to encode:

  • LOAD register number 3 with the hexadecimal value 56
  • ROTATE register number 5 three bits to the right
  • AND the contents of register A with the contents of register 5 and leave the result in register 0

Activity 2 Solutions

Instruction 1: LOAD register 3 with value 56

Answer: 1356

Explanation: Op-code 1 = LOAD, R = 3, XY = 56

Instruction 2: ROTATE register 5 three bits right

Answer: 8503

Explanation: Op-code 8 = ROTATE right, R = 5, S = 3

Instruction 3: AND register A with register 5, result in register 0

Answer: 40A5

Explanation: Op-code 4 = AND, R = 0, S = A, T = 5

4. Program Execution & Machine Cycle

Program Execution Process

A computer follows a program stored in its memory by copying instructions from memory into the CPU as needed. Two special purpose registers control this process:

📝 Instruction Register (IR)

Contains the instruction that is currently being executed (current instruction)

📊 Program Counter (PC)

Contains the address of the next instruction to be executed (next instruction)

The Fetch-Decode-Execute Cycle

Every instruction goes through three stages in a continuous cycle:

1. Fetch

The CPU requests the main memory to provide the instruction stored at the address indicated by the program counter.

  • CPU places the instruction in its instruction register
  • Program counter is incremented (ready for next fetch)
  • Note: Instructions may span multiple memory cells (e.g., 16-bit instructions)

2. Decode

The CPU breaks the operand field into its proper components based on the instruction's op-code.

  • Control Unit examines op-code in IR
  • Determines what operation to perform
  • Identifies which registers or memory addresses are involved

3. Execute

The CPU executes the instruction by activating the appropriate circuitry to perform the requested task.

  • ALU performs calculations if needed
  • Data moved between registers and memory
  • Results stored in appropriate locations

Special Case: JUMP Instructions

When executing a JUMP instruction (e.g., B258 = "JUMP to address 58 if contents of register 2 equals register 0"):

  • If registers contain different bit patterns, execution terminates normally
  • If equal, the machine places the value at address 58 in its program counter during execute step
  • Next fetch step finds 58 in the program counter, so instruction at that address is fetched next

🔍 Exercise: Program Execution Analysis

Exercise 1: Memory Analysis

Scenario: Suppose memory cells from address 00 to 05 contain the hexadecimal bit patterns:

00: 1504
01: 2505
02: 5056
03: 3007
04: C000
05: 0000

Question: If we start the machine with program counter at 00, what bit pattern is in memory cell address 17 when the machine halts?

Exercise 2: Register and Memory Analysis

Scenario: Memory cells from address B0 to B8 contain:

B0: 135C
B1: 235D
B2: 5056
B3: 7034
B4: 30B8
B5: C000
B6: 0000
B7: 0000
B8: 0000

Questions:

  1. What bit pattern is in register 3 after first instruction executes?
  2. What bit pattern is in memory cell B8 when halt instruction executes?

5. Arithmetic/Logic Instructions

Logic Operations in Computing

The AND, OR, and XOR operations can be extended to combine two strings of bits to produce a single output string:

Operation Example Result Application
AND 10011010 & 11001001 10001000 Clearing specific bits (masking)
OR 10011010 | 11001001 11011011 Setting specific bits to 1
XOR 10011010 ^ 11001001 01010011 Toggling/flipping bits

Masking Techniques

Using AND for Masking

Purpose: Place 0s in specific parts of a bit pattern

Example:
Mask: 00001111
Data: 10101010
Result: 00001010
Only lower 4 bits preserved

With mask 00001111, we can conclude that the four most significant bits will be 0s, and the four least significant bits will be a copy of that part of the second operand.

Using OR for Setting Bits

Purpose: Place 1s in specific positions

Example:
Mask: 00100000
Data: 10101010
Result: 10101010 (unchanged)
OR: 00100000
Final: 10101010 OR 00100000 = 10101010
Sets bit 5 to 1 if not already

Mask 00100000 with OR forces a 1 in the third bit from the high-order end.

Using XOR for Complementation

Purpose: Form the complement (invert) of a byte

Example:
Mask: 11111111
Data: 10101010
Result: 01010101
Every bit is flipped

XOR with 11111111 produces the one's complement of the original byte.

🧮 Logic Operations Exercises

Exercise 3: Bit Isolation with Masking

Problem: Suppose you want to isolate the middle four bits of a byte by placing 0s in the other four bits without disturbing the middle four bits.

Questions:

  1. What mask must you use?
  2. What operation (AND, OR, XOR) should you use?

Exercise 3 Solution

Answer

Mask: 00111100

Operation: AND

Explanation: The mask 00111100 has 1s in the middle four positions and 0s elsewhere. ANDing with this mask will preserve the middle four bits while clearing the other four bits to 0.

Example:
Data: 10101010
Mask: 00111100
AND Result: 00101000
Middle 4 bits (1010) preserved, others cleared

6. Shift and Rotate Operations

Bit Movement Operations

Shift and rotate operations provide means for moving bits within a register. These operations are classified by direction of motion (right or left).

Rotation Operations

Bits moved out of one end reappear at the other end

Example: Rotate 65 (hex) right by 1 bit
Original: 0110 0101 (65 hex)
After rotate right: 1011 0010 (B2 hex)
Rightmost bit (1) moved to leftmost position

Logical Shift Operations

Bits shifted out are discarded; holes filled with 0s

Example: Logical shift right by 1 bit
Original: 0110 0101 (65 hex)
After shift right: 0011 0010 (32 hex)
Rightmost bit discarded, leftmost filled with 0
Operation Symbol Description Example (Binary) Example (Hex)
Rotate Right ROR Bits move right, bit exiting right enters left 0110 → 0011 (1-bit rotate) 65 → B2 (1-bit rotate)
Rotate Left ROL Bits move left, bit exiting left enters right 0110 → 1100 (1-bit rotate) 65 → CA (1-bit rotate)
Logical Shift Right SHR Bits move right, left filled with 0s 0110 → 0011 (1-bit shift) 65 → 32 (1-bit shift)
Logical Shift Left SHL Bits move left, right filled with 0s 0110 → 1100 (1-bit shift) 65 → CA (1-bit shift)

7. Practical Programming Exercise

🔧 Programming Challenge: Bit Manipulation

Exercise 4: Setting the Most Significant Bit

Objective: Using the machine language of Appendix C, write a program that places a 1 in the most significant bit of the memory cell whose address is A7 without modifying the remaining bits in the cell.

Requirements:

  • Load the current value from memory address A7
  • Create a mask with MSB = 1 (binary 10000000 = hex 80)
  • Use OR operation to set MSB to 1 while preserving other bits
  • Store result back to memory address A7
  • Include HALT instruction

Exercise 4 Solution

Complete Program Solution:
# Set MSB of memory address A7 to 1
12A7 # LOAD register 2 from memory cell A7
2380 # LOAD register 3 with value 80 (MSB mask: 10000000)
7023 # OR register 2 and 3, result in register 0
30A7 # STORE register 0 to memory cell A7
C000 # HALT
Step-by-Step Explanation:
  1. 12A7: Loads the current value from memory address A7 into register 2
  2. 2380: Loads the hexadecimal value 80 (binary 10000000) into register 3. This mask has only the MSB set to 1.
  3. 7023: Performs OR operation between register 2 (original value) and register 3 (mask). This sets the MSB to 1 while preserving all other bits.
  4. 30A7: Stores the result from register 0 back to memory address A7
  5. C000: Halts program execution
Example Execution:
If memory cell A7 contains: 01010101 (55 hex)
After OR with mask 10000000 (80 hex): 11010101 (D5 hex)
Result: MSB changed from 0 to 1, other bits unchanged

Homework Assignment - Week 3

Submission Method: PDF document via Google Classroom

Instructions: Answer all questions in your own words. Show all work for calculations and programming exercises.

Part 1: Theory Questions (40 points)

  1. Explain the three main components of a CPU and their specific functions. (10 points)
  2. What is the difference between a LOAD instruction and a STORE instruction in machine language? (5 points)
  3. Describe the three steps of the machine cycle (fetch, decode, execute). (10 points)
  4. What is the purpose of the Program Counter (PC) register? What happens to it after each instruction is fetched? (5 points)
  5. Explain how the AND operation can be used for "masking" bits. Provide a specific example with binary numbers. (10 points)

Part 2: Machine Language Translation (30 points)

Using the machine language described in Appendix C:

  1. Translate the following machine language instructions to English: (15 points)
    • 15B3
    • 702F
    • C000
    • 2356
    • 8A03
  2. Write machine language instructions for the following operations: (15 points)
    • ADD the contents of register 4 and register 7, leaving the result in register 1
    • JUMP to memory address 5C if the contents of register 3 equal the contents of register 0
    • LOAD register number 3 with the value stored at memory address 56
    • ROTATE register 2 four bits to the left
    • AND register B with register 9, result in register 0

Part 3: Bit Manipulation Exercises (30 points)

  1. Given the byte 11011010 (binary), what mask would you use with what operation to: (15 points)
    • Clear (set to 0) the first 3 bits (leftmost)
    • Set the last 2 bits to 1
    • Toggle (flip) the middle 4 bits
  2. Write a machine language program that clears the least significant bit (bit 0) of the value in memory address B2 without affecting the other 7 bits. Provide your answer in hexadecimal instruction format. (15 points)

Submission Guidelines

  • Include your name, student ID, and "Week 3 Homework" at the top of the document
  • Organize your answers clearly with question numbers
  • For programming exercises, show your work and explain your reasoning
  • Submit as a single PDF file named: "YourName_ID_Week3_HW.pdf"

Ready to Continue Your Computing Journey?

Understanding computer architecture and machine language is fundamental to all computing fields. This knowledge forms the basis for understanding how software interacts with hardware and is essential for cybersecurity professionals who need to understand system vulnerabilities at the lowest level.

📅 Next Week Preview: Storage Systems

What You'll Learn: Different types of computer memory (RAM, ROM, cache), secondary storage devices (HDD, SSD), memory hierarchy, and how data is stored and retrieved in computer systems.

Continue to Week 4: Storage Systems