Introduction to the Little Man Computer (LMC) Instruction Set | 小矮人计算机 (LMC) 指令集入门

📚 Introduction to the Little Man Computer (LMC) Instruction Set | 小矮人计算机 (LMC) 指令集入门

The Little Man Computer (LMC) is a simplified conceptual model of a computer, used extensively in Edexcel A-Level Computer Science to teach the fundamentals of how a CPU works at the machine-code level. It features a tiny but complete instruction set that includes operations such as loading, storing, adding, subtracting, branching, and input/output. Understanding the LMC instruction set is essential for grasping assembly language programming, the fetch-decode-execute cycle, and addressing modes. This article provides a comprehensive breakdown of every LMC operation, including its binary mindset even though the LMC uses decimal opcodes, and shows how to write, trace, and debug simple programs.

小矮人计算机 (LMC) 是一个简化的计算机概念模型,在 Edexcel A-Level 计算机科学课程中被广泛用来讲解 CPU 在机器码层面的工作原理。它拥有一套精简但完整的指令集,包括加载、存储、加法、减法、分支以及输入/输出等操作。理解 LMC 指令集是掌握汇编语言编程、取指-译码-执行循环和寻址方式的关键。本文全面拆解每一条 LMC 操作,涵盖其背后与二进制机制相通的十进制思维,并演示如何编写、跟踪和调试简单的程序。

1. What Is the Little Man Computer? | 什么是小矮人计算机?

The LMC is an imaginary computer with a very small memory (typically 100 mailboxes, addressed 00 to 99), a calculator (accumulator), an instruction counter, an input basket, and an output basket. It works by repeatedly fetching a three-digit instruction from a mailbox, decoding the opcode (the first digit or in some cases the first two digits), and executing it. The model was designed by Dr. Stuart Madnick in 1965 to illustrate the basic operations of a von Neumann architecture. In the Edexcel specification, you are expected to understand the LMC instruction set using decimal numbers, recognise the difference between instructions and data, and write simple LMC assembly programs.

LMC 是一个虚拟的计算机,拥有一块极小的内存(通常为 100 个邮箱,地址从 00 到 99)、一个计算器(累加器)、一个指令计数器、一个输入篮和一个输出篮。它通过反复从邮箱中取出三位十进制指令,对操作码(第一位或前两位)进行译码,然后执行的方式来工作。该模型由 Stuart Madnick 博士于 1965 年设计,用于阐释冯·诺依曼体系结构的基本操作。在 Edexcel 考试说明中,要求考生能够理解使用十进制数的 LMC 指令集,区分指令和数据,并编写简单的 LMC 汇编程序。


2. LMC Memory and Registers | LMC 的内存与寄存器

The LMC has 100 memory locations numbered 00 to 99, referred to as mailboxes. Each mailbox can store a three-digit decimal number (from 000 to 999). In addition, the CPU contains a few special-purpose registers: the Accumulator (the ‘calculator’), the Program Counter (PC, sometimes called the instruction counter) which holds the address of the next instruction to fetch, the Memory Address Register (MAR) and the Memory Data Register (MDR), although in exam questions you mainly work with the PC and the accumulator. During execution, an instruction consists of an opcode and, for most instructions, a two-digit operand that represents a mailbox address.

LMC 拥有 100 个编号从 00 到 99 的内存单元,被称为邮箱。每个邮箱可存储一个三位十进制数字(从 000 到 999)。此外,CPU 内部包含几个专用寄存器:累加器(“计算器”)、程序计数器(PC,有时称为指令计数器,存放下一条要取出的指令地址)、内存地址寄存器(MAR)和内存数据寄存器(MDR),不过在考试题中主要涉及 PC 和累加器。在执行过程中,一条指令由操作码和(对大多数指令来说)一个表示邮箱地址的两位操作数构成。


3. Overview of the LMC Instruction Set | LMC 指令集概览

The LMC has 10 mnemonics, each mapped to a numeric opcode. Since the machine uses a three-digit instruction format, the first one or two digits define the operation, and the remaining digits (if any) provide the address. The complete set is: HLT (000), ADD (1xx), SUB (2xx), STA (3xx), LDA (5xx), BRA (6xx), BRZ (7xx), BRP (8xx), INP (901), OUT (902). Here ‘xx’ stands for a two-digit mailbox address. Note that INP and OUT do not need an address, so the opcode itself occupies all three digits. This instruction set is intentionally minimal, yet it can implement loops, decisions, and arithmetic.

