Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a directory in linux assembly language

I am trying to create a small assembly program to create a folder. I looked up the system call for creating a directory on this page. It says that it is identified by 27h. How would I go about implementing the mkdir somename in assembly?

I am aware that the program should move 27 into eax but I am unsure where to go next. I have googled quite a bit and no one seems to have posted anthing about this online.

This is my current code (I don't know in which register to put filename and so on):

section .data

section .text
global _start

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

Thanks

like image 715
Jayson Kane Avatar asked Sep 17 '25 05:09

Jayson Kane


1 Answers

One way of finding out, is using GCC to translate the following C code:

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

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

    return 0;
}

to assembly, with: 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

Anyway, ProgrammingGroundUp page 272 lists important syscalls, including 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.
like image 161
karlphillip Avatar answered Sep 19 '25 02:09

karlphillip