

指令组成:操作码(OP) + 地址码(Address) 操作码:决定指令功能(如加法、跳转) 地址码:指示操作数或指令地址
/**
* 模拟指令格式类
*/
public class Instruction {
private int opcode; // 操作码(4位,0-15)
private int address; // 地址码(28位,0-2^28-1)
// 构造方法:传入操作码和地址码
public Instruction(int opcode, int address) {
this.opcode = opcode & 0x0F; // 保留低4位作为操作码
this.address = address & 0xFFFFFFF; // 保留低28位作为地址码
}
// 获取操作码
public int getOpcode() {
return opcode;
}
// 获取地址码
public int getAddress() {
return address;
}
public static void main(String[] args) {
// 示例:加法指令(操作码0001),操作数地址0x123456
Instruction addInst = new Instruction(0x01, 0x123456);
System.out.println("操作码:" + addInst.getOpcode()); // 输出1
System.out.println("地址码:" + addInst.getAddress()); // 输出123456
}
}数据(0x12345678) | 大端模式(高位在前) | 小端模式(低位在前) |
|---|---|---|
地址 0x00 | 0x12 | 0x78 |
地址 0x01 | 0x34 | 0x56 |
地址 0x02 | 0x56 | 0x34 |
地址 0x03 | 0x78 | 0x12 |
public class EndianConverter {
// 大端转小端
public static byte[] bigToLittle(byte[] bigEndian) {
byte[] littleEndian = new byte[bigEndian.length];
for (int i = 0; i < bigEndian.length; i++) {
littleEndian[i] = bigEndian[bigEndian.length - 1 - i];
}
return littleEndian;
}
public static void main(String[] args) {
byte[] big = {0x12, 0x34, 0x56, 0x78}; // 大端模式数据
byte[] little = bigToLittle(big); // 转换为小端
System.out.println("小端模式:");
for (byte b : little) {
System.out.print(String.format("%02X ", b)); // 输出78 56 34 12
}
}
}立即寻址:操作数直接在指令中
int a = 10; // 立即数10直接寻址:地址码为操作数地址
int[] arr = {1, 2, 3};
int b = arr[1]; // 直接寻址arr[1]间接寻址:地址码为指针地址
int x = 5;
int* ptr = &x; // 指针存储x的地址(间接寻址)寄存器寻址:操作数在寄存器中
// 假设寄存器R1存储值为10
int c = R1; // 寄存器寻址操作码(4 位) | 寻址方式(2 位) | 寄存器号(3 位) | 地址码(23 位) |
|---|---|---|---|
0001(加法) | 01(直接寻址) | 001(R1) | 0x123456 |
/**
* 扩展指令格式类
*/
public class ExtendedInstruction {
private int opcode; // 4位
private int addressingMode; // 2位(0-3)
private int regNum; // 3位(0-7)
private int address; // 23位
public ExtendedInstruction(int opcode, int addressingMode, int regNum, int address) {
this.opcode = opcode & 0x0F;
this.addressingMode = addressingMode & 0x03;
this.regNum = regNum & 0x07;
this.address = address & 0x1FFFFFF; // 23位掩码
}
public static void main(String[] args) {
// 示例:乘法指令(0010),寄存器寻址(01),R2,地址0x89ABC
ExtendedInstruction mulInst = new ExtendedInstruction(0x02, 0x01, 0x02, 0x89ABC);
System.out.println("操作码:" + mulInst.opcode); // 2
System.out.println("寻址方式:" + mulInst.addressingMode); // 1
}
}特性 | RISC | CISC |
|---|---|---|
指令数量 | 少(简单指令) | 多(复杂指令) |
执行周期 | 单周期 | 多周期 |
设计重点 | 硬件执行效率 | 软件兼容性 |
典型架构 | ARM、RISC-V | x86 |
```mindmap
## **7.1 机器指令**
- 指令格式:操作码+地址码
## **7.2 操作数与操作类型**
- 数据类型:整数/浮点/字符
- 存储方式:大端/小端
- 操作类型:算术/逻辑/控制
## **7.3 寻址方式**
- 指令寻址:顺序/跳跃
- 数据寻址:立即/直接/间接
## **7.4 指令格式设计**
- 设计因素:字长/操作数/寻址
- 案例:32位指令格式
## **7.5 RISC技术**
- 特征:简单指令/单周期
- 对比:与CISC的主要区别
💡 说明:
📢 欢迎留言讨论指令系统的难点! 如果需要某部分的扩展案例或代码优化,可随时告知~