flat assembler
Message board for the users of flat assembler.

Index > Linux > What's causing this seg fault?

Author
Thread Post new topic Reply to topic
Ashy



Joined: 26 May 2004
Posts: 3
Ashy 26 May 2004, 08:36
I have written a program (with help from example code) that prints out the arguments (minus the program name) passed to it when run.
It seems to run fine except that it prints "Segmentation fault" just before it exits.
What does this mean?
Why is this happening?
Also what do i need to change in order for this to assemble with fasm?
(i guess that question should have come before the others Razz)

Here is the code in nasm format:
Code:
;prints the arguments passed to it when run
BITS 32
section .text
   global  _start

_start:
   pop     ebp     ;argc
       pop     ecx     ;get program name off stack
argument:
        dec     ebp     ;minus the last argument
    pop     ecx     ;get argument 
      mov     ebx,ecx
     xor     edx,edx
strlen:
      mov     al,[ebx];get a byte of the argument
 inc     edx     ;increment the char counter
 inc     ebx     ;increment the address ptr
  test    al,al   ;see if char was zero
       jnz     strlen
      mov byte [ebx-1],10     ;tag on a line break

;write to stdout    
    mov     eax,4   ;write syscall
      mov     ebx,1   ;to stdout
  int     80h     ;call kernel

;loop back to argument if there are any arguments left
  cmp     ebp,0   ;see if any arguments left
  jne     argument;get the next one if there is

;exit
  mov     eax,1   ;exit syscall
       mov     ebx,0   ;return success
     int     80h     ;call kernel
    
Post 26 May 2004, 08:36
View user's profile Send private message Reply with quote
binary108



Joined: 03 Apr 2004
Posts: 7
Location: Russia
binary108 26 May 2004, 12:04
May be you must write
_start:
pop ebp ;argc
dec ebp ;!!!!

But i'm not sure
Post 26 May 2004, 12:04
View user's profile Send private message Reply with quote
Ashy



Joined: 26 May 2004
Posts: 3
Ashy 26 May 2004, 13:06
That is happening anyway.
Are u saying that maybe the 'dec ebp' should come straight after 'pop ebp' ?
Would this make a difference?
Post 26 May 2004, 13:06
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 26 May 2004, 13:41
You were trying to display one more argument more than you had.

Here's the corrected version that can be compiled with fasm directly to executable:
Code:
format ELF executable

        pop     ebp     ;argc
        pop     ecx     ;get program name off stack
argument:
        dec     ebp     ;minus the last argument
        jz      exit
        pop     ecx     ;get argument
        mov     ebx,ecx
        xor     edx,edx
strlen:
        mov     al,[ebx];get a byte of the argument
        inc     edx     ;increment the char counter
        inc     ebx     ;increment the address ptr
        test    al,al   ;see if char was zero
        jnz     strlen
        mov byte [ebx-1],10     ;tag on a line break

;write to stdout
        mov     eax,4   ;write syscall
        mov     ebx,1   ;to stdout
        int     80h     ;call kernel

        jmp     argument

exit:
        mov     eax,1   ;exit syscall
        mov     ebx,0   ;return success
        int     80h     ;call kernel    


And here's the version which fasm will compile into an object file, which you can then link into final executable with gcc or ld command:
Code:
format ELF

section '.text' executable

        public  _start

_start:
        pop     ebp     ;argc
        pop     ecx     ;get program name off stack
argument:
        dec     ebp     ;minus the last argument
        jz      exit
        pop     ecx     ;get argument
        mov     ebx,ecx
        xor     edx,edx
strlen:
        mov     al,[ebx];get a byte of the argument
        inc     edx     ;increment the char counter
        inc     ebx     ;increment the address ptr
        test    al,al   ;see if char was zero
        jnz     strlen
        mov byte [ebx-1],10     ;tag on a line break

;write to stdout
        mov     eax,4   ;write syscall
        mov     ebx,1   ;to stdout
        int     80h     ;call kernel

        jmp     argument
exit:
        mov     eax,1   ;exit syscall
        mov     ebx,0   ;return success
        int     80h     ;call kernel    
Post 26 May 2004, 13:41
View user's profile Send private message Visit poster's website Reply with quote
Ashy



Joined: 26 May 2004
Posts: 3
Ashy 26 May 2004, 14:16
Thanks Privalov Very Happy
fasm seems to take the same syntax as nasm except for the section declerations. Is this correct?
I guess ill have a more in depth look at fasm.txt.
I didnt see anything about that when i perused it the first time around.
Post 26 May 2004, 14:16
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.