在mac上的程序集中打开并写入文件

时间:2016-01-16 17:52:40

标签: macos assembly x86 nasm 32-bit

我在mac上学习32位汇编,我一直在尝试写入桌面上的文件。我用了这段代码:

global _start
section .data
path:   db  "/Users/jackliu/Desktop/test.txt",0
string: db  "hello",0
.len:   equ $ - string

section .text

_start:
mov eax, 5
push dword 2
push dword path
sub esp, 8
int 0x80
add esp, 16
mov ebx, eax

mov eax, 4
push dword string.len
push dword string
push dword ebx
sub esp, 4
int 0x80
add esp, 16

mov eax, 1
push 0
sub esp, 12
int 0x80

该文件为空,它已存在于我的桌面上。运行后,它根本不会更改文件。

我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

在MAC OS上int 0x80开放系统调用是:

  

5 AUE_OPEN_RWTC ALL {int open(user_addr_t path,int flags,int mode); }

您的代码传递了2个参数:

mov eax, 5       ; Open system call = 5
push dword 2     ; Read/Write flag
push dword path  ; Path
sub esp, 8       ; Alignment
int 0x80

由于您要打开现有文件,请为第三个参数指定模式0。您的代码可能如下所示:

mov eax, 5       ; Open system call = 5
push dword 0     ; Mode = 0
push dword 2     ; Read/Write flag
push dword path  ; Path
sub esp, 4       ; Reserved space for system call
int 0x80