本学期学英文版《计算机系统概论》,因此要在Linux下安装LC3
以下是安装的教程:
Step 1:下载源文件
1.1 从官网下载
下载LC-3 Simulator Lab Manual(官方实验手册)
LC-3 Unix Simulator Manual (1555.0K)
下载LC-3 Simulator(仿真器)
LC-3 Unix Simulator (56.0K)
下载C to LC-3 Compiler(编译器)
C to LC-3 Compiler (369.0K)
1.2 从资源下载
Step2:解压文件
解压文件到你喜欢的目录,如我的是/home/ComputerSystem/LC3目录,解压后分别有lc3tools和lcc-1.3两个文件夹
因此在LC3输入ls的话,可以看到有lc3tools和lcc-1.3两个目录
Step3:安装组件
在整个过程,我们将需要用到gcc、flex、wish这三个组件,而Ubuntu通常自带gcc和wish,因此只需要安装flex
sudo apt-get install flex
安装即可
Step4:编译及安装
4.1 lc3工具箱
首先编译lc3tools文件,切换到lc3tools目录下,输入
./configure
成功会显示
Configuring for Linux...
Installation directory is /home/user/ComputerSystem/LC3/lc3tools
然后输入
gedit Makefile
找到“OS_SIM_LIBS = -lcurses”,把其中的“-lcurses”删掉后保存
修改后:OS_SIM_LIBS =
保存后退出,输入
make
完成后,输入
./lc3sim
即可打开模拟器,这也意味着安装lc3tools成功。
4.2 lcc编译器
接下来编译lcc-1.3文件,切换到lcc目录下,输入
sudo sh ./configure
成功会显示
Configuring for Linux...
Installation directory is /home/user/ComputerSystem/LC3/lcc-1.3/install
然后输入
make 和 make install 进行安装
完成后,输入
./install/lcc
会显示lcc的使用说明,证明安装lcc成功。
Step5:配置环境
为了在任何目录都可以使用lc3的工具及lcc,我们需要添加LC3到工作环境
5.1 在终端设置环境变量
输入
export
找到PATH项,可以查看当前的环境变量
输入 export PATH=$PATH:~/ComputerSystem/LC3/lc3tools:~/ComputerSystem/LC3/lcc
就会把需要添加的路径(~/ComputerSystem/LC3/lc3tools:~/ComputerSystem/LC3/lcc)添加到之前所有设置的路径($PATH)
但是换个终端,或者重启之后,环境变量就失效了,因为用这种方法配置的只能临时使用,因此介绍第二种方法
5.2 修改/etc/profile文件
用文本编辑器打开/etc/profile文件,如
sudo gedit /etc/profile
在文本开头添加
export PATH=$PATH:~/LC3/lc3tools:~/LC3/lcc-1.3
保存退出后,输入
source /etc/profile
重启使环境变量生效。
成功之后,在任何地方都可以使用lc3sim、lc3convert、lc3sim-tk、lcc等工具。
Step6:测试
在/home/Test目录中,新建test.c文件,Hello world代码如下
int main(int argc, char const *argv[])
{
printf("Hello world!\n");
return 0;
}
保存后退出
输入
lcc test.c
发现生成了a.asm (汇编文件)
但是会提示
lcc: /home/user/ComputerSystem/LC3/lcc-1.3/install/lc3as: No such file or directory
查看ReadMe,以下是具体的工作过程
(1) compile the .c files into a set of pseudo-assembly .lcc files,
(2) use the lc3pp (lc3 post processor) to link and massage the .lcc files and library files into a single .asm file, and
(3) use Steve Lumetta's LC-3 assembler to assemble the .asm into a .obj file.
因此我们知道,每次lcc编译c文件之后,都会接着使用lc3pp及lc3as
于是我们去lc3tools中把lc3as复制到lcc-1.3/install中
在lc3tools目录中输入
cp ./lc3as ../lcc-1.3/install
再次输入lcc test.c,显示
cpp: test.c:5 No newline at end of file(因为第一次的时候没有lc3as,但是已经有a.asm)
STARTING PASS 1
0 errors found in first pass.
STARTING PASS 2
0 errors found in second pass.
这次再看目录,拥有a.asm,a.obj,a.sym三个文件,输入
lc3sim a.obj 或 lc3sim-tk a.obj
就可以进行仿真调试
以上就是LC3的安装及配置教程。
参考网站
https://www.cnblogs.com/dave-developer/p/6142708.html LC3官网
https://www.cnblogs.com/lidabo/p/4344184.html Linux配置环境变量
https://download.csdn.net/download/sturobotic/11074378 上传的LC3源文件