Instruction set architecture
Введено
6:22
Архитектура компьютера
This course looks at the machine below the program, and how it earns its speed. The first two units are the contract, the instruction set, and the machine that…
Озвучивание на английском · переключаемые субтитры на английском и китайском языках
В этом уроке
| English | Значение |
|---|---|
| Computer architecture/kəmˈpjuːtə ˈɑːkɪtektʃə/ | The machine below the program. |
| instruction set architecture/ɪnˈstrʌkʃn set ˈɑːkɪtektʃə/ | архитектура набора инструкций |
| RISC/rɪsk/ | |
| load-store/ləʊd stɔː/ | a processor design in which only load and store instructions access memory; arithmetic works on registers |
| immediate/ɪˈmiːdɪət/ | A constant written inside an instruction itself rather than held in a register. |
| byte-addressable/baɪt əˈdresəbl/ | memory in which every byte has its own address |
| CISC/sɪsk/ | Complex instruction set computer: a design with many, varied instructions, some doing several steps, such as x86. |
| big-endian/bɪɡ enˈdɪən/ | storing the most significant byte of a word at the lowest address |
| little-endian/ˈlɪtl enˈdɪən/ | storing the least significant byte of a word at the lowest address |
| byte addressing |
The contract
- Computer architecture 计算机体系结构 is the machine below the program. The instruction set architecture 指令集架构 (ISA) is its contract with software: it states everything a compiler may rely on and nothing about the wires. Two machines with the same ISA run the same binaries.
- Our teaching ISA is in the RISC 精简指令集 style: fixed 32-bit instructions, load-store 加载存储 (only
lwandswtouch memory), and 16 registers with R0 always equal to zero.
R-type: opcode(4) | rd(4) | rs(4) | rt(4) | shift(4) | funct(8), 28 bits used and padded with zeros to 32. I-type: opcode(4) | rt(4) | rs(4) | immediate(16).
The ISA is best described as —
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
It states everything software may rely on and nothing about implementation.
Encode and decode
- Encode
add R3, R1, R2with opcode 1: the fields are $0001\,0011\,0001\,0010\,0000\,00000001$. Decode opcode 2, rt 4, rs 1, immediate $127$: this isaddi R4, R1, 127. - A 16-bit two's complement immediate 立即数 runs from $-32\,768$ to $32\,767$. So
addi R2, R0, 40000cannot be encoded and needs two instructions.

A 16-bit signed immediate can hold —
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
The range is −32 768 to 32 767; 40 000 needs two instructions.
addi R2, R0, 40000 fits in one instruction of the teaching ISA.
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
The immediate is a 16-bit two's complement field, so it reaches only $32\,767$. Loading $40\,000$ needs two instructions, for example an upper-half load and an add.
What R0 buys
- With byte-addressable 按字节编址 memory and 32-bit words, an aligned
lweffective address is a multiple of 4. A 16-bit signed offset reaches a window of $2^{16}$ bytes = 64 KiB around the base register. addi R0, R0, 0is the standard no-op;add R5, R0, R0sets R5 to zero in one cycle;beq R1, R0, loopcompares with zero without a constant. A fixed zero register removes a whole group of move instructions.
R0 hardwired to zero lets a program —
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
add R5, R0, R0 zeroes R5 in one cycle — no move-immediate needed.
Addressing modes and branch reach
- There are four ways to name an operand:
| Mode: where the operand is | Example |
|---|---|
| Register: in a register | add R3, R1, R2 |
| Immediate: in the instruction | addi R4, R1, 127 |
| Base + offset: memory at R1 + 8 | lw R4, 8(R1) |
| PC-relative: a branch target | beq R1, R2, -3 |
- A branch's immediate counts instructions, not bytes: target $= \text{PC} + 4 + 4 \times \text{imm}$. A
beqat $0x100$ with imm $-3$ goes to $0x104 - 12 = 0xF8$. - With a 16-bit immediate, a branch reaches about $\pm 32\,768$ instructions ($\pm 128$ KiB). CISC 复杂指令集 machines such as x86 use instructions of 1 to 15 bytes and allow memory operands. RISC keeps decoding simple, and that makes pipelining easy.
A beq sits at address 512 with immediate 5. What is the target address (in decimal)?
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
PC + 4 = 516, plus 4 x 5 = 20, gives 536.
In a load-store ISA, which instruction may read memory?
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
Only loads and stores touch memory; arithmetic works on registers.
Which byte comes first: endianness
- A 32-bit word fills four byte addresses. Store
0x12345678at0x100: a big-endian 大端 machine puts0x12at0x100; a little-endian 小端 machine, such as x86 and most RISC-V systems, puts0x78there. - Inside one machine you never see it. It causes bugs when bytes cross a boundary: a file, a network packet (network order is big-endian), or a pointer cast to
char*.
0x12345678 stored at 0x100 on a little-endian machine. The byte at 0x100 is:
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
Little-endian stores the least significant byte at the lowest address.
Worked example: decode a branch target
- A
beqat0x200holds immediate 立即数 $5$. The target is $\text{PC} + 4 + 4 \times 5 = 0x204 + 20 = \mathbf{0x218}$, five instructions after the next one. - Going backwards needs a negative immediate. To reach
0x1F0from0x200, $\text{imm} = (0x1F0 - 0x204)/4 = -5$. Loops are backward branches, so their immediates are negative.
A beq at 0x200 with immediate 5 branches to:
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
$0x204 + 4 \times 5 = 0x218$.
Immediate for a beq at 0x200 that targets 0x1F0?
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
$(0x1F0 - 0x204)/4 = -20/4 = -5$.
Marks that slip away
- Word index as byte offset. With byte addressing 字节编址, word $3$ of an array is at offset $12$, not $3$.
- Forgetting the immediate's range. A 16-bit signed field stops at $32\,767$; a larger constant needs two instructions.
- Counting a branch from itself. The target is $\text{PC} + 4 + 4 \times \text{imm}$: the offset counts words from the next instruction.
Recap
- The ISA is the contract between hardware and software; one ISA runs the same binaries on different hardware.
- Fixed 32-bit RISC formats, load-store 加载存储, R0 always zero.
- A 16-bit immediate runs from $-32\,768$ to $32\,767$; branch target $= \text{PC} + 4 + 4 \times \text{imm}$.
Что пошло не так? Это увидит только команда сайта.