Author Topic: beginner to Nasm - what's a segmentation fault, and how can I fix it?  (Read 17563 times)

Offline EGD Eric

  • New Member
  • Posts: 1
Hi, I've searched for information on segmentation faults, but haven't found much applicable to my situation, so I was wondering if there was something obvious in my hello world here that is causing a segmentation fault that you could see.

This program asks for the user's name, then says: "hello", then the user's name. Pretty fancy, here it is:
Code: [Select]


Segment .text

global _start
_start:

mov eax, 4
mov ebx, 1    
mov ecx, prompt1
mov edx, len1
int 0x80 ;print: "plz enter your name"

mov eax, 3
mov ebx, 0
mov ecx, name
mov edx, 200
int 0x80 ;get input,

mov esi, eax
mov eax, 4
mov ebx, 1
mov ecx, prompt2
mov edx, len2
int 0x80

mov ecx, name
mov edx, esi
int 0x80

Segment .data
prompt1 db "plz Enter your name", 10
len1 equ $-prompt1     ;length of the string: $-prompt1
prompt2 db "Hello "  
len2 equ $-prompt2

Segment .bss

name resb 200



What it ends up doing is just asking for your name, then once you give it, it says: hello segmentation fault.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner to Nasm - what's a segmentation fault, and how can I fix it?
« Reply #1 on: March 27, 2010, 02:41:26 AM »
What's the very first sys_call? Hint: you should do it last! :)

There's another problem, though...

Quote
Code: [Select]
...
mov esi, eax  ; save length of input

mov eax, 4
mov ebx, 1
mov ecx, prompt2
mov edx, len2
int 0x80

; at this point, ebx is still STDOUT, but eax is number
; of bytes printed - reload it with sys_write.

mov ecx, name
mov edx, esi
int 0x80
...

Best,
Frank