LMC 共有 10 种助记符,分别对应一个数字操作码。由于该机器采用三位指令格式,第一位或前两位数字定义操作,其余数字(如果有)提供地址。完整的指令集为:HLT(000)、ADD(1xx)、SUB(2xx)、STA(3xx)、LDA(5xx)、BRA(6xx)、BRZ(7xx)、BRP(8xx)、INP(901)、OUT(902)。这里“xx”代表两位邮箱地址。注意 INP 和 OUT 不需要地址,因此操作码本身占据全部三位。这个指令集故意设计得很精简,但仍能实现循环、条件判断和算术运算。


4. Data Movement: LDA and STA | 数据传送:LDA 与 STA

LDA xx (opcode 5xx) loads the content of the mailbox at address xx into the accumulator. The mailbox value remains unchanged. STA xx (opcode 3xx) stores the current content of the accumulator into mailbox xx, overwriting whatever was there previously. These two instructions are the only way to move data between the CPU and memory. For example, LDA 15 copies the value from mailbox 15 into the accumulator; STA 20 writes the accumulator’s value into mailbox 20. Any data that needs to be processed must first be loaded, and results must be stored back using STA if they are to be kept.

LDA xx(操作码 5xx)将地址 xx 处的邮箱内容加载到累加器中。邮箱中的值保持不变。STA xx(操作码 3xx)将累加器当前内容存储到邮箱 xx 中,覆盖其原有内容。这两条指令是在 CPU 与内存之间传送数据的唯一途径。例如,LDA 15 将邮箱 15 中的值复制到累加器;STA 20 则将累加器的值写入邮箱 20。任何需要处理的数据必须先被加载,而处理结果若要保留,则需用 STA 存回内存。


5. Arithmetic: ADD and SUB | 算术运算:ADD 与 SUB

ADD xx (1xx) adds the value in mailbox xx to the accumulator. The result replaces the original accumulator value. SUB xx (2xx) subtracts the value in mailbox xx from the accumulator, again leaving the result in the accumulator. There is no multiply or divide; you must implement them using repeated addition or subtraction. These instructions work only with the accumulator; you cannot add two arbitrary memory locations directly. An LMC program must typically load one operand into the accumulator first, then add or subtract the second operand from a given mailbox. Note that LMC uses ordinary decimal arithmetic, but negative results are represented in the same way: if a subtraction leads to a negative number, the accumulator will hold that negative value (e.g. 5 – 8 = -3, stored as 999 – 3 + 1 if the machine were to use signed representations, but for simplicity in basic exam examples you usually avoid negative results unless exploring branching conditions).

ADD xx(1xx)将邮箱 xx 中的值加到累加器中。加法结果将取代原来的累加器值。SUB xx(2xx)从累加器中减去邮箱 xx 中的值,结果同样留在累加器中。这里没有乘法或除法指令;必须用重复加法或减法来实现。这些指令只能与累加器配合使用;不能直接将两个内存位置的内容相加。LMC 程序通常需要先把一个操作数加载到累加器中,然后再与指定邮箱中的第二个操作数进行加法或减法。请注意,LMC 使用普通的十进制算术,但减法结果若为负,累加器同样会保存该负值(例如 5 – 8 = -3,在考虑带符号表示时可能以补码形式存储,但在基础层次的考试实例中通常避免负结果,除非专门探讨分支条件)。


6. Branching: BRA, BRZ, BRP | 分支指令:BRA、BRZ、BRP

Branch instructions alter the flow of execution by changing the program counter. BRA xx (6xx) always jumps to the instruction at address xx (an unconditional branch). BRZ xx (7xx) jumps to address xx only if the accumulator contains zero; otherwise the program continues with the next instruction. BRP xx (8xx) jumps to address xx only if the accumulator contains zero or a positive value (i.e. the result is not negative). There is no conditional branch for negative values alone, but you can combine SUB with BRZ and BRP to check for negative by testing whether the number is not zero and not positive. These instructions are essential for implementing loops and decisions. A typical counting loop uses a counter stored in memory, decremented by subtracting 1, then a BRZ to exit when the counter reaches zero.

