ARM 学习笔记之17:Uppercase or lowercase when writing assembly language source code

博客介绍了ARM指令编写规范,指令助记符、伪指令、指令和符号寄存器名(除A32或T32指令中的a1 - a4和v1 - v8)需全大写或全小写,标签和注释大小写不限。还提及许多核心寄存器有同义词,除特定寄存器外,寄存器名可全大写或全小写。

You must write instruction mnemonics, pseudo-instructions, directives, and symbolic register names
(except a1-a4 and v1-v8 in A32 or T32 instructions) in either all uppercase or all lowercase. You must
not use mixed case. Labels and comments can be in uppercase, lowercase, or mixed case.

                AREA     A32ex,     CODE,     READONLY
                                                                  ; Name this block of code A32ex
                ENTRY                                     ; Mark first instruction to execute
start
               MOV   r0,   #10                         ; Set up parameters
               MOV   r1,   #3
               ADD   r0,   r0,   r1                      ; r0 = r0 + r1
stop
              MOV   r0,   #0x18                       ; angel_SWIreason_ReportException
              LDR   r1,   =0x20026                 ; ADP_Stopped_ApplicationExit
              SVC   #0x123456                     ; AArch32 semihosting (formerly SWI)
              END                                            ; Mark end of file
 

--------------------------------------------------------------------------------------------------------------------------------------------------

Many of the core register names have synonyms. The following table shows the predeclared core registers:

Register namesMeaning
r0-r15 and R0-R15General purpose registers.
a1-a4Argument, result or scratch registers. These are synonyms for R0 to R3.
v1-v8Variable registers. These are synonyms for R4 to R11.
SBStatic base register. This is a synonym for R9.
IPIntra-procedure call scratch register. This is a synonym for R12.
SPStack pointer. This is a synonym for R13.
LRLink register. This is a synonym for R14.
PCProgram counter. This is a synonym for R15.

                                              Table 3-2 Predeclared core registers in AArch32 state


With the exception of a1-a4 and v1-v8, you can write the register names either in all upper case or all lower case.

