首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >汇编代码中的Syscalls

汇编代码中的Syscalls
EN

Stack Overflow用户
提问于 2015-03-06 21:05:44
回答 1查看 1.1K关注 0票数 0

我写了一个小程序来理解系统。在下面你会发现creat(.)写(.)。这应该很容易。所以,正如您可能猜到的,程序所做的是首先创建一个文件,然后用文本填充它。它也应该是位置独立的代码。因此,我写它使用“跳叫-流行”组合。

所以,看起来:

代码语言:javascript
复制
   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部分的某个地方。但我找不到。

有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2015-03-06 21:57:03

您的代码有一些bug:

  1. 您正在破坏存储在EDX中的文件描述符。
  2. 在x86上,写入DL并不清楚DH,写入DX并不清除EDX的上半部分。
  3. 文件权限以八进制形式指定,即基数为8。因此八进制0777是十六进制0x1ff
  4. 通过调用create()而不是open(..., O_CREAT),您无法处理文件已经存在的情况。
  5. 最好是预NULL-终止文件名字符串,因为文本部分通常是不可写的。我知道你在尝试编写外壳代码,但这一点仍然有效。
  6. 您将错误的缓冲区长度传递给write()

此代码适用于:

代码语言:javascript
复制
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()不关心空终止,因为它只是尝试写入长度字节。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28907647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档