AT&T汇编操作文件

相关的系统调用

系统调用系统调用值描述
open5打开文件
read3读取文件
write4写入文件
close6关闭文件

打开关闭文件

  • 打开文件
    • EAX 存储系统调用值
    • EBX 存储文件名
    • ECX 存储访问文件的权限
c语言访问权限对应的数值
O_RDONLY00
O_WRONLY01
O_RDWR02
O_CREATE0100
O_EXCL0200
O_TRUNC01000
O_APPEND02000
O_NONBLOCK04000
O_SYNC010000
O_ASYNC020000
* EDX 存储创建新文件是的权限
* 返回信息保存在eax
错误名错误值描述
EPERM1操作错误
ENOENT2文件不存在
EBADF3错误的文件句柄
EACCES13权限错误
EFAULT14错误的文件地址
EBUSY16设备忙
EEXIST17文件存在的
EISDIR21是文件目录
EMFILE24太多的打开文件
EFBIG27文件太大
EROFS30只读的文件系统
ENAMERTOOLONG36文件名太长
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 内存大小

内存映射文件

系统调用
mmap90
munmap91
msync144
  • 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_NONE0不能访问
PROT_READ1读访问
PROT_WRITE2写访问
PROT_EXEC4执行权限

flag:

类型描述
MAP_SHARE1内存映射文件可以和其他进程共享
MAP_PRIVATE2当前进程私有
  • 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值