# ;; cmp.s # should be a replacement for /bin/cmp ... ;-) # #include # #include # I'd like to use this, but... it's not feasible as far as I can tell .file "cmp.s" .data _argc: # argument count .long 0 _argv: # pointer to arguments .long 0 _exit_status: # exit status, initially zero, change it if and when we have an error .long 0 _byte_counter: # byte counter .long 1 _line_counter: # line counter .long 1 _fd1: # file descriptor 1 .long 0 _fd2: # file descriptor 2 .long 0 _buffer0: .space 4096,0 _buffer1: .space 4096,0 .text .globl cmp cmp: # xor %r11,%r11 # exit status, change it if and when we have an error # mov $1,%r12 # we'll use %r12 for our byte counter # xor $1,%r13 # we'll use %r13 for our line counter _break: pop %eax # this is argc... cmp $3,%eax # check that we have the right number of arguments jne _error_finish pop %ebx # remove command dec %eax jz _finish # check for no args left in argv # open first file mov $0,%ecx # not "xor" since we want to make it clear that this is $0 = O_RDONLY, not just any zero! pop %ebx # this is the next *argv --- go ahead and put it in %rdi since we are going to try to open it for reading mov $__NR_open,%eax int $0x80 cmp $0,%eax jl _error_finish mov %eax,_fd1 # save file descriptor in _fd1 # open second file mov $0,%ecx # not "xor" since we want to make it clear that this is $0 = O_RDONLY, not just any zero! pop %ebx # this is the next *argv --- go ahead and put it in %rdi since we are going to try to open it for reading mov $__NR_open,%eax int $0x80 cmp $0,%eax jl _error_finish mov %eax,_fd2 # save file descriptor in _fd2 _loop: # read one byte from file 0 into buffer0 mov _fd1,%ebx # recover file descriptor from %r14 lea _buffer0,%ecx # buffer location mov $1,%edx # how many bytes mov $__NR_read,%eax int $0x80 cmp $0,%eax jl _error_finish je _file0_finish # read one byte from file 1 into buffer1 mov _fd2,%ebx # recover file descriptor from %r15 lea _buffer1,%ecx # buffer location mov $1,%edx # how many bytes mov $__NR_read,%eax int $0x80 cmp $0,%eax jl _error_finish je _file1_finish mov (_buffer0),%eax cmp %eax,(_buffer1) jne _difference_found incl _byte_counter # byte count # check to see if end-of-line cmp $12,%eax # if memory serves... jne _loop incl _byte_counter # line count jmp _loop _file0_finish: # check to see if file2 is finished also --> if so, same # --> if not, then different mov _fd2,%ebx lea _buffer1,%ecx mov $1,%edx mov $__NR_read,%eax int $0x80 cmp $0,%eax je _finish jl _error_finish # or we fall through to "different" finish _file1_finish: _difference_found: mov $1,%ebx mov $__NR_exit, %eax # int $0x80 = 60 is exit int $0x80 _error_finish: mov $2,%ebx mov $__NR_exit, %eax # int $0x80 = 60 is exit int $0x80 _finish: mov _exit_status,%ebx # and exit with a status mov $__NR_exit,%eax # int $0x80