`

assembly - file

阅读更多

assembly operate file,

 

------

unix/linux file

 

linux use the same file concept of unix, any file could be accessed as a sequential stream of bytes,

 

step to use file:

* open file

      open file by file path, and will get a number called file descriptor, which will be used to refer to the file later,

* read/write file

      then read/write file with file descriptor,

* close file

      when done, close file, and the file descriptor will be released,

 

------

file relative syscalls

 

include:

* open file

      request:

            %eax: syscall number, 5,

            %ebx: address of file name,

            %ecx: open mode

            %edx: permission

      response:

            %eax: file descriptor which >=0, or error number which <0,

* close file

      request:

            %eax: syscall number, 6,

            %ebx: file descriptor,

* read file

      request:

            %eax: syscall number, 3,

            %ebx: file descriptor,

            %ecx: address of buffer

            %edx: size(byte) to read

      response:

            %eax: size(byte) that actural read which >=0 (0 means end of file), or error code which <0,

* write file

      request:

            %eax: syscall number, 3,

            %ebx: file descriptor,

            %ecx: address of buffer

            %edx: size(byte) to write

      response:

            %eax: size(byte) that actural write which >=0, or error code which <0,

 

------

file open mode

 

* 0

      read only

* 03101

      write only, create file if not exist, clear file first if exist,

* ..

 

------

permission

 

same as linux permission, in octal,

e.g.

      0644

      0666

 

------

standard & special files

 

standard file:

      linux has at least 3 standard file opened when started, and their file descriptor is already available, need not to open/close them,

      include:

      * STDIN

            standard input, represent keyboard usually, read only,

            file descriptor = 0,

            use enter to input a line, ctrl+c to stop program, the last line without enter will be ignored,

      * STDOUT

            standard output, represent screen display usually, write only,

            file descriptor = 1,

      * STDERR

            standard error, represent screen display usually, write only, error msg usually go to this file,

            file descriptor = 2,

 

special file:

      linux treat everything as file, include special file which are actural not file,

      include: input/output system, network connection, serial port, pipe that processes communicate with each other, .. ,

      some of special file are open/close by different methods than regular file, but they can be read/write the same as regular file,

 

------

end of file

 

read, return readed byte count into %eax, 0 means end of file,

 

------

code

 

file_copy.s

 

# file copy
# 	
# command line param:
#	first param:
#		input file path
#	second param:
# 		output file path
# 

.section .data
# constants
# stack position
.equ ST_ARGC, 0		# param count
.equ ST_ARGV_0, 4	# program file name
.equ ST_ARGV_1, 8	# input file name
.equ ST_ARGV_2, 12	# output file name
.equ ST_SIZE_RESERVE, 8	# reserve size
.equ ST_FD_IN, -4	# file descriptor - input file
.equ ST_FD_OUT, -8	# file descriptor - output file
# syscall number
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_EXIT, 1
# file open mode
.equ MODE_RDONLY, 0
.equ MODE_WRONLY_TRUNC, 03101
# syscall interrupt
.equ LINUX_SYSCALL, 0x80
# end of file
.equ END_OF_FILE, 0	# return value of read

.section .bss
.equ BUF_SIZE, 1024	# BUF_DATAfer size in byte
.lcomm BUF_DATA, BUF_SIZE	# a buffer

.section .text
.globl _start

_start:
movl %esp, %ebp	# backup %esp
subl $ST_SIZE_RESERVE, %esp	# reserve stack
jmp file_open

file_open:
open_input:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_RDONLY, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_IN(%ebp)
open_output:
movl $SYS_OPEN, %eax
movl ST_ARGV_2(%ebp), %ebx
movl $MODE_WRONLY_TRUNC, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_OUT(%ebp)
jmp file_read

# read/write until end of file
file_read:
movl $SYS_READ, %eax
movl ST_FD_IN(%ebp), %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
cmpl $END_OF_FILE, %eax
jle file_close
jmp file_write

file_write:
movl %eax, %edx
movl $SYS_WRITE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL
jmp file_read

file_close:
close_input:
movl $SYS_CLOSE, %eax
movl ST_FD_IN(%ebp), %ebx
int $LINUX_SYSCALL
close_output:
movl $SYS_CLOSE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL
movl %ebp, %esp	# restore %esp
jmp exit

exit:
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL
 

file_toupper.s

 

# file to upper case
# 	
# command line param:
#	first param:
#		input file path
#	second param:
# 		output file path
# 

.section .data
# constants
# stack position
.equ ST_ARGC, 0		# param count
.equ ST_ARGV_0, 4	# program file name
.equ ST_ARGV_1, 8	# input file name
.equ ST_ARGV_2, 12	# output file name
.equ ST_SIZE_RESERVE, 8	# reserve size
.equ ST_FD_IN, -4	# file descriptor - input file
.equ ST_FD_OUT, -8	# file descriptor - output file
# syscall number
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_EXIT, 1
# file open mode
.equ MODE_RDONLY, 0
.equ MODE_WRONLY_TRUNC, 03101
# syscall interrupt
.equ LINUX_SYSCALL, 0x80
# end of file
.equ END_OF_FILE, 0	# return value of read

.section .bss
.equ BUF_SIZE, 1024	# BUF_DATAfer size in byte
.lcomm BUF_DATA, BUF_SIZE	# a buffer

.section .text
.globl _start

_start:
movl %esp, %ebp	# backup %esp
subl $ST_SIZE_RESERVE, %esp	# reserve stack
jmp file_open

file_open:
open_input:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_RDONLY, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_IN(%ebp)
open_output:
movl $SYS_OPEN, %eax
movl ST_ARGV_2(%ebp), %ebx
movl $MODE_WRONLY_TRUNC, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_OUT(%ebp)
jmp file_read

# read/write until end of file
file_read:
movl $SYS_READ, %eax
movl ST_FD_IN(%ebp), %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
cmpl $END_OF_FILE, %eax
jle file_close
pushl $BUF_DATA	# param 1
pushl %eax	# backup %eax, as well as param 0
call toupper
popl %eax	# restore %eax
addl $4, %esp	# restore %esp
jmp file_write

file_write:
movl %eax, %edx
movl $SYS_WRITE, %eax
movl ST_FD_OUT(%ebp), %ebx
movl $BUF_DATA, %ecx
int $LINUX_SYSCALL
jmp file_read

file_close:
close_input:
movl $SYS_CLOSE, %eax
movl ST_FD_IN(%ebp), %ebx
int $LINUX_SYSCALL
close_output:
movl $SYS_CLOSE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL
movl %ebp, %esp	# restore %esp
jmp exit

exit:
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL

# a function that convert a buffer in memory from lower case to upper case
# param:
#       first param:
#               buffer size, in byte,
#       second param:
#               start address of buffer
# storage:
#       %eax:
#               start address of buffer
#       %ebx:
#               buffer size
#       %cl:
#               store each char in buffer
#       %edi:
#             	current buffer offset
.type toupper, @function
toupper:
.equ LOWER_A, 'a'
.equ LOWER_Z, 'z'
.equ LOWER_TO_UPPER, 'a'-'A'
toupper_begin:
pushl %ebp	# backup %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx	# param 0 - actual buf size
movl 12(%ebp), %eax	# param 1 - buf location
movl $0, %edi		# init %edi for loop
jmp toupper_loop

toupper_loop:
cmpl %edi, %ebx
jle toupper_end
movb (%eax, %edi, 1), %cl	# read 1 byte
cmpb $LOWER_A, %cl
jl toupper_loop_end
cmpb $LOWER_Z, %cl
jg toupper_loop_end
subb $LOWER_TO_UPPER, %cl	# lower to upper
movb %cl, (%eax, %edi, 1)	# change buf
toupper_loop_end:
incl %edi
jmp toupper_loop

toupper_end:
movl %ebp, %esp
popl %ebp
ret

 

file_stdin.s

 

# file - stdin, save input of stdin to a file,
#
# params:
#	first param: output file path
# keys:
# 	enter -> to input a line,
# 	ctrl+c -> to stop program,
# 

.section .data
.equ SYS_EXIT, 1
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ MODE_REONLY, 0
.equ MODE_WTONLY_TRUNC, 03101
.equ FD_STDIN, 0
.equ LINUX_SYSCALL, 0x80
.equ EOF, 0
.equ ST_ARGC, 4		# param count
.equ ST_ARGV_0, 8	# program file path
.equ ST_ARGV_1, 12	# output file path
.equ ST_RESERVE_SIZE, 8
.equ ST_FD_OUTPUT, -4
.equ ST_LAST_READ_SIZE, -8
.section .bss
.equ BUF_SIZE, 100
.lcomm BUF_DATA, BUF_SIZE+1
.section .text
.globl _start

_start:
pushl %ebp
movl %esp, %ebp
subl $ST_RESERVE_SIZE, %esp
jmp file_open

file_open:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_WTONLY_TRUNC, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_OUTPUT(%ebp)

read:
movl $SYS_READ, %eax
movl $FD_STDIN, %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
movl %eax, ST_LAST_READ_SIZE(%ebp)

write:
movl $SYS_WRITE, %eax
movl ST_FD_OUTPUT(%ebp), %ebx
movl $BUF_DATA, %ecx
movl ST_LAST_READ_SIZE(%ebp), %edx
int $LINUX_SYSCALL
jmp read

file_close:
movl $SYS_CLOSE, %eax
int $LINUX_SYSCALL

end:
movl %ebp, %esp
popl %ebp
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL

 
file_stdout.s

 

# file - stdout, read file and print to stdout, similar to cat command
#
# params:
#	first param: input file path
# 

.section .data
.equ SYS_EXIT, 1
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ MODE_REONLY, 0
.equ MODE_WTONLY_TRUNC, 03101
.equ FD_STDIN, 0
.equ FD_STDOUT, 1
.equ LINUX_SYSCALL, 0x80
.equ EOF, 0
.equ ST_ARGC, 4		# param count
.equ ST_ARGV_0, 8	# program file path
.equ ST_ARGV_1, 12	# input file path
.equ ST_RESERVE_SIZE, 8
.equ ST_FD_INPUT, -4
.equ ST_LAST_READ_SIZE, -8
.section .bss
.equ BUF_SIZE, 100
.lcomm BUF_DATA, BUF_SIZE
.section .text
.globl _start

_start:
pushl %ebp
movl %esp, %ebp
subl $ST_RESERVE_SIZE, %esp
jmp file_open

file_open:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_REONLY, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_INPUT(%ebp)

read:
movl $SYS_READ, %eax
movl ST_FD_INPUT(%ebp), %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
movl %eax, ST_LAST_READ_SIZE(%ebp)

write:
cmpl $0, %eax
jle file_close
movl $SYS_WRITE, %eax
movl $FD_STDOUT, %ebx
movl $BUF_DATA, %ecx
movl ST_LAST_READ_SIZE(%ebp), %edx
int $LINUX_SYSCALL
jmp read

file_close:
movl $SYS_CLOSE, %eax
int $LINUX_SYSCALL

end:
movl %ebp, %esp
popl %ebp
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL
 

------


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics