【问题标题】:Open and write to file in assembly on mac在 Mac 上的程序集中打开并写入文件
【发布时间】:2016-04-22 04:12:16
【问题描述】:

我正在 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

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

我的代码有什么问题吗?

【问题讨论】:

  • 你是对的。在 os x 中,我读到您必须将堆栈与第 16 位对齐,所以这就是我想要做的。我只是根据您的建议尝试过,但没有运气。
  • 好的,现在可以正常使用了。谢谢!

标签: macos assembly x86 nasm 32-bit


【解决方案1】:

在 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

【讨论】:

  • 好的,所以我对此进行了更多试验。我删除了桌面上的文件。然后,我将push dword 0 替换为push dword 0666o,将push dword 2 替换为push dword 0102o。这应该在我的桌面上创建文件,然后向它写“hello”,但什么也没发生。我应该添加其他内容吗?
  • @JackLiu : push dword 0102o 看起来不对。你确定你不是指push dword 0102h(十六进制而不是八进制?)
  • 我刚试过。可悲的是它没有奏效。我可能认为 Mac 上的 O_CREAT 标志与其他计算机上的不同?但是我找不到任何资源。
  • Doh,我在想 Linux。在 OS/X 上应该是 0202h。查找这些类型的最佳位置是在/usr/include/sys/fcntl.h 的系统标头中
  • 效果很好。谢谢,但我的 usr 文件夹中没有包含目录。
猜你喜欢
  • 2017-08-26
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 2011-08-16
相关资源
最近更新 更多