如何在汇编器中打开文件并对其进行修改? [英] How to open a file in assembler and modify it?

查看:136
本文介绍了如何在汇编器中打开文件并对其进行修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Assembler,并且在Unix中工作.我想打开一个文件并在上面写"Hello world".

I'm starting to learn Assembler and I'm working in Unix. I want to open a file and write 'Hello world' on it.

section .data

textoutput db 'Hello world!', 10
lentext equ $ - textoutput
filetoopen db 'hi.txt'

section .text
global _start

_start:

mov eax, 5            ;open
mov ebx, filetoopen
mov ecx, 2            ;read and write mode
int 80h

mov eax, 4
mov ebx, filetoopen   ;I'm not sure what do i have to put here, what is the "file descriptor"?
mov ecx, textoutput
mov edx, lentext

mov eax, 1
mov ebx, 0
int 80h              ; finish without errors

但是当我编译它时,它什么也没做.我究竟做错了什么? 当我打开文件时,文件描述符值返回到哪里?

But when I compile it, it doesn't do anything. What am I doing wrong? When I open a file where does the file descriptor value return to?

推荐答案

这是x86 Linux(x86不是唯一的汇编语言,Linux也不是唯一的Unix!)...

This is x86 Linux (x86 is not the only assembly language, and Linux is not the only Unix!)...

section .data

textoutput db 'Hello world!', 10
lentext equ $ - textoutput
filetoopen db 'hi.txt'

文件名字符串需要一个0字节的终止符:filetoopen db 'hi.txt', 0

The filename string requires a 0-byte terminator: filetoopen db 'hi.txt', 0

section .text
global _start

_start:

mov eax, 5            ;open
mov ebx, filetoopen
mov ecx, 2            ;read and write mode

2open系统调用的O_RDWR标志.如果要创建尚不存在的文件,则还需要O_CREAT标志.如果指定O_CREAT,则需要第三个参数,即文件的权限模式.如果在C标头中四处查找,您会发现O_CREAT被定义为0100-注意前导零:这是一个八进制常量!您可以使用o后缀在nasm中编写八进制常量.

2 is the O_RDWR flag for the open syscall. If you want the file to be created if it doesn't already exist, you will need the O_CREAT flag as well; and if you specify O_CREAT, you need a third argument which is the permissions mode for the file. If you poke around in the C headers, you'll find that O_CREAT is defined as 0100 - beware of the leading zero: this is an octal constant! You can write octal constants in nasm using the o suffix.

因此,您需要类似mov ecx, 0102o的东西来获取正确的标志,而需要mov edx, 0666o来设置权限.

So you need something like mov ecx, 0102o to get the right flags and mov edx, 0666o to set the permssions.

int 80h

系统调用的返回代码在eax中传递.在这里,这将是文件描述符(如果打开成功)或一个小的负数,它是负的errno代码(例如,EPERM的-1).请注意,从原始系统调用返回错误代码的惯例与C系统调用包装器(通常返回-1并在出现错误的情况下设置errno)完全不同.

The return code from a syscall is passed in eax. Here, this will be the file descriptor (if the open succeeded) or a small negative number, which is a negative errno code (e.g. -1 for EPERM). Note that the convention for returning error codes from a raw syscall is not quite the same as the C syscall wrappers (which generally return -1 and set errno in the case of an error)...

mov eax, 4
mov ebx, filetoopen   ;I'm not sure what do i have to put here, what is the "file descriptor"?

...因此在这里,您需要先mov ebx, eax(要在eax被覆盖之前保存open结果),然后是mov eax, 4. (您可能要考虑先检查结果是否为阳性,如果不是,则以某种方式处理打开失败的问题.)

...so here you need to mov ebx, eax first (to save the open result before eax is overwritten) then mov eax, 4. (You might want to think about checking that the result was positive first, and handling the failure to open in some way if it isn't.)

mov ecx, textoutput
mov edx, lentext

在这里缺少int 80h.

mov eax, 1
mov ebx, 0
int 80h              ; finish without errors

这篇关于如何在汇编器中打开文件并对其进行修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