begin assembly language

本文介绍了Linux平台上的汇编语言开发流程,包括使用GAS和NASM两种汇编器进行汇编,以及如何利用ld链接器将目标代码链接成可执行文件。

主要参考这篇文章:Linux 汇编语言开发指南

1.汇编器

汇编器(assembler)的作用是将用汇编语言编写的源程序转换成二进制形式的目标代码。Linux 平台的标准汇编器是 GAS,它是 GCC 所依赖的后台汇编工具,通常包含在 binutils 软件包中。GAS 使用标准的 AT&T 汇编语法,可以用来汇编用 AT&T 格式编写的程序:

[xiaowp@gary code]$ as -o hello.o hello.s

Linux 平台上另一个经常用到的汇编器是 NASM,它提供了很好的宏指令功能,并能够支持相当多的目标代码格式,包括 bin、a.out、coff、elf、rdf 等。NASM 采用的是人工编写的语法分析器,因而执行速度要比 GAS 快很多,更重要的是它使用的是 Intel 汇编语法,可以用来编译用 Intel 语法格式编写的汇编程序:

[xiaowp@gary code]$ nasm -f elf hello.asm

2.链接器

由汇编器产生的目标代码是不能直接在计算机上运行的,它必须经过链接器的处理才能生成可执行代码。链接器通常用来将多个目标代码连接成一个可执行代码,这样可以先将整个程序分成几个模块来单独开发,然后才将它们组合(链接)成一个应用程序。 Linux 使用 ld 作为标准的链接程序,它同样也包含在 binutils 软件包中。汇编程序在成功通过 GAS 或 NASM 的编译并生成目标代码后,就可以使用 ld 将其链接成可执行程序了:

[xiaowp@gary code]$ ld -s -o hello hello.o


在64位机器上,上面的链接命令会报错,用下面这个:
ld -m elf_i386 -s -o hello hello.o


Theory of Computer Science Laboratory 2 / Project_part2 2025 Creating a programs for Input-Output devices of the W-Machine p.1 Create the program in assembly language for the W-Machine that: Task 1 (1 point): Read/ take a value from a user (from an external input device). This value is a single-digit number. After that, output to the external device the first letter (in the Latin alphabet) of your name as many times as the value read (value taken from the user). Task 2 (2 points): Change all lowercase (small) letters into their capital equivalents (uppercase). Dot (full-stop) ends the sentence. For example: My name is Bob!!!. -> MY NAME IS BOB. So, you have to take the typed characters from the input device and write them to the output device, but if it is a lowercase letter, you have to convert it to uppercase. You must understand how the prepared program code works. You must present to the teacher how the program works on the computer (with the use of a simulator of the W-Machine). A grade will be determined based on this presentation. Additionally, a report will be required, including the solution created. If you do not present a working program on the computer, the grade will be determined based on the report, but then you will be able to receive a maximum of 2 points (instead of 3 points). The report must be in the form of a single text file ( .txt). The file name must contain the student ID number. The report must begin with the student's information (name in English and Chinese, student's number) laboratory number, report submission date, followed by the assignment task and the solution. The program code should be properly formatted – only one assembly instruction per line. The grade will be reduced for program code that is not formatted correctly. It is not possible to increase the grade/points received for reports that have already been graded. Additional information: Warning: After closing the I/O console, it cannot be reopened - the entire W-Machine simulator must be restarted. List of instructions with mnemonic and code IN 1 sending a character from an external device to the accumulator OUT 2 sending a character from the accumulator to an external device STOP the end of the program ADD add SUB subtract LOAD load STORE store JMP jump JN jump if (ACC) < 0 JZ jump if (ACC) = 0 RPA memory reservation, RST predefined value
最新发布
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值