//--------------------------------------------------------------- // out2file.s // // This demo shows the basic steps needed to create a file. // // to assemble: $ as out2file.s -o out2file.o // and to link: $ ld out2file.o -o out2file // // programmer: ALLAN CRUSE // written on: 24 JAN 2008 //--------------------------------------------------------------- .equ sys_EXIT, 1 # system-call ID-number .equ sys_WRITE, 4 # system-call ID-number .equ sys_OPEN, 5 # system-call ID-number .equ sys_CLOSE, 6 # system-call ID-number .equ S_ACCESS, 00600 # file-permissions (octal) .equ O_CREAT, 0100 # access-mode flag (octal) .equ O_TRUNC, 01000 # access-mode flag (octal) .equ O_WRONLY, 01 # access_mode flag (octal) .section .data handle: .int -1 # for storing file-handle flags: .int O_CREAT | O_WRONLY | O_TRUNC # for access-mode access: .int S_ACCESS # for file's permissions fname: .asciz "mycookie.dat" # for storing file's name info: .ascii "Computer Science 210\n" # filedata line 1 .ascii "Spring 2008\n" # filedata line 2 .ascii "University of San Francisco\n" # filedata line 3 size: .int . - info # total bytes of filedata .section .text _start: # open the file for writing mov $sys_OPEN, %eax # specify the service-ID lea fname, %ebx # pointer to file's name mov flags, %ecx # how we access the file mov access, %edx # access-rights for file int $0x80 # invoke kernel service mov %eax, handle # preserve return-value # write information to the file mov $sys_WRITE, %eax # specify the service-ID mov handle, %ebx # setup file's 'handle' lea info, %ecx # point to data-buffer mov size, %edx # amount of data (bytes) int $0x80 # invoke kernel service # close the file mov $sys_CLOSE, %eax # specify the service-ID mov handle, %ebx # setup file's handle int $0x80 # invoke kernel service # terminate this program mov $sys_EXIT, %eax # specify the service-ID mov $0, %ebx # use zero as exit-code int $0x80 # invoke kernel service .global _start # make entry-point public .end # no more to be assembled