Skip to content

Commit ac8bb1d

Browse files
author
mdrohansheikh
committed
Week 3 Updated
2 parents d587e84 + c70d703 commit ac8bb1d

File tree

10 files changed

+332
-5
lines changed

10 files changed

+332
-5
lines changed

.github/workflows/c-cpp.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: C/C++ CI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: configure
17+
run: ./configure
18+
- name: make
19+
run: make
20+
- name: make check
21+
run: make check
22+
- name: make distcheck
23+
run: make distcheck

.vscode/tasks.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
"tasks": [
33
{
44
"type": "cppbuild",
5+
<<<<<<< HEAD
56
"label": "C/C++: g++ build active file",
67
"command": "/usr/bin/g++",
8+
=======
9+
"label": "C/C++: g++.exe build active file",
10+
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
11+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225
712
"args": [
813
"-fdiagnostics-color=always",
914
"-g",
1015
"${file}",
1116
"-o",
17+
<<<<<<< HEAD
1218
"${fileDirname}/${fileBasenameNoExtension}"
19+
=======
20+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
21+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225
1322
],
1423
"options": {
1524
"cwd": "${fileDirname}"

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,8 @@ Coming Soon.
118118
## License 🛠️
119119
Open License. Feel Free to use & Modify
120120

121-
**Primary Contact:** 📬 MD. Rohan, Mail: [email protected]
121+
<<<<<<< HEAD
122+
**Primary Contact:** 📬 MD. Rohan, Mail: [email protected]
123+
=======
124+
**Primary Contact:** 📬 MD. Rohan, Mail: [email protected]
125+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225

Week 1/Week1.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@
8787
- **Boost**: For a wide range of utility libraries.
8888
- **Google Test**: For unit testing.
8989
- **CMake**: For build automation.
90-
- **GitHub/GitLab**: For version control.
90+
<<<<<<< HEAD
91+
- **GitHub/GitLab**: For version control.
92+
=======
93+
- **GitHub/GitLab**: For version control.
94+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225

Week 2/Assembler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ int main() {
4848
cout << "\nMachine Code:\n" << assemble(assemblyCode) << endl;
4949

5050
return 0;
51-
}
51+
<<<<<<< HEAD
52+
}
53+
=======
54+
}
55+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225

Week 2/Assembler.exe

422 KB
Binary file not shown.

Week 2/Week2.md

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12

23
# Week 2: C++ Program for Assembling and Converting Assembly Code to Machine Code
34

@@ -127,4 +128,148 @@ END
127128
Machine Code:
128129
<converted machine code>
129130
```
130-
- **Explanation**: The program first shows a sample input and its corresponding machine code output. It then prompts the user to enter their assembly code and converts it to machine code, displaying the result.
131+
- **Explanation**: The program first shows a sample input and its corresponding machine code output. It then prompts the user to enter their assembly code and converts it to machine code, displaying the result.
132+
=======
133+
# Week 2: Instruction Set Architecture (ISA)
134+
135+
## Assembler Program Description
136+
137+
The Assembler Program is a tool designed to convert assembly code into machine code. It allows users to write assembly instructions, which are then translated into the binary code that a CPU can execute. This program provides an intuitive interface for entering assembly instructions and viewing the corresponding machine code.
138+
139+
### How to Use the Program
140+
141+
1. **Input Assembly Code**:
142+
- Enter your assembly instructions in the "Input" section. Each instruction should be on a new line.
143+
144+
2. **Assemble the Code**:
145+
- Click the "Assemble" button to convert the assembly code into machine code.
146+
147+
3. **View Output**:
148+
- The resulting machine code will be displayed in the "Output" section.
149+
150+
### Sample Input and Output
151+
152+
**Sample Input**:
153+
```assembly
154+
ADD R1, R2, R3
155+
SUB R4, R5, R6
156+
LOAD R1, [R2]
157+
STORE R3, [R4]
158+
```
159+
**Sample Output**:
160+
```text
161+
0001000100100011
162+
0010010001010110
163+
0011000100100000
164+
0100001101000000
165+
```
166+
167+
### ADD:
168+
**Operation**: Adds the values from two registers and stores the result in a destination register.
169+
**Format**: `ADD R1, R2, R3`
170+
**Description**: `R1 = R2 + R3`
171+
172+
### SUB:
173+
**Operation**: Subtracts the value of the second register from the first register and stores the result in the destination register.
174+
**Format**: `SUB R1, R2, R3`
175+
**Description**: `R1 = R2 - R3`
176+
177+
### LOAD:
178+
**Operation**: Loads a value from memory into a register.
179+
**Format**: `LOAD R1, [R2]`
180+
**Description**: `R1 = Memory[R2]`
181+
182+
### STORE:
183+
**Operation**: Stores the value from a register into memory.
184+
**Format**: `STORE R1, [R2]`
185+
**Description**: `Memory[R2] = R1`
186+
187+
### MUL (Multiplication):
188+
**Operation**: Multiplies the values from two registers and stores the result in a destination register.
189+
**Format**: `MUL R1, R2, R3`
190+
**Description**: `R1 = R2 * R3`
191+
192+
### DIV (Division):
193+
**Operation**: Divides the value of the first register by the value of the second register and stores the result in a destination register.
194+
**Format**: `DIV R1, R2, R3`
195+
**Description**: `R1 = R2 / R3`
196+
197+
### AND (Bitwise AND):
198+
**Operation**: Performs a bitwise AND operation on two registers and stores the result in a destination register.
199+
**Format**: `AND R1, R2, R3`
200+
**Description**: `R1 = R2 & R3`
201+
202+
### OR (Bitwise OR):
203+
**Operation**: Performs a bitwise OR operation on two registers and stores the result in a destination register.
204+
**Format**: `OR R1, R2, R3`
205+
**Description**: `R1 = R2 | R3`
206+
207+
### XOR (Bitwise XOR):
208+
**Operation**: Performs a bitwise XOR operation on two registers and stores the result in a destination register.
209+
**Format**: `XOR R1, R2, R3`
210+
**Description**: `R1 = R2 ^ R3`
211+
212+
### NOT (Bitwise NOT):
213+
**Operation**: Performs a bitwise NOT operation on a single register and stores the result in the same register.
214+
**Format**: `NOT R1`
215+
**Description**: `R1 = ~R1`
216+
217+
### JMP (Jump):
218+
**Operation**: Jumps to a specified memory address.
219+
**Format**: `JMP [address]`
220+
**Description**: `PC = address`
221+
222+
### JZ (Jump if Zero):
223+
**Operation**: Jumps to a specified memory address if the zero flag is set.
224+
**Format**: `JZ [address]`
225+
**Description**: `if (ZF == 1) PC = address`
226+
227+
### JNZ (Jump if Not Zero):
228+
**Operation**: Jumps to a specified memory address if the zero flag is not set.
229+
**Format**: `JNZ [address]`
230+
**Description**: `if (ZF == 0) PC = address`
231+
232+
### CALL (Call Subroutine):
233+
**Operation**: Calls a subroutine at a specified memory address.
234+
**Format**: `CALL [address]`
235+
**Description**: `SP = PC; PC = address`
236+
237+
### RET (Return from Subroutine):
238+
**Operation**: Returns from a subroutine to the instruction following the CALL.
239+
**Format**: `RET`
240+
**Description**: `PC = SP`
241+
242+
### CMP (Compare):
243+
**Operation**: Compares the values of two registers and sets the appropriate flags.
244+
**Format**: `CMP R1, R2`
245+
**Description**: `ZF = (R1 == R2); SF = (R1 < R2)`
246+
247+
248+
## Document the Instruction Formats:
249+
Each instruction can have a specific format, often including the opcode and operands. Here’s a simple example:
250+
251+
| Instruction | Opcode | Format | Description |
252+
|-------------|--------|------------------|----------------------------------|
253+
| ADD | 0001 | ADD R1, R2, R3 | R1 = R2 + R3 |
254+
| SUB | 0010 | SUB R1, R2, R3 | R1 = R2 - R3 |
255+
| LOAD | 0011 | LOAD R1, [R2] | R1 = Memory[R2] |
256+
| STORE | 0100 | STORE R1, [R2] | Memory[R2] = R1 |
257+
| MUL | 0101 | MUL R1, R2, R3 | R1 = R2 * R3 |
258+
| DIV | 0110 | DIV R1, R2, R3 | R1 = R2 / R3 |
259+
| AND | 0111 | AND R1, R2, R3 | R1 = R2 & R3 |
260+
| OR | 1000 | OR R1, R2, R3 | R1 = R2 | R3 |
261+
| XOR | 1001 | XOR R1, R2, R3 | R1 = R2 ^ R3 |
262+
| NOT | 1010 | NOT R1 | R1 = ~R1 |
263+
| JMP | 1011 | JMP [address] | PC = address |
264+
| JZ | 1100 | JZ [address] | if (ZF == 1) PC = address |
265+
| JNZ | 1101 | JNZ [address] | if (ZF == 0) PC = address |
266+
| CALL | 1110 | CALL [address] | SP = PC; PC = address |
267+
| RET | 1111 | RET | PC = SP |
268+
| CMP | 0000 | CMP R1, R2 | ZF = (R1 == R2); SF = (R1 < R2) |
269+
270+
271+
#### Create a Simple Assembler:
272+
1. **Parser**: Read assembly code and parse each line to identify the instruction and operands.
273+
2. **Opcode Mapping**: Create a mapping from instruction names to their corresponding opcode values.
274+
3. **Code Generation**: Convert parsed instructions into machine code by replacing instruction names with opcodes and resolving operand addresses.
275+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225

Week 3/Register.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iostream>
2+
<<<<<<< HEAD
23
#include <sstream>
34
#include <map>
45
#include <vector>
@@ -139,5 +140,66 @@ int main() {
139140
cout << "Final Register States:\n";
140141
cpu.registers.display();
141142

143+
=======
144+
#include <vector>
145+
146+
class ALU {
147+
public:
148+
int performOperation(const std::string& operation, int operand1, int operand2) {
149+
if (operation == "ADD") return operand1 + operand2;
150+
if (operation == "SUB") return operand1 - operand2;
151+
if (operation == "AND") return operand1 & operand2;
152+
if (operation == "OR") return operand1 | operand2;
153+
return 0; // Default case
154+
}
155+
};
156+
157+
class CPU {
158+
int programCounter;
159+
int instructionRegister;
160+
public:
161+
CPU() : programCounter(0), instructionRegister(0) {}
162+
163+
int getPC() const { return programCounter; }
164+
void setPC(int value) { programCounter = value; }
165+
166+
int getIR() const { return instructionRegister; }
167+
void setIR(int value) { instructionRegister = value; }
168+
169+
void fetchInstruction(const std::vector<int>& memory) {
170+
instructionRegister = memory[programCounter];
171+
programCounter++;
172+
}
173+
};
174+
175+
class Registers {
176+
int reg[4]; // Let's assume 4 general-purpose registers
177+
public:
178+
int getRegister(int index) const { return reg[index]; }
179+
void setRegister(int index, int value) { reg[index] = value; }
180+
};
181+
182+
183+
int main() {
184+
ALU alu;
185+
Registers regs;
186+
CPU cpu;
187+
188+
std::vector<int> memory = {0x010203, 0x040506}; // Example instructions in memory
189+
190+
// Simulating fetching and executing instructions
191+
for (int i = 0; i < memory.size(); ++i) {
192+
cpu.fetchInstruction(memory);
193+
int instruction = cpu.getIR();
194+
int operand1 = (instruction & 0xFF0000) >> 16; // Extracting operands
195+
int operand2 = (instruction & 0x00FF00) >> 8;
196+
int operation = instruction & 0x0000FF;
197+
198+
// For simplicity, let's assume operations are encoded as integers
199+
int result = alu.performOperation("ADD", operand1, operand2); // Assuming ADD operation
200+
std::cout << "Instruction: " << instruction << ", Result: " << result << std::endl;
201+
}
202+
203+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225
142204
return 0;
143205
}

Week 3/Register.exe

252 KB
Binary file not shown.

Week 3/Week3.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Week 3: Basic CPU Components
22

3+
<<<<<<< HEAD
34
### 1. ALU Class
45
```cpp
56
class ALU {
@@ -219,4 +220,79 @@ int main() {
219220
- Converts the assembly code to machine code using the `assemble` function.
220221
- Displays the converted machine code.
221222
- Displays the initial state of the registers.
222-
- Loads the machine code into the
223+
- Loads the machine code into the
224+
=======
225+
## Objective
226+
The goal of this week is to implement the core components of a CPU. This includes building the Arithmetic Logic Unit (ALU), implementing general-purpose registers, and creating the program counter and instruction register.
227+
228+
## Program Counter and Instruction Register
229+
230+
### Overview
231+
The **Program Counter (PC)** and **Instruction Register (IR)** are critical components in the design of a CPU. They work together to ensure that instructions are fetched, decoded, and executed in a sequential manner.
232+
233+
### Program Counter (PC)
234+
The PC holds the memory address of the next instruction to be executed. It increments automatically after each instruction fetch, ensuring the CPU processes instructions sequentially unless a jump or branch instruction modifies the sequence.
235+
236+
### Instruction Register (IR)
237+
The IR holds the currently executing instruction fetched from memory. It temporarily stores the instruction so that the CPU can decode and execute it.
238+
239+
## Program Structure
240+
241+
[Source Code](/Week%203/Register.cpp)
242+
243+
## Explanation
244+
245+
### Program Counter (PC)
246+
- **Initialization**: The PC is initialized to `0` in the CPU class constructor.
247+
- **Fetching**: In the `fetchInstruction` method, the PC points to the memory address of the next instruction.
248+
- **Incrementing**: After fetching an instruction, the PC is incremented to point to the next instruction.
249+
250+
### Instruction Register (IR)
251+
- **Holding Instructions**: The IR holds the instruction fetched from memory.
252+
- **Fetching**: In the `fetchInstruction` method, the IR is set to the value of the memory at the address pointed to by the PC.
253+
- **Decoding and Executing**: The fetched instruction can then be decoded and executed by the CPU.
254+
255+
### Simulated CPU Execution
256+
- **Memory Representation**: A vector `memory` is used to represent the instruction memory.
257+
- **Fetching and Storing**: The CPU fetches instructions from this memory, stores them in the IR, and increments the PC.
258+
- **Execution**: The ALU performs operations based on the fetched instructions, demonstrating how arithmetic and logical operations would be executed.
259+
260+
## Arithmetic Logic Unit (ALU)
261+
262+
The **Arithmetic Logic Unit (ALU)** is a critical component of the CPU that performs arithmetic and logic operations. It's the execution core of the CPU, where all the actual computation happens.
263+
264+
### Key Functions of the ALU
265+
266+
#### Arithmetic Operations
267+
- **Addition**: Adds two numbers.
268+
- **Subtraction**: Subtracts one number from another.
269+
- **Multiplication**: Multiplies two numbers (though sometimes handled by a separate multiplier unit).
270+
- **Division**: Divides one number by another (though often handled by a separate division unit).
271+
272+
#### Logical Operations
273+
- **AND**: Performs a bitwise AND operation.
274+
- **OR**: Performs a bitwise OR operation.
275+
- **NOT**: Performs a bitwise NOT operation, inverting each bit.
276+
- **XOR**: Performs a bitwise XOR operation.
277+
278+
#### Comparison Operations
279+
- **Equality**: Checks if two numbers are equal.
280+
- **Inequality**: Checks if two numbers are not equal.
281+
- **Greater Than**: Checks if one number is greater than another.
282+
- **Less Than**: Checks if one number is less than another.
283+
284+
#### Shift Operations
285+
- **Left Shift**: Shifts bits to the left, filling with zeros.
286+
- **Right Shift**: Shifts bits to the right, with the option of filling with zeros (logical shift) or preserving the sign bit (arithmetic shift).
287+
288+
### ALU Structure
289+
An ALU is typically composed of:
290+
- **Input Registers**: These hold the operands (the numbers to be operated on).
291+
- **Operation Decoder**: This decodes the operation to be performed (e.g., add, subtract).
292+
- **Arithmetic Circuitry**: Performs arithmetic operations.
293+
- **Logic Circuitry**: Performs logic operations.
294+
- **Output Register**: Holds the result of the operation.
295+
296+
## Conclusion
297+
The Program Counter (PC) and Instruction Register (IR) are essential for the sequential execution of instructions in a CPU, while the ALU is responsible for performing the core computations. Together, these components form the backbone of CPU operations, enabling the execution of programs by processing instructions methodically and efficiently. This project lays the foundation for understanding how modern processors handle instruction cycles and perform computations, providing a solid base for further exploration into CPU architecture and design. 🚀
298+
>>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225

0 commit comments

Comments
 (0)