Click here to Skip to main content
15,942,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why does my code have a segmentation fault? How can I solve it?
Please help!

ASM
section .data
    hello     db    'Hello, world!',10  ; Our dear string
    helloLen  equ   $ - hello       ; Length of our dear string
        fileName db '/home/fateme/Desktop/text.txt',0
        handler  dd   10                         ;baraye negah dashtane filedescriptor
section .bss
        fileContent  resb   100000   ;mohtaviyate file o tu en mirizim va chon nemidunim file cheghade eno ye adade bozorg midim
section .text
    global _start
_start:
    pop ebx     ; argc (argument count)
    pop ebx     ; argv[0] (argument 0, the program name)
    pop ebx     ; The first real arg, a filename
    mov eax,8       ; The syscall number for creat() (we already have the filename in ebx)
        mov     ebx,fileName
    mov ecx,00644Q  ; Read/write permissions in octal (rw_rw_rw_)
    int 80h     ; Call the kernel
                ; Now we have a file descriptor in eax
    test    eax,eax     ; Lets make sure the file descriptor is valid
    js  skipWrite   ; If the file descriptor has the sign flag
                ;  (which means it's less than 0) there was an oops,
                ;  so skip the writing. Otherwise call the filewrite "procedure"
    call    fileWrite
        call    readfile
        call    write
Exit:
         mov eax,1
         mov ebx,0
         int 80h

skipWrite:
    mov ebx,eax     ; If there was an error, save the errno in ebx
    mov eax,1       ; Put the exit syscall number in eax
    int 80h     ; Bail out
; proc fileWrite - write a string to a file
fileWrite:
        mov     [handler],eax      ;azin b bad filedescriptor dar handler ast
    mov ebx,[handler]   ; sys_creat returned file descriptor into eax, now move into ebx
    mov eax,4       ; sys_write
                ; ebx is already set up
    mov ecx,hello   ; We are putting the ADDRESS of hello in ecx
    mov edx,helloLen    ; This is the VALUE of helloLen because it's a constant (defined with equ)
    int 80h
    ;mov    eax,6       ; sys_close (ebx already contains file descriptor)
    ;int    80h
; endp fileWrite
;readfile to file content: file ro mikhune va mohtaviyate uno tu fileContent mirize
readfile:
    mov edx,100000
    mov ecx,fileContent
    mov ebx,[handler]
    mov eax,3
    int 80h
write:
    mov edx,eax    ;dar eax tedade bytehaEye k dar "readfile to filecontent" az file khunde
    mov ecx,fileContent
    mov ebx,1   ;this determines file descriptor or output that here is stdout to display output on monitor
    mov eax,4   ;function call
    int 80h
Posted
Updated 13-Jul-10 2:19am
v3
Comments
Richard MacCutchan 13-Jul-10 7:46am    
You could start by explaining which instruction causes the fault.

A segmentation fault means that you've made the memory protection systems think that something is wrong. Generally, it means that you're trying to access an unmapped address, or passing a wrong address to a system call. Work out where it's faulting, and check what you're supposed to provide to the system call it faults on.
 
Share this answer
 
I'm not sure how much help you're going to be able to get here, (this is for Linux, right?). I googled "asm segmentation fault", and got 116,000 hits.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900