//--------------------------------------------------------------- // 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 // revised on: 15 FEB 2009 -- for Linux x86_64 environment //--------------------------------------------------------------- .equ sys_WRITE, 1 # system-call ID-number .equ sys_OPEN, 2 # system-call ID-number .equ sys_CLOSE, 3 # system-call ID-number .equ sys_EXIT, 60 # 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: .quad -1 # for storing file-handle flags: .quad O_CREAT | O_WRONLY | O_TRUNC # for access-mode access: .quad 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 2009\n" # filedata line 2 .ascii "University of San Francisco\n" # filedata line 3 size: .quad . - info # total bytes of filedata .section .text _start: # open the file for writing mov $sys_OPEN, %rax # specify the service-ID lea fname, %rdi # pointer to file's name mov flags, %rsi # how we access the file mov access, %rdx # access-rights for file syscall # invoke kernel service mov %rax, handle # preserve return-value # write information to the file mov $sys_WRITE, %rax # specify the service-ID mov handle, %rdi # setup file's 'handle' lea info, %rsi # point to data-buffer mov size, %rdx # amount of data (bytes) syscall # invoke kernel service # close the file mov $sys_CLOSE, %rax # specify the service-ID mov handle, %rdi # setup file's handle syscall # invoke kernel service # terminate this program mov $sys_EXIT, %rax # specify the service-ID mov $0, %rdi # use zero as exit-code syscall # invoke kernel service .global _start # make entry-point public .end # no more to be assembled