汇编语言新手技术问询:不同架构汇编仅语法有差异吗?
Hey there! Awesome question—since you're new to assembly, let's break this down step by step. First, let's recap the examples you shared in clean, formatted blocks, then dive into your technical inquiries.
ARM Assembly
.text .global _start _start: mov r0, #1 ldr r1, =message ldr r2, =len mov r7, #4 swi 0 mov r7, #1 swi 0 .data message: .asciz "hello world\n" len = .-message
NASM (Intel Syntax) Assembly
section .data ; 定义内存的段 msg db "Hello, World!", 10 ; 将字符串定义为'msg',10为'\n'字符 len equ $ - msg ; 将字符串长度赋值给'len' section .text ; 存放代码的段 global _start ; 为链接器(ld)声明全局标签 _start: mov rax, 1 ; 系统调用ID(sys_write) mov rdi, 1 ; 文件描述符(标准输出) mov rsi, msg ; 待写入字符串的地址 mov rdx, len ; 字符串长度 syscall ; 调用内核 mov rax, 60 ; 系统调用ID(sys_exit) mov rdi, 0 ; 错误码0 syscall ; 调用内核
GAS (AT&T Syntax) Assembly
.data hello: .string "Hello world!\n" .text .globl _start _start: movl $4, %eax # write(1, hello, strlen(hello)) movl $1, %ebx movl $hello, %ecx movl $13, %edx int $0x80 movl $1, %eax # exit(0) movl $0, %ebx int $0x80
MIPS Assembly
.data msg: .asciiz "Hello World" .extern foobar 4 .text .globl main main: li $v0, 4 # 系统调用4(print_str) la $a0, msg # 参数:字符串 syscall # 打印字符串 lw $t1, foobar jr $ra # 返回调用者
GAS (AT&T Syntax)
movq $2, %r8 # %r8 = 2 movq $3, %r9 # %r9 = 3 movq $5, %r10 # %r10 = 5 imulq %r9, %r10 # %r10 = 3 * 5 = 15 addq %r8, %r10 # %r10 = 2 + 15 = 17
NASM (Intel Syntax)
mov r8, 2 ; r8 = 2 mov r9, 3 ; r9 = 3 mov r10, 5 ; r10 = 5 mul r9, r10 ; r10 = 3 * 5 = 15 add r8, r10 ; r10 = 2 + ( 15 ) = 17
1. Do different assembly languages have different code writing orders?
Great question—yes, but it depends on whether we're talking about different syntax flavors for the same architecture (like GAS vs NASM for x86) or completely different CPU architectures (ARM vs x86 vs MIPS):
For same-architecture syntax variants: The logical flow of code (e.g., "prepare syscall arguments → trigger syscall → exit") is identical because they target the same CPU. The only order difference is operand order: AT&T uses
source, destination(e.g.,addq %r8, %r10means "add r8 to r10"), while Intel usesdestination, source(your example'sadd r8, r10achieves the same result but writes the instruction in reverse operand order).For different architectures: The code order and structure can be drastically different. Each CPU has unique rules for things like:
- How to invoke system calls (ARM uses
swi, x86 usessyscall/int 0x80, MIPS usessyscallwith distinct register setups) - Entry point conventions (ARM/x86 use
_startas the linker entry; MIPS often uses amainfunction that returns to the runtime viajr $ra) - Argument passing (registers used for syscalls/function calls vary wildly across ARM, x86, and MIPS)
- How to invoke system calls (ARM uses
2. Are differences limited to syntax alone?
Absolutely not—syntax is the most visible difference, but there are deep, architecture-specific distinctions that go way beyond how you write instructions:
- Unique Instruction Sets: Each CPU family has exclusive instructions. For example, ARM uses a load/store architecture (you can only access memory via dedicated load/store instructions), while x86 allows memory operands in most arithmetic instructions. MIPS has delayed branching (the instruction after a branch executes before the branch takes effect)—a feature you won't find in ARM or x86.
- Register Models: The number of registers and their roles differ. ARM has 16 general-purpose registers (32-bit mode), x86-64 has 16, MIPS has 32. Stack pointers are
r13(ARM),rsp(x86),$sp(MIPS)—and syscall argument registers are completely unrelated across architectures. - Calling Conventions: Rules for passing function arguments, returning values, and preserving registers vary widely. x86-64 uses the System V AMD64 convention (args in
rdi,rsi,rdx, etc.), ARM uses APCS (args inr0-r3), and MIPS has its own convention (args in$a0-$a3). - Memory Endianness: Some architectures default to little-endian (x86, ARM), others can be big-endian or switchable (MIPS). This affects how multi-byte data is stored and accessed.
- Syscall Mechanisms: As shown in the Hello World examples, each architecture has a unique way to call kernel functions—different instructions, different registers for syscall numbers and arguments.
The exception here is syntax variants for the same architecture (like GAS vs NASM for x86): their differences are almost entirely syntactic. They use the same instructions, registers, and follow the same CPU rules—just with different ways to write the same operations.
内容的提问来源于stack exchange,提问作者Lasan Nishshanka

