Linux 如何在汇编程序中打开文件并修改它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8312290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a file in assembler and modify it?
提问by Rama
I'm starting to learn Assembler and I'm working in Unix. I want to open a file and write 'Hello world' on it.
我开始学习汇编程序,我正在 Unix 中工作。我想打开一个文件并在上面写上“Hello world”。
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?
但是当我编译它时,它什么也没做。我究竟做错了什么?当我打开一个文件时,文件描述符值返回到哪里?
采纳答案by Matthew Slattery
This is x86 Linux (x86 is not the only assembly language, and Linux is not the only Unix!)...
这是 x86 Linux(x86 不是唯一的汇编语言,Linux 也不是唯一的 Unix!)...
section .data
textoutput db 'Hello world!', 10
lentext equ $ - textoutput
filetoopen db 'hi.txt'
The filename string requires a 0-byte terminator: filetoopen db 'hi.txt', 0
文件名字符串需要一个 0 字节的终止符: filetoopen db 'hi.txt', 0
section .text
global _start
_start:
mov eax, 5 ;open
mov ebx, filetoopen
mov ecx, 2 ;read and write mode
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.
2
是系统调用的O_RDWR
标志open
。如果您希望在文件不存在的情况下创建该文件,则还需要该O_CREAT
标志;如果指定O_CREAT
,则需要第三个参数,即文件的权限模式。如果您查看 C 头文件,您会发现它O_CREAT
被定义为0100
- 注意前导零:这是一个八进制常量!您可以nasm
使用o
后缀编写八进制常量。
So you need something like mov ecx, 0102o
to get the right flags and mov edx, 0666o
to set the permssions.
所以你需要一些类似的东西mov ecx, 0102o
来获得正确的标志并mov edx, 0666o
设置权限。
int 80h
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 notquite the same as the C syscall wrappers (which generally return -1
and set errno
in the case of an error)...
系统调用的返回码被传入eax
。在这里,这将是文件描述符(如果打开成功)或一个小的负数,它是一个负errno
代码(例如 -1 表示EPERM
)。请注意,从原始的系统调用返回错误代码的惯例是不完全一样的C系统调用包装(一般返回-1
并设置errno
在一个错误的情况下)...
mov eax, 4
mov ebx, filetoopen ;I'm not sure what do i have to put here, what is the "file descriptor"?
...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 ebx, eax
首先(open
在eax
被覆盖之前保存结果)然后mov eax, 4
。(您可能需要考虑首先检查结果是否为正,如果不是,则以某种方式处理无法打开的情况。)
mov ecx, textoutput
mov edx, lentext
Missing int 80h
here.
int 80h
这里不见了。
mov eax, 1
mov ebx, 0
int 80h ; finish without errors
回答by patthoyts
It depends what assembler you are using and if you expect to be using the C runtime or not. In this case which appears to be the Hello World text example from rosettacodethey are using nasm. Given you have a _start field you are not needing the C runtime so you assemble this to an elf object file and link it into a program:
这取决于您使用的汇编程序以及您是否希望使用 C 运行时。在这种情况下,这似乎是来自rosettacode的 Hello World 文本示例,他们使用的是 nasm。鉴于您有一个 _start 字段,您不需要 C 运行时,因此您将其组装到一个 elf 目标文件并将其链接到一个程序中:
nasm -felf hello.asm
ld hello.o -o hello
Now you can run the hello
program.
现在您可以运行该hello
程序。
A slightly more portable example that uses the C runtime to do the work rather than linux syscalls might look like the sample below. If you link this as described it can use printf
to do the printing.
使用 C 运行时而不是 linux 系统调用来完成工作的稍微更便携的示例可能类似于下面的示例。如果您按照描述链接它,它可以printf
用来进行打印。
;;; helloworld.asm -
;;;
;;; NASM code for Windows using the C runtime library
;;;
;;; For windows - change printf to _printf and then:
;;; nasm -fwin32 helloworld.asm
;;; link -subsystem:console -out:helloworld.exe -nodefaultlib -entry:main
;;; helloworld.obj msvcrt.lib
;;; For gcc (linux, unix etc):
;;; nasm -felf helloworld.asm
;;; gcc -o helloworld helloworld.o
extern printf
section .data
message:
db 'Hello, World', 10, 0
section .text
global main
main:
push dword message ; push function parameters
call printf ; call C library function
add esp, 4 ; clean up the stack
mov eax, 0 ; exit code 0
ret
For information about file descriptors - read the open(2)
manual page or look at wikipedia. It is how posix refers to an open i/o stream. In your case, stdout.
有关文件描述符的信息 - 阅读open(2)
手册页或查看维基百科。这就是 posix 如何引用开放的 i/o 流。在你的情况下,标准输出。
回答by Basile Starynkevitch
Did you read the Linux Assembly HOWTO? It covers your question.
你读过Linux Assembly HOWTO吗?它涵盖了您的问题。
You can also compile some C code with gcc -S -fverbose-asm -O1
and look at the generated assembly.
您还可以编译一些 C 代码gcc -S -fverbose-asm -O1
并查看生成的程序集。
At last, I don't think it is worth bothering a lot about assembler. The compiler will surely generate better code than what you could write.
最后,我认为不值得为汇编程序费心。编译器肯定会生成比你能写的更好的代码。
回答by Rommel Samanez
This is a x64 Linux sample
这是一个 x64 Linux 示例
; Program to open and write to file
; Compile with:
; nasm -f elf64 -o writeToFile64.o writeToFile64.asm
; Link with:
; ld -m elf_x86_64 -o writeToFile64 writeToFile64.o
; Run with:
; ./writeToFile64
;==============================================================================
; Author : Rommel Samanez
;==============================================================================
global _start
%include 'basicFunctions.asm'
section .data
fileName: db "testFile.txt",0
fileFlags: dq 0102o ; create file + read and write mode
fileMode: dq 00600o ; user has read write permission
fileDescriptor: dq 0
section .rodata ; read only data section
msg1: db "Write this message to the test File.",0ah,0
msglen equ $ - msg1
msg2: db "File Descriptor=",0
section .text
_start:
mov rax,2 ; sys_open
mov rdi,fileName ; const char *filename
mov rsi,[fileFlags] ; int flags
mov rdx,[fileMode] ; int mode
syscall
mov [fileDescriptor],rax
mov rsi,msg2
call print
mov rax,[fileDescriptor]
call printnumber
call printnewline
; write a message to the created file
mov rax,1 ; sys_write
mov rdi,[fileDescriptor]
mov rsi,msg1
mov rdx,msglen
syscall
; close file Descriptor
mov rax,3 ; sys_close
mov rdi,[fileDescriptor]
syscall
call exit