Unidad0 Instrucciones
Understanding CPU Instructions
What is an Instruction?
- The discussion begins with defining what an instruction is and how it is formed within a CPU. It highlights the role of microprogrammed and hardwired control units in decoding instructions.
- An instruction must indicate the operation to be performed, known as the opcode or operation code, which specifies actions like addition, subtraction, shifting, or comparison.
Operand Identification
- After determining the operation, it's essential to identify the operands involved. Typically, two operands are required for operations such as addition or subtraction.
- The process includes locating these operands either in memory or within internal CPU registers. Understanding where each operand resides is crucial for executing instructions effectively.
Result Storage and Next Instruction
- Once the operands are identified, it’s necessary to decide where to store the result of the operation. This step ensures that results are not lost after computation.
- After completing one instruction, the CPU needs guidance on which instruction to execute next. This leads into discussing how instructions can be organized sequentially in memory.
Program Counter Functionality
- Instead of embedding addresses directly into instructions for subsequent operations, a program counter (PC) keeps track of which instruction will execute next by incrementing its value continuously.
- The PC simplifies execution flow by indicating where in memory the next instruction resides without needing explicit references within each instruction itself.
Intermediate Results and Memory Management
- Often results from computations may not be final but intermediate; thus, storing them temporarily in memory becomes necessary during complex calculations involving multiple steps.
- For example, if two operands are summed and one operand's original value isn't needed anymore, it can be overwritten with a new result to save space and improve efficiency.
Addressing Modes Explained
- The method used by CPUs to access data is referred to as addressing mode. It determines how operand locations are specified within instructions based on supported modes by different architectures.
- There exist various theoretical addressing modes; however, practical implementations vary among manufacturers. Some instructions may not require any operands at all (implicit mode).
Data Location: Registers vs Memory
- When an instruction contains data (operands), they typically reside either in CPU registers (register mode) or in main memory (memory mode).
Addressing Data Access in Memory
Absolute Addressing Mode
- The concept of absolute addressing is introduced, where all bits that make up the address are known, allowing direct access to data stored in memory.
Relative Addressing Mode
- In cases where an absolute address isn't available, a reference from an internal register is used. The offset or displacement from this reference determines how far to access the desired data.
Indirect Addressing Mode
- When the instruction contains the address of another address (the "address of the data"), it is referred to as indirect or absolute indirect addressing. This requires multiple memory accesses to retrieve the actual data.
Register Indirect Addressing
- If the address of the data is stored in a CPU register, it simplifies access since only one memory fetch is needed using that register's value.
Instruction Formats and Operations
- The R-type instruction format involves operands being registers. For example,
add r0, r3, r5indicates adding values from registersr3andr5, storing the result inr0.
Understanding Immediate Values and Constants
Immediate Value Usage
- Instructions can also include immediate values or constants directly within them. This allows for operations involving both registers and constant values without needing additional memory accesses.
ALU Operations with Constants
- An example illustrates how an operation like
addi x0, x3, 2adds a constant (2) to a value in registerx3, storing it inx0. Negative constants can effectively perform subtraction.
Memory Access Techniques
Store Instruction Format
- The S-type instruction format (Store type), which comes from "store," uses base plus offset addressing. A complete 32-bit memory address is constructed by adding a base address from a register with an offset.
Example of Memory Storage Operation
Understanding Addressing Modes in RISC-V
Storing Values in Memory
- The content of register r0 is 80 51 65 02; after executing the Store instruction, this value will be stored at memory address 1 2 3 4 5 6 7.
- In RISC-V assembly syntax, the Store Word (swb) instruction is used to save a 32-bit word from register x0 to an address calculated as X3 + offset (e.g., +2 or -2).
Jump Instructions and Program Counter
- The jump addressing mode (type J) modifies the program counter (PC), using a constant added to its current value.
- This jump instruction updates the PC with the sum of its current value and a specified constant, which can be positive or negative.
Saving Return Addresses
- When executing a jump, the current PC value is saved into a designated register (usually R1), allowing for return to that point after subroutine execution.
- For example, when using
jal x31, it saves the updated PC into x1 for later retrieval upon returning from a subroutine.
Efficient Use of Memory Addresses
- The
jlrinstruction allows jumping back by taking the PC's value from another register plus an offset, facilitating efficient control flow.
- Different addressing modes exist to avoid storing absolute memory addresses directly in instructions, which would consume excessive space.
Reducing Instruction Size
- Using offsets instead of full addresses reduces memory consumption; for instance, an addition operation may require three addresses but can be optimized with base addressing.
- By utilizing base addresses and offsets for operands and results, fewer bytes are needed compared to storing complete addresses.
Summary of Addressing Mode Benefits
- Implementing a program counter that auto-increments helps streamline instruction processing by eliminating redundant address storage.