📚 Opcodes and Addressing Modes in Little Man Computer (LMC) | 小矮人计算机中的操作码与寻址模式
The Little Man Computer (LMC) is a simplified instructional model used extensively in the Edexcel A-Level Computer Science curriculum to illustrate the fundamental operations of a CPU. It helps students understand how opcodes, operands, and addressing modes work together to execute programs. This article explores the LMC instruction set, the direct addressing mode it employs, and how combined operations are used to solve problems.
小矮人计算机(LMC)是一个简化的教学模型,在 Edexcel A-Level 计算机科学课程中被广泛用于说明 CPU 的基本操作。它帮助学生理解操作码、操作数和寻址模式如何协同工作来执行程序。本文探讨 LMC 指令集、它所使用的直接寻址模式,以及如何组合操作来解决问题。
1. Overview of the Little Man Computer | 小矮人计算机概述
The LMC consists of a “little man” placed inside a room with 100 mailboxes numbered 00 to 99, an input and output basket, a calculator that serves as the accumulator, and a program counter. The little man repeatedly performs the fetch-decode-execute cycle. Each mailbox holds a three-digit number that can represent either an instruction or a piece of data. This model mimics the von Neumann architecture in a simple, visual way.
LMC 由一个小房间里的“小矮人”、100 个编号为 00 至 99 的信箱、一个输入篮子和一个输出篮子、一个充当累加器的计算器以及一个程序计数器组成。小矮人不断执行取指-译码-执行周期。每个信箱存放一个三位数,它既可以代表一条指令,也可以代表一个数据。该模型以简单直观的方式模拟了冯·诺依曼体系结构。
2. LMC Architecture and Components | LMC 体系结构与组件
Key components of the LMC include the Program Counter (PC) that holds the address of the next instruction, the Accumulator (ACC) for storing intermediate results, and 100 memory locations (mailboxes). Input and output operations are handled using the input basket and the output basket. Although the LMC does not explicitly contain Memory Address Register (MAR) or Memory Data Register (MDR), the little man effectively fetches an instruction by reading the memory location pointed to by the PC.
LMC 的关键组件包括存放着下一条指令地址的程序计数器(PC)、用于存储中间结果的累加器(ACC),以及 100 个内存位置(信箱)。输入和输出操作通过输入篮子和输出篮子进行处理。虽然 LMC 没有明确包含内存地址寄存器(MAR)或内存数据寄存器(MDR),但小矮人实际上通过读取 PC 指向的内存位置来取出指令。
3. Instruction Format and Opcodes | 指令格式与操作码
Instruction = Opcode (first digit) + Address (last two digits)
Every LMC instruction is a three-digit decimal number. The first digit acts as the opcode and determines the operation to be performed. The remaining two digits form an operand that almost always represents a memory address. This fixed-length format makes decoding straightforward: the little man splits the instruction digit by digit.
每一条 LMC 指令都是一个三位十进制数。第一位数字作为操作码,决定要执行的操作。后两位数字构成操作数,几乎总是代表一个内存地址。这种定长格式使得译码十分简单:小矮人按位拆分指令即可。
The following table summarises all standard LMC instructions. Note that the opcode and address are concatenated; for example, ADD 15 is written as the machine code 115.
| Opcode (numeric) | Mnemonic | Description |
|---|---|---|
| 1xx | ADD | Add the contents of mailbox xx to the accumulator |
| 2xx | SUB | Subtract the contents of mailbox xx from the accumulator |
| 3xx | STA | Store the accumulator value into mailbox xx |
| 5xx | LDA | Load the accumulator with the contents of mailbox xx |
| 6xx | BRA | Branch (jump) unconditionally to mailbox xx |
| 7xx | BRZ | Branch to mailbox xx if the accumulator is zero |
| 8xx | BRP | Branch to mailbox xx if the accumulator is zero or positive |
| 901 | INP | Input a value from the user and store it in the accumulator |
| 902 | OUT | Output the value currently in the accumulator |
| 000 | HLT | Halt (stop) the program |
下表总结了所有 LMC 指令:操作码 1xx 将邮箱 xx 的内容与累加器相加;2xx 则从累加器减去邮箱 xx 的内容;3xx 将累加器值存入邮箱 xx;5xx 将邮箱 xx 的内容加载到累加器;6xx 是无条件跳转;7xx 在累加器为零时跳转;8xx 在累加器为零或正数时跳转;901 输入;902 输出;000 停机。理解这些操作码是编写 LMC 程序的基础。
4. Direct Addressing Mode | 直接寻址模式
LMC exclusively employs direct addressing. This means that the operand part of an instruction – the last two digits – directly specifies the memory mailbox containing the data. For instance, the instruction LDA 25 tells the little man to load the accumulator with whatever value is currently stored in mailbox 25, not the number 25 itself.
LMC 仅使用直接寻址。这意味着指令的操作数部分——后两位数字——直接指定了存放数据的内存信箱。例如,指令 LDA 25 告诉小矮人把邮箱 25 中当前存储的任何值加载到累加器,而不是数字 25 本身。
Because there is no immediate addressing mode in the standard LMC, constants must be placed in a mailbox before they can be used. This reinforces the concept that all data must reside in memory before the processor can operate on it.
由于标准 LMC 中没有立即寻址模式,常量必须先存放在某个信箱中才能被使用。这强化了所有数据必须在处理器操作之前驻留在内存中的概念。
5. Data Transfer: LDA and STA | 数据传送:LDA 和 STA
The LDA instruction loads the accumulator with the value stored at a specified memory address. For example, LDA 99 (machine code 599) copies the contents of mailbox 99 into the accumulator. The STA instruction does the reverse: STA 10 (310) writes the current accumulator value into mailbox 10, overwriting whatever was previously there.
LDA 指令将指定内存地址中存储的值加载到累加器。例如,LDA 99(机器码 599)将邮箱 99 的内容复制到累加器。STA 指令则做相反操作:STA 10(310)将当前累加器的值写入邮箱 10,覆盖之前存在的任何内容。
These two instructions are the backbone of data movement in any LMC program. Together they allow the little man to fetch data from memory and store results back, much like a real CPU’s LOAD and STORE operations.
这两条指令是任何 LMC 程序中数据移动的支柱。它们共同使小矮人能够从内存中取出数据并将结果存回,非常像真实 CPU 的加载和存储操作。
6. Arithmetic: ADD and SUB | 算术运算:ADD 和 SUB
The ADD instruction adds the value found in a specified mailbox to the accumulator. For instance, if the accumulator holds 20 and mailbox 35 contains 15, ADD 35 (135) results in the accumulator becoming 35. The SUB instruction subtracts the mailbox value from the accumulator. LMC handles results using decimal arithmetic; if a subtraction yields a negative number, the accumulator wraps around in a 0–999 range using ten’s complement representation, but exam questions typically avoid such complexity or specify that the accumulator holds a signed value.
ADD 指令将指定信箱中的值与累加器相加。例如,如果累加器持有 20,邮箱 35 包含 15,则 ADD 35(135)执行后累加器变为 35。SUB 指令从累加器中减去信箱的值。LMC 使用十进制算术处理结果;如果减法产生负数,累加器会利用十的补码表示在 0 至 999 范围内回绕,但考试题通常避免此类复杂性或明确说明累加器存放的是带符号值。
Arithmetic operations always affect the accumulator, which is the heart of the LMC’s calculator. Because the operand is a memory address, you cannot directly add a constant like 5 unless that 5 has been placed in a mailbox using a data directive beforehand.
算术运算始终影响累加器,累加器是 LMC 计算器的核心。由于操作数是内存地址,您不能直接加上一个常数 5,除非该 5 事先已经通过数据指示放入某个邮箱。
7. Branching: BRA, BRZ, BRP | 分支指令:BRA, BRZ, BRP
LMC provides three branching instructions to alter the flow of control. BRA xx (6xx) performs an unconditional jump to mailbox xx. BRZ xx (7xx) jumps only if the accumulator is exactly zero. BRP xx (8xx) jumps if the accumulator is zero or positive (i.e. non‑negative). These conditional branches allow loops and decision‑making.
LMC 提供了三条分支指令来改变控制流。BRA xx(6xx)无条件跳转到邮箱 xx。BRZ xx(7xx)仅在累加器恰好为零时才跳转。BRP xx(8xx)在累加器为零或正数(即非负)时跳转。这些条件分支使循环和决策成为可能。
Note that BRP covers both positive and zero, which differs from some assembly languages that have a separate instruction for “branch if positive”. When writing programs, students must therefore initialise accumulators carefully to ensure correct loop exits.
请注意,BRP 同时覆盖正数和零,这与某些具有单独“正跳”指令的汇编语言不同。因此,学生在编写程序时必须仔细初始化累加器,以确保正确的循环退出。
8. Input and Output: INP and OUT | 输入与输出:INP 与 OUT
Interaction with the outside world is achieved via INP (code 901) and OUT (code 902). When the little man encounters 901, it waits for the user to type a number, which is then placed into the accumulator. When it encounters 902, the content of the accumulator is displayed as output. These instructions have no address operand because they always target the input basket or output basket.
与外部世界的交互通过 INP(代码 901)和 OUT(代码 902)实现。当小矮人遇到 901 时,它会等待用户输入一个数字,该数字随后被放入累加器。当遇到 902 时,累加器的内容作为输出显示。这些指令没有地址操作数,因为它们始终针对输入篮子或输出篮子。
An LMC program typically begins with one or more INP instructions to obtain data and ends with OUT to display results. Numerous exam problems ask you to write a sequence that reads two numbers, adds them, and prints the sum – a classic use of these I/O commands.
一个 LMC 程序通常以一个或多个 INP 指令开始以获取数据,并以 OUT 指令结束以显示结果。大量考试题要求你编写一个读取两个数字、将它们相加并输出总和的序列——这是这些 I/O 命令的经典用法。
9. Halt: HLT | 停机:HLT
The HLT instruction, represented by machine code 000, tells the little man to stop. Without HLT at the end of a program, the fetch-decode-execute cycle would continue blindly into subsequent mailboxes, potentially misinterpreting data as instructions and causing unexpected results.
HLT 指令由机器码 000 表示,它告诉小矮人停止工作。如果程序末尾没有 HLT,取指-译码-执行周期就会盲目地继续进入后续的信箱,可能将数据误解释为指令,从而导致意外结果。
In LMC simulators the halt instruction also signals that the program has finished successfully, so the simulator can stop and display the final state of memory and the accumulator.
在 LMC 模拟器中,停机指令还表示程序已成功完成,因此模拟器可以停下来并显示内存和累加器的最终状态。
10. Fetch-Decode-Execute Cycle in LMC | LMC 中的取指-译码-执行周期
The little man’s operation follows a strict cycle: First, fetch: read the instruction from the mailbox whose address is in the PC, then increment the PC. Second, decode: split the three-digit number into an opcode (first digit) and an address (last two digits). Third, execute: carry out the action dictated by the opcode, using the address as needed. After execution, the cycle repeats unless HLT is encountered.
小矮人的操作遵循严格的循环:首先,取指:从 PC 中地址所指示的信箱读取指令,然后递增 PC。其次,译码:将三位数拆分为操作码(首位数字)和地址(后两位)。第三,执行:按照操作码规定的动作执行,必要时使用该地址。执行后,循环重复,除非遇到 HLT。
Understanding this cycle is essential for predicting how an LMC program will behave and for debugging incorrect sequences. It also directly mirrors the real CPU fetch-decode-execute cycle, making LMC an excellent conceptual bridge.
理解这一周期对于预测 LMC 程序的行为以及调试错误序列至关重要。它还直接反映了真实 CPU 的取指-译码-执行周期,使 LMC 成为一个极佳的概念桥梁。
11. Combining Operations: A Worked Example | 组合操作:一个实例程序
To see how opcodes and the direct addressing mode work together, consider a program that takes two user inputs, adds them, and outputs the result. The required sequence, with mnemonics and corresponding machine codes, is:
为了看清操作码和直接寻址模式如何协同工作,考虑一个程序:它获取两个用户输入,相加后输出结果。所需的序列以及助记符和对应的机器码如下:
| Mailbox | Mnemonic | Machine Code | Comment |
|---|---|---|---|
| 00 | INP | 901 | Input first number to ACC |
| 01 | Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信) CommentsMore posts |
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导