Assembly 汇编代码中的系统调用
我写了一个小程序来理解系统调用。在以下内容中,您将找到create(…)和write(…)。这应该很容易。因此,正如您可能猜到的,程序所做的是首先创建一个文件,然后用文本填充它。它也应该是位置独立的代码。因此,我使用“jump call pop”组合来编写它 所以,它看起来像:Assembly 汇编代码中的系统调用,assembly,system-calls,Assembly,System Calls,我写了一个小程序来理解系统调用。在以下内容中,您将找到create(…)和write(…)。这应该很容易。因此,正如您可能猜到的,程序所做的是首先创建一个文件,然后用文本填充它。它也应该是位置独立的代码。因此,我使用“jump call pop”组合来编写它 所以,它看起来像: Section .text global _start _start: jmp short GoToFileName fileCreation:
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'
当我编译它时,链接它,然后运行它…在/tmp目录中什么都没有发生。仅创建该文件。但它是空的。
作为比较,我用C编写了相同的代码。在/tmp中,突然创建了一个file.txt文件。但是使用汇编代码是行不通的。
另一点是:
当我只使用创建部分(fileCreation)时,程序集就可以工作了。但是当我把它和写部分(复制过程)结合起来时,整个过程就不起作用了。因此,我假设我的错误在copyProcess部分的某个地方。但是我找不到它
有人能帮我吗?您的代码有一些错误:
0777
是十六进制0x1ff
create()
而不是open(…,O_create)
您无法处理文件已经存在的情况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吗
答复
- 您在此处进行两次系统调用:
采用以NULL结尾的文件名open()
采用字节数组和长度write()
- 空终止仅适用于
open()
不关心空终止,因为它只是尝试写入长度字节write()
0777
是十六进制0x1ff
create()
而不是open(…,O_create)
您无法处理文件已经存在的情况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吗
答复
- 您在此处进行两次系统调用:
采用以NULL结尾的文件名open()
采用字节数组和长度write()
- 空终止仅适用于
open()
不关心空终止,因为它只是尝试写入长度字节write()
0777
是十六进制0x1ff
create()
而不是open(…,O_create)
您无法处理文件已经存在的情况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吗
答复
- 您在此处进行两次系统调用:
采用以NULL结尾的文件名open()
采用字节数组和长度write()
- 空终止仅适用于
open()
不关心空终止,因为它只是尝试写入长度字节write()
0777
是十六进制0x1ff
create()
而不是open(…,O_create)
您无法处理文件已经存在的情况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吗
答复
- 您在此处进行两次系统调用:
采用以NULL结尾的文件名open()
采用字节数组和长度write()