分支指令通过改变程序计数器来改变执行流程。BRA xx(6xx)总是跳转到地址 xx 处的指令(无条件分支)。BRZ xx(7xx)仅当累加器中的值为零时才跳转到地址 xx;否则程序继续执行下一条指令。BRP xx(8xx)仅当累加器中的值为零或正数时跳转到地址 xx(即结果不为负)。没有单独针对负值的条件分支指令,但可以结合 SUB、BRZ 和 BRP,通过测试数值是否既不为零也不是正数来检查负数。这些指令对于实现循环和条件判断至关重要。一个典型的计数循环会在内存中存储一个计数器,每次减 1,然后用 BRZ 在计数器减到零时退出循环。


7. Input and Output: INP and OUT | 输入与输出:INP 与 OUT

The LMC interacts with the outside world via a single numeric input and a single numeric output. INP (901) reads a decimal number from the input tray and places it in the accumulator. The user (or a test harness) must supply one number whenever an INP instruction is executed. OUT (902) copies the content of the accumulator to the output tray for display. For programs that need multiple inputs, you must use several INP instructions, usually storing the values into memory with STA between inputs. Likewise, to show multiple outputs you use OUT after loading the desired value. It is vital to ensure that the number of inputs provided matches the number of INP calls; otherwise the program may stall.

LMC 通过单一的数字输入和单一的数字输出与外界交互。INP(901)从输入托盘中读取一个十进制数并放入累加器。用户(或测试程序)必须在每次执行 INP 指令时提供一个数字。OUT(902)将累加器的内容拷贝到输出托盘以便显示。对于需要多个输入的程序,必须使用多条 INP 指令,通常会在两次输入之间用 STA 将值保存到内存。同样,若要显示多个输出,可以在将所需值加载到累加器后再用 OUT。关键是要确保提供的输入个数与 INP 的调用次数匹配,否则程序可能停滞。


8. Halting the Program: HLT and DAT | 程序终止:HLT 与 DAT

HLT (000) tells the computer to stop execution. It is essential to place HLT at the end of the code segment so that the CPU does not attempt to execute data as instructions. DAT is not a machine instruction but an assembler directive; it reserves a mailbox and optionally sets an initial data value. For example, NUM DAT 5 creates a label NUM pointing to a mailbox that contains the value 5. In examination questions, you may need to allocate storage using DAT for constants, variables, and counters. Uninitialised mailboxes are assumed to contain 000 unless specified. The HLT instruction must be used carefully; forgetting it is a common mistake that causes the program to wander into data areas and produce unpredictable results.

HLT(000)告诉计算机停止执行。必须在代码段的末尾放置 HLT,以免 CPU 试图把数据当作指令来执行。DAT 不是机器指令,而是一个汇编器伪指令;它预留一个邮箱并可设置初始数据值。例如,NUM DAT 5 创建一个标签 NUM,指向一个存有数值 5 的邮箱。在考试问题中,可能需要使用 DAT 来为常量、变量和计数器分配存储空间。未经初始化的邮箱默认视为包含 000。HLT 指令必须谨慎使用;忘记放置 HLT 是一个常见错误,会导致程序跑到数据区域并产生不可预测的结果。


9. Writing a Simple LMC Program: Adding Two Numbers | 编写简单的 LMC 程序:两数相加

Let us write a program that takes two numbers as input, adds them, and outputs the sum. The LMC assembly code, stored in a sequence of mailboxes, would look like this:

我们来编写一个程序,要求输入两个数字,将它们相加并输出和数。LMC 汇编代码存储在一系列邮箱中,如下所示:

INP          ;input first number
STA FIRST    ;store in FIRST
INP          ;input second number
ADD FIRST    ;add the first number to accumulator
OUT          ;output the result
HLT          ;stop
FIRST DAT    ;reserve a mailbox for the number

The corresponding machine code (opcodes) assuming FIRST is at address 06: mailbox 00: 901; 01: 306; 02: 901; 03: 106; 04: 902; 05: 000; 06: 000. The program loads the first value from the user, stores it, gets the second value, adds the stored first value, and outputs the total. This example demonstrates the fetch-decode-execute cycle in action; the PC advances through each mailbox, the CU decodes the opcode, and the ALU performs addition when ADD is executed.

对应的机器码(假设 FIRST 位于地址 06):邮箱 00: 901;01: 306;02: 901;03: 106;04: 902;05: 000;06: 000。程序从用户处读入第一个值,将其存储,再读入第二个值,加上存储的第一个值,然后输出总和。这个例子生动展示了取指-译码-执行循环的运作:PC 依次经过每个邮箱,CU 译码操作码,ALU 在执行 ADD 时完成加法。


