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

你可能感兴趣的:(汇编)