GAS aarch64语法获取.ascii字符串长度

问题描述

我正在尝试翻译此x86_64程序:

# hello_world.s
# GNU Assembly,x86_64 Linux

.global _start

.data

.equ SYS_EXIT,60
.equ SYS_WRITE,1
.equ STDOUT,1
.equ SUCCESS,0

MSG:
    .ascii "Hello world!\n"

.equ MSG_LEN,. - MSG

.text

_start:
    # write(STDOUT,MSG,MSG_LEN)
    mov $SYS_WRITE,%rax
    mov $STDOUT,%rdi
    mov $MSG,%rsi
    mov $MSG_LEN,%rdx
    syscall

    # exit(SUCCESS)
    mov $SYS_EXIT,%rax
    mov $SUCCESS,%rdi
    syscall

进入aarch64程序:

// hello_world.s
// GNU Assembly,aarch64 Linux

.data

.equ SYS_EXIT,93
.equ SYS_WRITE,64
.equ STDOUT,. - MSG

.text

.global _start

_start:
    // write(STDOUT,MSG_LEN)
    mov x8,#SYS_WRITE
    mov x0,#STDOUT
    adr x1,MSG
    mov x2,#MSG_LEN
    svc #0

    // exit(SUCCESS)
    mov x8,#SYS_EXIT
    mov x0,#SUCCESS
    svc #0

但是,当我尝试汇编上述程序时,出现此错误:

hello_world.s:27:13: error: expected compatible register or logical immediate
    mov x2,#MSG_LEN
            ^

我认为这有点像鲱鱼,因为如果我更改此行:

.equ MSG_LEN,. - MSG

对此:

.equ MSG_LEN,13

然后正常工作。但是,我对这种解决方案不满意,因为我不想对MSG_LEN进行硬编码,我希望汇编程序能够像x86_64版本那样在汇编时确定长度。您能帮我弄清楚如何在程序的MSG_LEN版本中设置aarch64而不用显式地硬编码一个值吗?

其他详细信息:我正在根据此Dockerfile构建的docker容器中编译和运行这些程序:

FROM ubuntu:20.04

RUN apt-get update
RUN apt-get -y install clang qemu qemu-system gcc-aarch64-linux-gnu

我正在使用以下程序编译和运行x86_64程序:

clang -nostdlib -target x86_64-linux-gnu -s hello_world.s -o hello_world.out && ./hello_world.out

我正在使用以下程序编译和运行aarch64程序:

clang -nostdlib -target aarch64-linux-gnu -s hello_world.s -o hello_world.out && ./hello_world.out

解决方法

解决方案:我需要将-fno-integrated-asclang一起使用,以告诉它直接使用GNU汇编器,而不是它自己的内置集成汇编器( suppose 替代GAS,但显然不是)。我使用以下更新的命令来编译并运行我的aarch64程序,而不会出现问题:

clang -nostdlib -fno-integrated-as -target aarch64-linux-gnu -s hello_world.s -o hello_world.out && ./hello_world.out

感谢@Jester和@Nate Eldredge帮助我在评论中进行调试。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...