删除程序集中的文件

Delete file in assembly

我正在尝试删除汇编代码中的文件 (NASM),但我在 "mov dx, file_name" 部分遇到了错误 "relocation truncated to fit against data"。仅供参考,我试图删除 "input.txt" 文件的文件确实存在于我的文件夹中。我不太确定这类问题。让我知道我错过了什么。

section .data
; filename
file_name           DB  "input.txt"

section .text
    global _start

_start:
    nop
    mov dx, file_name   ; getting an error : relocation truncated to fit R_386_16 against .data
    mov ah,41h          ; function 41h - delete file
    int 21h             ; call DOS service

endIt:
    nop
    ;Exit the program back to linux legally
    mov eax, 1                  ; exit system call value
    mov ebx, 0                  ; exit return code
    int 80h                     ; Call the kernel

在 Linux 上,要删除文件(在 UNIX 术语中,“取消链接”文件),您需要使用系统调用 unlink。它唯一的参数是指向要取消链接的文件名的指针,它 returns 成功时为 0,错误时为负值。 unlink在i386上的系统调用号是10,所以要调用unlink,代码如下:

mov eax, 10        ; system call 10: unlink
mov ebx, file_name ; file name to unlink
int 80h            ; call into the system

文件名应为以 NUL 结尾的 C 字符串。您可以通过将 ,0 附加到您的字符串来制作 C 字符串:

file_name    DB  "input.txt",0

请注意,此系统调用不能用于取消链接目录。要删除目录(这里使用“removed”是因为删除目录在传统文件系统上比仅仅取消链接更复杂),您需要使用 rmdir 系统调用,它具有编号 40 和相同的参数,并且 return值。