Machine instructions
مُقدَّم
5:52
أساسيات أنظمة الحاسوب
This course follows a program from a single bit to a running process. The first two units are the alphabet: how bits encode data, and how logic gates compute…
سرد باللغة الإنجليزية · ترجمة نصية بالإنجليزية و中文 يمكنك التبديل بينهما
في هذا الدرس
| English | المعنى |
|---|---|
| instructions/ɪnˈstrʌkʃnz/ | |
| immediate/ɪˈmiːdɪət/ | A constant written inside an instruction itself rather than held in a register. |
| addressing mode/əˈdresɪŋ məʊd/ | The rule an instruction uses to specify where an operand is or how its address is obtained. |
| RISC/rɪsk/ | Reduced instruction set computer: an instruction-set design approach emphasising comparatively simple operations. |
| CISC/sɪsk/ | Complex instruction set computer: a design with many, varied instructions, some doing several steps, such as x86. |
| countdown loop | |
| base register | |
| Base address/beɪs əˈdres/ | |
| loop counter/luːp ˈkaʊntə/ |
One line of C, several machine steps
- The line
a[i] = a[i] + 1looks like one action. The processor carries it out as a short list of tiny instructions 指令. - Every compiler targets this small vocabulary, and every speed question ends here. This lesson reads it.
The instruction kinds
add rd, rs1, rs2adds two registers;addiadds an immediate 立即数, a constant written inside the instruction.lw(load word) andsw(store word) move data between memory and registers;beqandbnecompare two registers and branch.
Match each instruction to what it does.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Four instruction kinds cover arithmetic, memory and control.
Addressing modes
- The addressing mode 寻址方式 says where an operand lives: in a register, in the instruction (immediate), or in memory at base + offset.
lw R1, 8(R2)reads memory at address R2 + 8. The 8 is an offset, not the value 8.

In lw R1, 0(R2) the 0 is —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Address = R2 + 0. This is base + offset addressing.
R2 holds 400. Which memory address does lw R1, 12(R2) read?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Base + offset = 400 + 12 = 412.
Why RISC only loads and stores
- In RISC 精简指令集 designs, only
lwandswtouch memory; arithmetic works register to register. One simple shape decodes fast and fits a pipeline well. - CISC 复杂指令集 designs let arithmetic read memory directly. The code is shorter but harder to pipeline: more instructions against faster instructions.
In a RISC load/store design, add can take one of its inputs straight from memory.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Only lw and sw touch memory. add works on registers only.
A loop is two instructions
loop: addi R1, R1, -1thenbne R1, R0, loop. R0 always holds 0, so this means "subtract 1, and jump back unless R1 is now 0".- This pair can implement a countdown loop 倒计数循环 whose body executes a known positive number of times; a general
whileloop needs code for its own condition, including an initial test when necessary.
A C while loop that counts down compiles to the pair —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Two instructions per pass — decrement, compare with zero, jump back.
R1 starts at 6. The loop is addi R1, R1, -1 then bne R1, R0, loop. How many times does bne jump back?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The body runs 6 times. After the sixth pass R1 is 0, so the last bne falls through; it jumps back 5 times.
Arrays are address arithmetic
- Element
a[i]lives at base + $i$ × element size. With 4-byte words,a[5]is at base + 20. - So a pointer is just a base register 基址寄存器, and indexing is a multiply and an add.
An array of 4-byte integers starts at address 2000. At which address is a[7]?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
2000 + 7 × 4 = 2028.
Worked example: a[i] = a[i] + 1
- Base address 基地址 in R2, $i = 3$, 4-byte words: the offset is $3 \times 4 = 12$, so the element is at R2 + 12.
lw R5, 12(R2)loads it;addi R5, R5, 1adds one;sw R5, 12(R2)stores it back: load, add, store.
Put the machine steps of a[i] = a[i] + 1 in order.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Address, load, add, store — indexing is scaled address arithmetic.
Worked example: counting a loop
- R1 starts at 3. Pass 1: R1 becomes 2,
bnejumps back. Pass 2: R1 becomes 1, jumps back. Pass 3: R1 becomes 0, falls through. - The body runs 3 times and
bnejumps back only twice.
Three common slips
- Reading
0(R2)as the value 0. It is offset 0 from the address in R2. - Forgetting to set the loop counter 循环计数器. The loop then runs from whatever R1 held.
- Using $i$ as the byte offset. Scale it by the element size: $4i$ for 4-byte words.
Recap
- Six instructions; memory only through load and store, at base + offset:
| Instruction | Does |
|---|---|
add |
register + register |
addi |
register + a constant |
lw R1, 8(R2) |
load from R2 + 8 |
sw R1, 8(R2) |
store to R2 + 8 |
beq, bne |
branch if equal, if not equal |
- RISC 精简指令集 trades more instructions for simpler, faster ones that pipeline well.
- A while loop is decrement and branch;
a[i]is base + $i$ × size.
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.