//---------------------------------------------------------------- // filedemo.s // // This assembly language program allows us to demonstrate // the way in which an application executing in user-space // can request the Linux kernel to perform system-services // that the application is not able to perform for itself. // Here our application will make four system-calls (open, // write, close, and exit), to create a new file with some // textual data, located in the current working directory. // // assemble using: $ as filedemo.s -o filedemo.o // and link using: $ ld filedemo.o -o filedemo // // We can then employ a debugging utility (in class) to do // a live 'single-step' trace of this program's execution, // watching each instruction's effect upon register-values // and memory-locations, and observing the kernel response // to each of the individual system-calls being invoked. // // programmer: ALLAN CRUSE // written on: 31 AUG 2004 //--------------------------------------------------------------- .section .data fname: .asciz "testfile.dat" # name to use for our new file fd: .int -1 # storage for the file-descriptor buf: .ascii "CS 326: Operating Systems (4-units) FALL 2004\n" len: .int . - buf # number of bytes of textual data .section .text _start: // open the file movl $5, %eax # sys_OPEN leal fname, %ebx # address of the filename movl $01101, %ecx # flags: O_TRUNC | O_CREAT | O_WRONLY movl $0666, %edx # mode int $0x80 # enter the kernel mov %eax, fd # save return-value // write to the file movl $4, %eax # sys_WRITE movl fd, %ebx # file-descriptor leal buf, %ecx # address of data buffer movl len, %edx # length of the data int $0x80 # enter the kernel // close the file movl $6, %eax # sys_CLOSE movl fd, %ebx # file-descriptor int $0x80 # enter the kernel // exit this program movl $1, %eax # sys_EXIT movl $0, %ebx # return-code int $0x80 # enter the kernel .globl _start # makes entry-point visible