我写了一个小程序来理解系统。在下面你会发现creat(.)写(.)。这应该很容易。所以,正如您可能猜到的,程序所做的是首先创建一个文件,然后用文本填充它。它也应该是位置独立的代码。因此,我写它使用“跳叫-流行”组合。
所以,看起来:
Section .text
global _start
_start:
jmp short GoToFileName
fileCreation:
pop esi ; address of file name
xor eax, eax ; clear eax
mov byte [esi+13], al ; terminate file name string with NULL
mov cx, 0x309 ; create file with all possible permissions
mov ebx, esi ; ebx gets address of file name
mov al, 0x8 ; al gets syscall number 8 of creat()
int 0x80 ; create the file
mov edx, eax ; write resulting file descriptor in edx
jmp short TextInput
copyProcess:
pop esi ; address of input string
xor eax, eax ; clear eax
mov byte [esi+23], al ; terminate input string with NULL
mov dl, 0x17 ; dl gets number of bytes to write
mov ecx, esi ; ecx gets address of input string
mov ebx, edx ; ebx gets file descriptor of created file
mov al, 0x4 ; al gets syscall number of write
int 0x80 ; write that input string
GoToFileName:
call fileCreation
db '/tmp/file.text'
TextInput:
call copyProcess
db 'This the output file'当我编译它时,链接它,然后运行it...nothing发生/tmp目录。只创建文件。但它是空的。作为比较,我用C语言编写了相同的代码。在/tmp中,突然创建了一个file.txt。但是使用程序集代码它不起作用。另一点是:当我只接受创建部分(FileCreation)时,程序集就可以工作了。但是,当我将它与写部分(CopyProcess)结合时,整个系统就不能工作了。因此,我假设我的错误在copyProcess部分的某个地方。但我找不到。
有人能帮我吗?
发布于 2015-03-06 21:57:03
您的代码有一些bug:
0777是十六进制0x1ff。create()而不是open(..., O_CREAT),您无法处理文件已经存在的情况。write()。此代码适用于:
Section .text
global _start
_start:
jmp short GoToFileName
open:
pop esi ; address of file name
mov edx, 0x1ff ; 0777
mov ecx, 0x41 ; O_CREAT | O_WRONLY
mov ebx, esi ; ebx gets address of file name
mov eax, 0x5 ; SYS_open
int 0x80
mov ebx, eax ; write resulting file descriptor in EBX
jmp short TextInput
write:
pop esi ; address of input string
mov edx, 20 ; edx gets number of bytes to write
mov ecx, esi ; ecx gets address of input string
mov eax, 0x4 ; SYS_write
int 0x80
exit:
mov ebx, 0
mov eax, 1
int 0x80
GoToFileName:
call open
db '/tmp/file.text',00
TextInput:
call write
db 'This the output file'跟进问题
当我在字符串末尾添加NULL -结束符时,必须也计算NULL吗?
回答
open()接受以空结尾的文件名writ()采用字节数组和长度
open()。write()不关心空终止,因为它只是尝试写入长度字节。https://stackoverflow.com/questions/28907647
复制相似问题