簡體   English   中英

用Linux匯編語言創建目錄

[英]Creating a directory in linux assembly language

我正在嘗試創建一個小的匯編程序來創建一個文件夾。 我在此頁面上查找了用於創建目錄的系統調用。 它說到27h被識別。 我將如何在程序mkdir somename實現mkdir somename

我知道該程序應該將27轉換為eax,但是我不確定下一步該怎么做。 我在Google上搜索了很多,似乎沒有人在網上發布過有關此內容的內容。

這是我當前的代碼(我不知道在哪個寄存器中放置文件名,依此類推):

section .data

section .text
global _start

mov eax, 27
mov ????????
....
int 80h

謝謝

一種發現的方法是使用GCC轉換以下C代碼:

#include <stdio.h>
#include <sys/stat.h>

int main()
{
    if (mkdir("testdir", 0777) != 0)
    {
        return -1;
    }

    return 0;
}

要組裝,請使用: gcc mkdir.c -S

    .file   "mkdir.c"
    .section    .rodata
.LC0:
    .string "testdir"
    .text
.globl main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $16, %esp
    movl    $511, 4(%esp)
    movl    $.LC0, (%esp)
    call    mkdir           ; interesting call
    testl   %eax, %eax
    setne   %al
    testb   %al, %al
    je  .L2
    movl    $-1, %eax
    jmp .L3
.L2:
    movl    $0, %eax
.L3:
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
    .section    .note.GNU-stack,"",@progbits

無論如何, ProgrammingGroundUp頁面272列出了重要的系統調用,包括mkdir

%eax   Name    %ebx                 %ecx       %edx    Notes
------------------------------------------------------------------
39     mkdir   NULL terminated    Permission           Creates the given
               directory name                          directory. Assumes all 
                                                       directories leading up 
                                                       to it already exist.

您也可以按照大會指導手冊的建議進行操作。 但是確實,從Libc調用mkdir更方便。 您需要查看asm/unistd.h以獲得系統調用號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM