如何使用适用于x86-64位的NASM读取和写入文件 [英] How to read from and write to files using NASM for x86-64bit

查看:18
本文介绍了如何使用适用于x86-64位的NASM读取和写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适用于64位Linux系统的NASM程序,它可以在标准I/O设备上运行,它看起来是这样的:

section .data

prompt      db  "Enter your text: ", 10
length      equ $ - prompt
text        times 255 db 0
textSize    equ $ - text    

section .text
global main
main:
    mov     rax, 1
    mov     rdi, 1
    mov     rsi, prompt
    mov     rdx, length
    syscall         ;print prompt

    mov     rax, 0
    mov     rdi, 0
    mov     rsi, text
    mov     rdx, textSize
    syscall         ;read text input from keyboard

    mov     rcx, rax  ; rcx  - character counter
    mov     rsi, text ; a pointer to the current character starting from the beginning.

   ****
exit:
    mov     rax, 60
    mov     rdi, 0
    syscall

我需要该程序来读取和写入文件,但我找不到必须使用哪些系统调用以及如何使用它们来实现这些结果。所以,我想知道你们中是否有人能帮我。提前谢谢。

推荐答案

使用系统调用"Open"和"Close":

在64位Linux下打开文件:

rax = 2
rdi = pointer to NUL-terminated filename
rsi = something like O_WRONLY
rdx = file flags if creating a file (e.g. 0644 = rw-r--r--)
syscall
now rax contains the file hanle

关闭文件:

rax = 3
rdi = file handle
syscall

从文件读取/写入文件:

rax = 0 or 1 (like keyboard/screen in/output)
rdi = file handle (instead of 0/1)
...

这篇关于如何使用适用于x86-64位的NASM读取和写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