file: lowercase_to_uppercase.s:1: error: parser: instruction expected file: lowercase_to_uppercase.s:2: error: parser: instruction expected file: lowercase_to_uppercase.s:3: error: parser: instruction expected file: lowercase_to_uppercase.s:4: error: parser: instruction expected file: lowercase_to_uppercase.s:6: error: parser: instruction expected file: lowercase_to_uppercase.s:7: error: parser: instruction expected file: lowercase_to_uppercase.s:8: error: parser: instruction expected file: lowercase_to_uppercase.s:11: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:12: error: parser: instruction expected file: lowercase_to_uppercase.s:13: error: expression syntax error file: lowercase_to_uppercase.s:15: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:16: error: parser: instruction expected file: lowercase_to_uppercase.s:18: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:19: error: parser: instruction expected file: lowercase_to_uppercase.s:21: error: label `cmpb' inconsistently redefined file: lowercase_to_uppercase.s:19: note: label `cmpb' originally defined here file: lowercase_to_uppercase.s:21: error: parser: instruction expected file: lowercase_to_uppercase.s:24: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:25: error: parser: instruction expected file: lowercase_to_uppercase.s:28: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:29: error: parser: instruction expected file: lowercase_to_uppercase.s:31: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:32: error: parser: instruction expected file: lowercase_to_uppercase.s:33: error: parser: instruction expected file: lowercase_to_uppercase.s:36: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:37: error: label `movl' inconsistently redefined file: lowercase_to_uppercase.s:32: note: label `movl' originally defined here file: lowercase_to_uppercase.s:37: error: parser: instruction expected file: lowercase_to_uppercase.s:39: error: label or instruction expected at start of line file: lowercase_to_uppercase.s:40: error: parser: instruction expected
12-11
你遇到的错误: ``` error: parser: instruction expected error: label or instruction expected at start of line error: expression syntax error ``` 说明 **汇编器无法识别你的代码语法**,最可能的原因是:你使用的是 **Intel 语法风格的代码** 或 **AT&T 语法写错了格式**,但用的是不支持该语法的汇编器(比如 `nasm`),或者反过来。 --- ### 🔍 根本原因分析 你贴出的原始汇编代码是 **GNU 汇编器 `gas` 使用的 AT&T 语法**,但看起来你可能在使用 **NASM(Netwide Assembler)** 来汇编这个文件。 > ❌ 错误命令示例: ```bash nasm -f elf64 lowercase_to_uppercase.s -o out.o ``` ⚠️ **NASM 不支持 `.section`, `.globl`, `%` 寄存器等 GNU 风格语法(AT&T)以外的某些特性,且对指令格式要求更严格。** --- ## ✅ 正确解决方案:使用正确的工具链处理 AT&T 语法 你需要使用 **GNU Assembler (`as`) + GCC** 来编译 AT&T 语法的 `.s` 文件。 ### ✅ 正确编译步骤(Linux 系统下) ```bash # 使用 GNU as 汇编(注意是 .s 文件) as --64 lowercase_to_uppercase.s -o lowercase_to_uppercase.o # 使用 gcc 链接(因为调用了 printf) gcc -m64 lowercase_to_uppercase.o -o lowercase_to_uppercase ``` ✅ 然后运行: ```bash ./lowercase_to_uppercase ``` 输出应为: ``` Uppercase: H ``` --- ### 🛠️ 如果你坚持使用 NASM(Intel 语法),请改用以下版本 下面是将原程序 **转换为 NASM Intel 语法** 的完整版本(适用于 `nasm`): ```asm ; file: uppercase_nasm.asm ; 使用 NASM 编译:nasm -f elf64 uppercase_nasm.asm -o out.o extern printf global main section .data char: db 'h' ; 输入字符 fmt_str: db "Uppercase: %c", 10, 0 ; 格式化字符串(含换行和 null 结尾) section .text main: push rbp mov rbp, rsp ; 加载字符到 al mov al, [char] ; 判断是否在 'a' ~ 'z' cmp al, 'a' jl .print cmp al, 'z' jg .print ; 转大写:减去 32 sub al, 32 .print: ; 调用 printf("Uppercase: %c\n", char) mov rdi, fmt_str ; 第一个参数:格式字符串 mov rsi, rax ; 第二个参数:传递 AL 扩展后的值(高位已清零?需注意) and rsi, 0xFF ; 确保只保留低8位 xor rax, rax ; 不使用 XMM 寄存器 call printf ; 返回 0 xor eax, eax pop rbp ret ``` #### 使用 NASM 编译: ```bash nasm -f elf64 uppercase_nasm.asm -o uppercase_nasm.o gcc uppercase_nasm.o -o uppercase_nasm ./uppercase_nasm ``` --- ### 💡 如何判断你在用哪个汇编器? | 工具 | 支持语法 | 常见扩展名 | |------|--------|----------| | `as` (GNU Assembler) | AT&T 语法(默认) | `.s` | | `nasm` | Intel 语法 | `.asm` | | `yasm` | 类似 NASM | `.asm` | --- ### ✅ 小结:解决你的错误 你的错误是因为 **把 GNU AT&T 语法的 `.s` 文件拿去用 NASM 汇编了**,而 NASM 不认识 `.section .data`、`%rax` 这类写法。 🔧 **修复方法任选其一:** #### ✅ 方法 1:继续使用 GNU 工具链(推荐用于 `.s` 文件) ```bash as --64 lowercase_to_uppercase.s -o obj.o gcc obj.o -o output ``` #### ✅ 方法 2:改用 NASM 并重写为 Intel 语法(如上所示) ```nasm nasm -f elf64 your_file.asm -o obj.o gcc obj.o -o output ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值