我一直在使用理查德·布鲁姆的“专业汇编语言”一书学习汇编,并通过在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
答案 0 :(得分:1)
下面是用于在mac os x(MacOS)上附加文件的更正的汇编语言代码版本(AT& T样式)。
使用'as'和'ld'命令在Mac终端中编译'myfile.s'程序集文件:
as -m32 -o myfile.o myfile.s
ld -e _main -o myfile myfile.o
.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 $0x09, %ecx # Access mode values loaded into ECX
# on Linux, APPEND TEXT FILE using a $02002(octal) according to "Professional Assembly Language" (PAL) textbook
# however, on Mac OS X (MacOS), APPEND mode is 0x0008, according to usr/include/sys/fcntl.h
# ... so for write (0x01) and append (0x08) access (O_WRONLY | O_APPEND), the value becomes 0x09
# (for read/write (0x02) and append (0x08) access (O_RDWR|O_APPEND), the value would be 0x0a)
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, include an additional 4-bytes of space on the stack,
# (stack grows downward, thus the 'subl' instruction) prior to the 'int' system call
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