【发布时间】:2017-09-07 10:30:12
【问题描述】:
我一直在使用 Richard Blum 的“专业汇编语言”一书学习汇编,并通过在 MacOS 上编写汇编来完成所有这些,当然除了一些“使用文件”练习。具体来说,在附加文件时遇到问题。我可以写入文件,没问题,但不确定是否有正确的“访问模式值”来附加文件。根据 usr/include/sys/fcntl.h 文件,MacOS 喜欢使用 0x0008 来附加文件。 PAL 手册使用 $02002(八进制)。 (我想我可以尝试使用库函数来代替,但显然这些只是“int”系统调用的包装,只是试图了解这一切是如何工作的)。
感谢您的帮助,如果这是一个愚蠢的问题或做了一些非常愚蠢的事情,我们深表歉意。干杯。
这是我的代码:
.data
filename:
.asciz "cpuid.txt"
output:
.asciz "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
.bss
.lcomm filehandle, 4
.text
.globl _main
_main:
movl $0, %eax
# Get the CPUID and place the CPUID values (stored in ebx, edx and ecx) accordingly within,
# the correct address space, after the 'output' address.
cpuid
movl $output, %edi
movl %ebx, 28(%edi)
movl %edx, 32(%edi)
movl %ecx, 36(%edi)
# OPEN/CREATE A FILE:
movl $5, %eax
movl $filename, %ebx
movl $0x0008, %ecx # Access mode values loaded into ECX
#.... APPEND TEXT FILE, using a $02002(octal) according to PAL textbook
# on MacOS, APPEND mode is 0x0008 or $00007(octal)? according to usr/include/sys/fcntl.h
movl $0644, %edx # file permission values loaded into EDX
# For MacOS, we need to put all of this on the stack (in reverse order),
# and, add an additional 4-bytes of space on the stack,
# prior to the system call (with 'int')
pushl %edx
pushl %ecx
pushl %ebx
subl $4, %esp
int $0x80 # ...make the system call
addl $16, %esp # clear the stack
test %eax, %eax # check the error code returned (stored in EAX) after attempting to open/create the file
js badfile # if the value was negative (i.e., an error occurred, then jump)
movl %eax, filehandle # otherwise, move the error code to the 'filehandle'
# WRITE TO FILE:
movl $4, %eax
movl filehandle, %ebx
movl $output, %ecx
movl $42, %edx
# once again, for MacOS, put all of this on the stack,
# and, add an additional 4-bytes of space on the stack
pushl %edx
pushl %ecx
pushl %ebx
subl $4, %esp
int $0x80
addl $16, %esp # and, again, clear the stack
test %eax, %eax
js badfile
# CLOSE THE FILE:
movl $6, %eax
movl filehandle, %ebx
# okay, move it onto the stack again (only one parameter on stack for closing this time)
pushl %ebx
subl $4, %esp
int $0x80
addl $8, %esp
badfile:
subl $9, %esp
movl %eax, %ebx
movl $1, %eax
int $0x80
【问题讨论】:
-
在打开文件时将 $02001 放入 %ecx 似乎不起作用。如果我使用该访问模式代码然后尝试附加文件,则该文件似乎不会附加到 mac osx (MacOS) 中(尽管如果它为空,则能够写入“cpuid.txt”上方的文件)。
-
如果
O_APPEND在 mac 上是 0x8,那么我希望O_WRONLY | O_APPEND是 0x9。 -
好的,在转换一个使用 O_APPEND 的 C 程序并检查堆栈中的值后,我发现了 Mac OS X (MacOS) 的附加值......它是 $012(八进制)或 0x0a .
-
是的,你是对的。 0x9 (O_WRONLY | O_APPEND) 也有效。我正在查看的 C 程序使用了 O_RDWR|O_APPEND,这意味着 2+8 或 0x0a。所以,0x09 或 0x0a 到 ECX 上面(而不是 0x0008)有效。感谢您帮助解决这个问题。
标签: macos file assembly append