I want to write a text to a file that i take from argv[1] but for some reason, it returns this Error:
[1] 5590 segmentation fault (core dumped) ./hey hey.txt
Here is my code:
.section .data
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ SYS_WRITE, 4
.equ SYS_READ, 3
.equ SYS_EXIT, 1
.equ LINUX_SYSCALL, 0X80
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2
.equ O_CREATE_WRITE, 03101
.equ TEXT_SIZE, 17
text:
.ascii "hey diddle diddle\0"
.section .text
.globl _start
_start:
pushl %ebp
#reserve memory for file descriptor
subl $4, %esp
#opening file we want to write to
movl $SYS_OPEN, %eax
movl 8(%ebp), %ebx #8(%ebp) is the argv[1] that's where we take our filename
movl $O_CREATE_WRITE, %ecx
movl $0666, %edx
int $LINUX_SYSCALL
movl %eax, -4(%ebp) #store file descriptor
_write_text:
# making the write syscall
movl $SYS_WRITE, %eax
movl -4(%ebp), %ebx #file descriptor in %ebx
movl $text, %ecx #Addres of text
movl $TEXT_SIZE, %edx
int $LINUX_SYSCALL
_exit:
#close the file
movl $SYS_CLOSE, %eax
movl -4(%ebp), %ebx #file dexcriptor to close
int $LINUX_SYSCALL
#exit
movl $SYS_EXIT, %eax
movl $0, %ebx
int $LINUX_SYSCALL
this is how I assembled and linked:
➜ as --32 hey.s -o hey.o ➜ ld -m elf_i386 hey.o -o hey ➜ ./hey hey.txt
what's the problem here and how can I give the filename to it directly in a string, not in argv?