在程序集中获取系统调用的返回值

问题描述 投票:0回答:1

我正在Darwin x86_64(MacOS 10.14.6)上编写一个简单的汇编程序,该程序将创建文件(test.txt)并向其中写入“ E”。但是,由于某种原因,“ E”未写入文件。我在做什么错?

这是我的程序:

global start
section .text
    start:
        ;Create the file
        mov rax, 0x2000005
        mov rdi, fname
        mov rsi, 0x200
        mov rdx, 0o644
        syscall

        ;Write to file
        mov rdi, rax     ;As far as I know, this uses the fd returned by creating a file (previous syscall)
        mov rsi, msg
        mov rdx, 1
        mov rax, 0x2000004
        syscall

        ;Exit
        mov rax, 0x2000001
        mov rdi, 0
        syscall
section .data
    msg: db "E"     ;Message
    fname: db "test.txt"     ;File name

我也尝试过这个:

global start
section .text
    start:
        mov rax, 0x2000005
        mov rdi, fname
        mov rsi, 0x200
        mov rdx, 0o644
        syscall
        mov rdi, rax
        mov rsi, msg
        mov rdx, 2
        mov rax, 0x2000004
        syscall
        mov rax, 0x2000001
        mov rdi, 0
        syscall
section .data
    msg: db "E", -1
    fname: db "test.txt", 0

但都不起作用。

macos assembly x86-64
1个回答
0
投票

我找到了答案:

[我发现先创建文件,然后将其打开以进行写然后写作品。

global start
section .text
    start:
        ;Create file
        mov rax, 0x2000005
        mov rdi, fname
        mov rsi, 0x200
        mov rdx, 0o644
        syscall

        ;Open for writing
        mov rax, 0x2000005
        mov rdi, fname
        mov rsi, 1
        mov rdx, 0o644

        ;Now write
        syscall
        mov rdi, rax
        mov rsi, msg
        mov rdx, 1
        mov rax, 0x2000004
        syscall

        ;Exit
        mov rax, 0x2000001
        mov rdi, 0
        syscall
section .data
    msg: db "E"
    fname: db "test.txt"
© www.soinside.com 2019 - 2024. All rights reserved.