Results 1 to 5 of 5

Thread: Segfault in NASM program.

  1. #1
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Segfault in NASM program.

    Hello everyone. I am learning NASM so I challenged myself to write a program that print the first letter of the command line arguments.

    So far, if I provide arguments, it prints the first letter of the first argument and then segfaults.
    If I provide none, it does nothing.

    So what is wrong with this code? Am I missing something?
    Code:
    section .text
        global _start
    _start:
        pop esi            ;get number of args
        pop eax            ;remove the program name from the stack
        dec esi            ;decrement nr of args (we no longer have the progam name)
    
        jmp printArgs        ;start printing args
        
    exit:                ;exit label
        mov eax,1        ;move exit in eax
        mov ebx,0        ;move return code 
        int 80h            ;kernel interrupt
        
    printArgs:
        cmp esi, 0        ;compare esi and 0
        ja printNextArg        ;if esi above, jump printNextArg
        jmp exit        ;if not, jump exit
        
        
    printNextArg:            ;printNextArg label
        jmp printArg        ;printArg
        dec esi            ;decrement esi
        jmp printArgs        ;loop
        
    printArg:            ;printArg label
         mov eax, 4        ;move print in eax
         mov ebx, 1        ;move stdout in ebx
         pop ecx            ;move next argument on stack in ecx
         mov edx, 1        ;move string length
         int 80h            ;kernel interrupt
    Last edited by cgroza; October 18th, 2011 at 12:09 AM.
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  2. #2
    Join Date
    Jun 2010
    Location
    ~
    Beans
    Hidden!

    Re: Segfault in NASM program.

    Quote Originally Posted by cgroza View Post
    So what is wrong with this code? Am I missing something?
    Code:
    printArg:            ;printArg label
         mov eax, 4        ;move print in eax
         mov ebx, 1        ;move stdout in ebx
         pop ecx            ;move next argument on stack in ecx
         mov edx, 1        ;move string length
         int 80h            ;kernel interrupt
    when you exec 'printArg', you don't automatically return from the function. try adding 'ret' to the end

    And your only writing 1 Char to stdout with 'mov edx, 1'
    Last edited by emiller12345; October 18th, 2011 at 03:27 AM.
    CADWEB Advance Toolkit Utility: http://cad.webatu.com/
    Homesite: http://digitalmagican.comze.com/

  3. #3
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Segfault in NASM program.

    "ret" works best if you've used something like "call" that pushes a return address on the stack. If I've understood the code properly, a little bit of rearranging as follows might work better:

    Code:
    printNextArg:            ;printNextArg label
      jmp printArg        ;printArg
        dec esi            ;decrement esi
        jmp printArgs        ;loop
       
    printArg:            ;printArg label
         mov eax, 4        ;move print in eax
         mov ebx, 1        ;move stdout in ebx
         pop ecx            ;move next argument on stack in ecx
         mov edx, 1        ;move string length
         int 80h            ;kernel interrupt
    
         dec esi            ;decrement esi
        jmp printArgs        ;loop
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  4. #4
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Segfault in NASM program.

    Still getting the same result. I tried calling it instead of jumping into printArg, it just displayed an N and then segfaulted.

    Here is the new printArg:
    Code:
    printArg:                       ;printArg label
            mov eax, 4              ;move print in eax
            mov ebx, 1              ;move stdout in ebx
            pop ecx                 ;move next argument on stack in ecx
            mov edx, 1              ;move string length
            int 80h                 ;kernel interrupt
            ret
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  5. #5
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Segfault in NASM program.

    Thanks Lisati, your solution worked, although I still do not understand what was wrong and what is right now.
    Here is the new full code:

    Code:
    section .text
            global _start
    _start:
            pop esi                 ;get number of args
            pop eax                 ;remove the program name from the stack
            dec esi                 ;decrement nr of args (we no longer have the progam name)
    
            jmp printArgs           ;start printing args
            
    exit:                           ;exit label
            mov eax,1               ;move exit in eax
            mov ebx,0               ;move return code 
            int 80h                 ;kernel interrupt
            
    printArgs:
            cmp esi, 0              ;compare esi and 0
            ja printArg             ;if esi above, jump printNextArg
            jmp exit                ;if not, jump exit
            
    printArg:            ;printArg label
         mov eax, 4        ;move print in eax
         mov ebx, 1        ;move stdout in ebx
         pop ecx            ;move next argument on stack in ecx
         mov edx, 1        ;move string length
         int 80h            ;kernel interrupt
    
         dec esi            ;decrement esi
         jmp printArgs        ;loop
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •