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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: "At" 是一个英文介词,通常用来表示位置、时间或方向等概念。在位置方面,在英文中 "at" 通常用来表示某个特定位置或场所,例如 "我在家里",可以翻译为 "I am at home"。在时间方面,"at" 通常用来表示具体的时刻或时间点,例如 "我会在下午3点到达",可以翻译为 "I will arrive at 3 PM"。而在方向方面,"at" 则通常用来表示某个方向的位置,例如 "他站在门口",可以翻译为 "He is standing at the door"。 "At" 这个单词很常用,我们可以通过学习它在语境中的不同用法来更好地理解它的含义。在使用 "at" 时需要注意单词后面应该跟上什么类型的名词,以便正确表达我们所想要的意思。最后,我们也应该注意英语中介词的灵活性,不同语言中介词的用法也有很大的差异,因此我们需要多加练习和理解。 ### 回答2: "At" 可以有多种含义和用法,这个词最常见的意思是介词。作为介词时,它表示位置、方向、时间等概念。 首先,它可以表达位置,例如:“The cat is sitting at the table.” 这句话中,“at the table” 表示小猫在桌子上。另外,它还可以表示某个建筑物、街道、城市等地方,例如:“I live at 123 Main Street.” 这句话中,“at 123 Main Street” 表示某人住在这个地址。 其次, "at" 还可以用来表示方向,例如:“Turn left at the traffic light.” 这句话中,“at the traffic light” 表示在交通灯处向左转。此外,它还可以表示在某个特定的位置上,例如:“We waited at the airport gate for two hours.” 这句话中,“at the airport gate” 表示在机场的航站楼候机口里等待。 最后, "at" 还可以表示时间,例如:“We will meet at 7pm tonight.” 这句话中,“at 7pm” 表示在晚上7点这个时间点上会见。除此之外,它还可以表示某个特定时间段,例如:“I am going on vacation at the end of the month.” 这句话中,"at the end of the month" 表示本月底会去度假。 总之, "at" 是一个常用的介词,可以表示位置、方向、时间等。它是学习英语语法的基础之一。 ### 回答3: 在英语中,“at”是一个常见的介词,可用于表示时间、地点或位置。例如,“at 9 o'clock”表示在九点钟,“at the park”表示在公园,“at the top of the mountain”表示在山顶。在电子邮件和聊天中,“at”也常用于引用某人的用户名,例如“@JohnDoe”表示提到了名为JohnDoe的用户。此外,“at”还可用作缩写词,例如“ATM”表示自动取款机(Automated Teller Machine),“ATP”表示三磷酸腺苷(Adenosine triphosphate)。总之,“at”是一个在英语中非常常用和多功能的介词,可以表示时间、地点、位置等,并在某些情况下用作缩写词的一部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值