AT&T彙編操作文件

相關的系統調用

系統調用 系統調用值 描述
open 5 打開文件
read 3 讀取文件
write 4 寫入文件
close 6 關閉文件

打開關閉文件

  • 打開文件
    • EAX 存儲系統調用值
    • EBX 存儲文件名
    • ECX 存儲訪問文件的權限
c語言訪問權限 對應的數值
O_RDONLY 00
O_WRONLY 01
O_RDWR 02
O_CREATE 0100
O_EXCL 0200
O_TRUNC 01000
O_APPEND 02000
O_NONBLOCK 04000
O_SYNC 010000
O_ASYNC 020000
* EDX 存儲創建新文件是的權限
* 返回信息保存在eax
錯誤名 錯誤值 描述
EPERM 1 操作錯誤
ENOENT 2 文件不存在
EBADF 3 錯誤的文件句柄
EACCES 13 權限錯誤
EFAULT 14 錯誤的文件地址
EBUSY 16 設備忙
EEXIST 17 文件存在的
EISDIR 21 是文件目錄
EMFILE 24 太多的打開文件
EFBIG 27 文件太大
EROFS 30 只讀的文件系統
ENAMERTOOLONG 36 文件名太長
movl $5, %eax
movl $filename, %ebx
movl $0120, %ecx
movl $0644, %edx
int $0x80
test %eax, %eax
js badfile
  • 關閉文件
movl filehandle %ebx
movl $6, %eax
int $0x80

向文件寫入內容

#cpuidfile.s - An example of writing data to a file
.section .data

filename:
	.asciz "cpuid.txt"
output:
	.asciz "The processor Vendor ID is `XXXXXXXXXXXX'\n"
.section .bss
	.lcomm filehandle, 4
.section .text
.globl _start
_start:
	movl $0, %eax
	cpuid
	movl $output, %edi
	movl %ebx, 28(%edi)
	movl %edx, 32(%edi)
	movl %ecx, 36(%edi)

	movl $5, %eax
	movl $filename, %ebx
	movl $01101, %ecx
	movl $0644, %edx
	int $0x80
	test %eax, %eax
	js badfile
	movl %eax, filehandle

	movl $4, %eax
	movl filehandle, %ebx
	movl $output, %ecx
	movl $42, %edx
	int $0x80
	test %eax, %eax
	js badfile

	movl $6, %eax
	movl filehandle, %eax
	int $0x80

	badfile:
		movl %eax, %ebx
		movl $1, %eax
		int $0x80

讀取文件

  • EAX 讀取文件的系統調用號
  • EBX 文件句柄
  • ECX 內存地址
  • EDX 內存大小

內存映射文件

系統調用
mmap 90
munmap 91
msync 144
  • mmap 系統調用
	void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
	
	start: 將映射文件存放的內存地址
	length:映射到內存的字節長度
	prot:內存的保護設置
	flags:映射對象的類型
	fd:要映射的文件句柄
	offset:映射文件的起始地址

prot:

類型 描述
PROT_NONE 0 不能訪問
PROT_READ 1 讀訪問
PROT_WRITE 2 寫訪問
PROT_EXEC 4 執行權限

flag:

類型 描述
MAP_SHARE 1 內存映射文件可以和其他進程共享
MAP_PRIVATE 2 當前進程私有
  • msync 系統調用
    int msync(const void *start, size_t length, int flags);

    flags:
    MS_ASYNC
    MS_SYNC

  • munmap 系統調用
    int munmap(void *start, size_t length);

# sizefunc.s - Find the size of a file 
.section .text
.globl sizefunc
.type sizefunc, @function
sizefunc:
	pushl %ebp
	movl %esp, %ebp
	subl $8, %esp
	pushl %edi
	pushl %esi
	pushl %ebx

	movl $140, %eax
	movl 8(%ebp), %ebx
	movl $0, %ecx
	movl $0, %edx
	leal -8(%ebp), %esi
	movl $2, %edi
	int $0x80
	movl -8(%ebp), %eax

	popl %ebx
	popl %esi
	popl %edi
	movl %ebp, %esp
	popl %ebp
	ret

# convert.s - A function to convert lower case letters to upper case
.section .text
.type convert, @function
.globl convert
convert:
	pushl %ebp
	pushl %esp, %ebp
	pushl %esi
	pushl %edi

	movl 12(%ebp), %esi
	movl %esi, %edi
	movl 8(%ebp), %ecx

convert_loop:
	loadsb
	cmpb $0x61, %al
	jl skip
	cmpb $0x7a, %al
	jg skip
	subb $0x20, %al
skip
	stosb	
	loop convert_loop

	pop %edi
	pop %esi
	movl %ebp, %esp
	popl %ebp
	ret
# fileconvert.s - Memory map a file and convert it
.section .bss
	.lcomm filehandle, 4
	.lcomm size, 4
	.lcomm mappedfile, 4
.section .text
.globl _start
_start:
	# get the file name and open it in read/write
	movl %esp, %ebp
	movl$5, %eax
	movl 8(%ebp), %ebx
	movl $0102, %ecx
	movl $0644, %edx
	int $0x80
	test %eax, %eax
	js badfile
	movl %eax, filehandle

	# find the size of the file
	pushl filehandle
	call sizefunc
	movl %eax, size
	addl $4, %esp

	# map file to memory
	push $0
	pushl filehandle
	pushl $1 #MAP_SHARED
	pushl $3 #PROT_READ | PROT_WRITE
	pushl size # file size
	pushl $0 # null
	movl %esp, %ebx
	movl $90, %eax
	int $0x80
	test %eax, %eax
	js badfile
	movl %eax, mappedfile
	addl $24, %esp

	#convert the memory mapped file to all uppers
	pushl mappedfile
	pushl size
	call convert
	addl $8, %esp

	# use munmap to send the changs to the file
	movl $91, %eax
	movl mappedfile, %ebx
	movl size, %ecx
	test %eax, %eax
	jnz badfile

	# close the open file handle
	movl $6, %eax
	movl filehandle, %ebx
	int $0x80

badfile:
	movl %eax, %ebx
	movl $1, %eax
	int $0x80
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章