10. Implementing a Loop: Multiplication via Repeated Addition | 实现循环:用重复加法做乘法

Since the LMC lacks a multiplication instruction, you must use a loop. To multiply two numbers A and B, you can add A to the accumulator B times. One approach: input A, store it, input B, and use B as a counter. The loop checks whether the counter has reached zero; if not, it adds A, decrements the counter, and branches back. Below is a complete LMC program that multiplies two positive integers:

由于 LMC 没有乘法指令,必须使用循环。要计算 A 与 B 的乘积,可以将 A 累加 B 次。一种做法是:输入 A 并存储,输入 B 作为计数器。循环检查计数器是否已减到零;若不是,则加上 A,计数器减 1,然后跳回循环。下面是一个完整的 LMC 程序,用于计算两个正整数的乘积:

INP
STA A
INP
STA COUNT
LOOP LDA COUNT
BRZ END      ;if zero, exit
SUB ONE
STA COUNT
LDA RESULT
ADD A
STA RESULT
BRA LOOP
END LDA RESULT
OUT
HLT
A DAT
COUNT DAT
RESULT DAT 0
ONE DAT 1

Note that this program uses multiple DAT locations to store the multiplicand (A), the counter (COUNT), the accumulating result (RESULT), and a constant 1 for decrementing. The BRZ ensures the loop exits when COUNT reaches zero. Tracing such programs manually is an important exam skill; you must keep track of the accumulator and the contents of each mailbox as each instruction executes.

注意,该程序使用多个 DAT 位置来存储乘数(A)、计数器(COUNT)、累加的结果(RESULT)以及用于减一的常数 1。BRZ 确保当 COUNT 减到零时退出循环。手动跟踪此类程序是一项重要的考试技能;在执行每条指令时,必须跟踪累加器和每个邮箱中的内容。


11. Addressing Modes in LMC | LMC 中的寻址方式

The LMC instruction set primarily uses direct addressing, where the operand part of an instruction (the xx in 1xx) directly specifies the address of the mailbox containing the data. However, the concept of immediate addressing does not exist as a separate mode because all data must come from a mailbox. To use a constant, you must store it in a mailbox using DAT and then reference that address. For example, to load the number 5 you cannot say LDA #5; you must place 5 in a mailbox (e.g. FIVE DAT 5) and then use LDA FIVE. Indirect addressing is not supported. Understanding these limitations helps you appreciate why LMC programs need many data statements and why the address bus and data bus operate the way they do.

LMC 指令集主要使用直接寻址,即指令的操作数部分(如 1xx 中的 xx)直接指定存放数据的邮箱地址。然而,立即寻址并没有作为一种独立的寻址模式存在,因为所有数据都必须来自某个邮箱。要使用常数,必须先用 DAT 将其存入邮箱,然后引用该地址。例如,要加载数字 5,不能写成 LDA #5;必须将 5 存放到一个邮箱(如 FIVE DAT 5),然后再用 LDA FIVE。间接寻址也不支持。理解这些限制有助于领会为什么 LMC 程序需要许多数据声明,以及地址总线和数据总线为何如此工作。


12. Exam Tips and Common Pitfalls | 考试技巧与常见陷阱

When answering LMC exam questions, always distinguish between assembly mnemonics and machine code. Write clear labels and allocate DAT storage after the HLT so that execution does not accidentally start executing data. If the question asks you to trace a program, draw a table with columns for the PC, the instruction fetched, the accumulator, and any relevant memory locations. Be careful with subtraction: SUB xx subtracts the content of mailbox xx from the accumulator, not the other way around. Also, remember that the accumulator is implicitly used; you do not need to specify it as a destination. Finally, check that the number of inputs and outputs matches the program logic, and always end a program with HLT. These strategies will help you secure full marks on LMC-related questions.

在回答 LMC 考试题时,始终要区分汇编助记符与机器码。清晰地书写标签,并将 DAT 存储区安排在 HLT 之后,以免执行流意外进入数据区。如果题目要求跟踪程序,请画一个表格,列标题为 PC、取出的指令、累加器以及任何相关的内存单元。在减法上要小心:SUB xx 是从累加器中减去邮箱 xx 的内容,而不是反过来。还要记住,累加器是默认使用的,无需显式指定为目标。最后,检查输入输出的次数是否与程序逻辑一致,且程序总以 HLT 结束。这些策略将助你在 LMC 相关题目上获得满分。


Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading