I've been learning assembly using the "Professional Assembly Language" book by Richard Blum and have gotten through all of it by writing assembly on MacOS, except of course a few of the "using files" exercises. Specifically, having trouble with appending a file. I can write to file, no problem, but not sure if have the correct "access mode values" for appending the file. According to the usr/include/sys/fcntl.h file MacOS likes using 0x0008 for appending files. The PAL book uses $02002(octal). (I suppose I could try doing this with library functions instead, but apparently those are just wrappers for the 'int' system calls, and just trying to understand how this all works).
Thanks for any help, and sorry if this is a dumb question or a did something really dumb. cheers.
Here's my code:
.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