Introduction to Programming - (C Language) - Unit : 1 - Program counter & Data measurement
PROGRAM COUNTER
The program counter (PC), also known as the instruction pointer (IP)
in some architectures, is a vital component of a computer's central processing
unit (CPU) or microprocessor. It is a special-purpose register that keeps track
of the memory address of the next instruction to be fetched and executed by the
CPU. In other words, it points to the location in memory where the CPU should
read the next machine instruction.
Here's how the program counter works within the context of executing
a program:
Initialization: When a program is loaded
into memory and execution begins, the program counter is initialized to point
to the memory address of the first instruction in the program.
Instruction Fetch: The CPU reads the
instruction at the memory address pointed to by the program counter and
increments the program counter to point to the next instruction. This is often
a simple increment operation, although in some cases, it may be more complex,
depending on the instruction set architecture (ISA) of the CPU.
Execution: The CPU decodes and executes
the instruction it just fetched. This instruction might include arithmetic
operations, data transfers, control flow instructions (like jumps and
branches), or other operations.
Repeat: Steps 2 and 3 are repeated in a
loop until the program completes or encounters a control flow instruction that
changes the program counter's value (e.g., a jump or branch instruction). Such
control flow instructions can alter the flow of execution by causing the
program counter to point to a different memory address.
The program counter is crucial for maintaining the sequential
execution of instructions in a program. It ensures that the CPU knows where to
fetch the next instruction and keeps the program's execution on track.
Program counter’s role in computer architecture:
Size: The size of the program counter
register depends on the computer architecture. In 32-bit systems, it's
typically 32 bits long, while in 64-bit systems, it's 64 bits long. This
determines the range of memory addresses it can point to.
Incrementing: After fetching an
instruction, the program counter is typically incremented by the size of the
instruction or the size of the memory address (e.g., 4 bytes in a 32-bit
system, 8 bytes in a 64-bit system) to point to the next instruction's address.
This is known as the "program counter increment."
Exception Handling: During program
execution, exceptions or interrupts (e.g., hardware interrupts or software
exceptions like divide-by-zero errors) can occur. In such cases, the program counter's
value can be temporarily saved to handle the exception and then restored to
resume normal program execution once the exception is handled.
Branching and Control Flow:
In addition to simple incrementing, the program counter can be
modified explicitly by branching and control flow instructions.
Conditional Branches: Conditional branch
instructions allow the program counter to be set to different addresses based
on certain conditions. For example, in an if-else statement, the program
counter might be set to the address of the "if" block or the
"else" block depending on the condition's outcome.
Unconditional Jumps: Unconditional jump
instructions can directly set the program counter to a specific memory address,
enabling non-sequential execution. This is commonly used for loops and function
calls.
Pipelining: In modern CPUs, instruction
fetching and execution often occur in pipelines to maximize performance. The
program counter plays a crucial role in ensuring that the correct instructions
are fetched in the correct order and at the right time.
Privilege Levels: Some CPUs, especially
in multi-core and multi-threaded systems, have different privilege levels
(e.g., user mode and kernel mode). The program counter may have separate
versions or restrictions depending on the privilege level to control access to
sensitive parts of the system.
Context Switching: In multi-tasking
operating systems, the program counter is saved during a context switch when
the CPU switches from executing one process to another. This allows the CPU to
resume execution of the previously running process from where it left off.
Security: Security features like address
space layout randomization (ASLR) can randomize the base address of a program,
making it harder for attackers to predict the location of specific functions.
The program counter is involved in managing this address translation.
Overall, the program counter is a vital component in the CPU's
execution of instructions, managing control flow, and enabling the sequential
execution of programs. Its operation and features may vary depending on the
specific architecture of the CPU.
DATA MEASUREMENT
Computer doesn’t know our language. It can understand only Machine Language
(0,1). Computer reads the data
in binary form. 0,1 are Called bits or binary digits. 0 means False, Off, No and 1 means True, On, Yes. Data converts
into ASCII code and then converts into Binary code. ASCII means (American Standard code for Information Interchange). ASCII codes are 256 (0 – 255)
8 Bits (0,1) are equal to 1 Byte (1 Character)
8 bits |
= |
1 Byte |
1024 Bytes |
= |
1 K.B ( Kilo Byte ) |
1024 K.B |
= |
1 M.B ( Mega Byte ) |
1024 M.B |
= |
1 G.B ( Giga Byte
) |
1024 G.B |
= |
1 T.B ( Terra
Byte ) |
1024 T.B |
= |
1 P.B ( Peta
Byte ) |
1024 P.B |
= |
1 E.T ( Exa Byte ) |
1024 E.T |
= |
1 Z.T ( Zeta Byte
) |
1024 Z.T |
= |
1 Y.T ( Yotta Byte ) |
Comments
Post a Comment