Author Topic: I get segmentation error?  (Read 9038 times)

Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
I get segmentation error?
« on: February 24, 2013, 12:53:33 AM »
Code: [Select]
segment .data

alert1 db "warning", 0xA, 0XD
len4 equ $ -msg

alert2 db "danger", 0xA, 0XD
len5 equ $ -msg


I tried to run an assembly program, but I get segmentation error

I don't know what causes it.. What might be the possible causes?

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: I get segmentation error?
« Reply #1 on: February 24, 2013, 01:00:01 AM »
I see data, but no code.

Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: I get segmentation error?
« Reply #2 on: February 24, 2013, 02:05:15 AM »
Code: [Select]
mov eax, 3
mov ebx, 0
mov ecx, id
mov edx, 10
int 0x80

cmp ecx, 50
jle done
cmp ecx, 100
jge done
mov eax, 4
mov ebx, 1
mov ecx, warning
mov edx, len4
int 0x80

done: mov eax, 1
xor ebx, ebx
int 0x80

Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: I get segmentation error?
« Reply #3 on: February 24, 2013, 02:38:19 AM »
Code: [Select]
; "Hello World!" in 32 bit Linux NASM
; adapted from http://asm.sourceforge.net/intro/hello.html by Mark Loiseau
; referenced in http://blog.markloiseau.com/2012/04/hello-world-NASM-Linux

global _start ; global entry point export for ld

section .text
_start:

; sys_write(stdout, message, length)

mov eax, 4 ; sys_write syscall
mov ebx, 1 ; stdout
mov ecx, message ; message address
mov edx, length ; message string length
int 80h

; sys_exit(return_code)

mov eax, 1 ; sys_exit syscall
mov ebx, 0 ; return 0 (success)
int 80h

section .data
message: db 'Hello, world!',0x0A ; message and newline
length: equ $-message ; NASM definition pseudo-instruction

this also gives me a segmentation error


umm is it because of cygwin?

$ nasm -f elf -o hello32.o hello32.asm
$ ld -o hello32 hello32.o
$ ./hello32

I downloaded nasm and I downloaded gcc so I could use the ld command

What the??

help...

*I selected gcc for c++, but it seems it also downloaded gcc for all the other languages...
« Last Edit: February 24, 2013, 02:46:15 AM by maplesirop »

Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Re: I get segmentation error?
« Reply #4 on: February 24, 2013, 11:16:04 AM »
Hi maplesirop,

I think one error is that:

Code: [Select]
length: equ $-message

It should be:

Code: [Select]
length equ $-message

See, for example, http://www.nasm.us/doc/nasmdoc3.html#section-3.2.4

Gerhard

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
Re: I get segmentation error?
« Reply #5 on: February 24, 2013, 12:05:14 PM »
It worked fine in 32 bit linux for me.

If you are using 64 bit linux

you might need to try
$ nasm -felf32 hello32.asm
$ ld -melf_i386 -o hello32 hello32.o

If you are still getting ld: "unrecognised emulation mode:"  error.

You need to install the packages  libc6-i386  and libc6-dev-i386
Use the following commands in debian based OS.
$ sudo apt-get install libc6-i386
$ sudo apt-get install libc6-dev-i386

All the best

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: I get segmentation error?
« Reply #6 on: February 24, 2013, 02:20:23 PM »
Quote
umm is it because of cygwin?

Yeah, I think it probably is. I installed Cygwin, back when I was running Win98. Unfortunately, my memory's shot, and I don't remember what it did for me. I remember I didn't like it. I remember it was painfully slow. I think it gave us "all the power and stability of the Windows OS with the user-friendly Unix interface". :) In other words, the worst of both worlds. I think it was for writing Windows programs while pretending you were in Linux. I don't remember for sure whether it would actually run a Linux program - I don't think so.

Some of the code you've posted hasn't really been "complete" - no errors showing, but possibly something in the "not shown" parts. The "hello world" example from Mark Loiseau should be rock solid. I don't think the colon in "length:" is a problem, although I would not put one there as a matter of "taste". I'm blaming Cygwin.

I don't know whether the best solution for you is to write Windows programs or run Linux. I think there's a thing called "andlinux" that should work, or any emulator with Linux running on it. When you're first learning asm, you really don't need any "extra" confusion! So shoot for whatever you find least confusing...

It is probably premature to discuss this before you've solved the segmentation fault, but some of your code...
Code: [Select]
mov eax, 3
mov ebx, 0
mov ecx, id
mov edx, 10
int 0x80

cmp ecx, 50
jle done
cmp ecx, 100
jge done
; more...
You've put the address of "id" in ecx - a number like 0x8049123 or so. Doing the int 0x80 will return the number of characters entered in eax, but shouldn't alter any other registers (I don't know if this is "guaranteed", but it doesn't). So ecx is always going to be greater than 100, and your conditional jumps aren't going to do anything useful. As you seem to suspect, I think you want to convert the entered text to a number. I'll try to get to that question soon.

As Betov used to say, "Courage!"

Best,
Frank


Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: I get segmentation error?
« Reply #7 on: February 24, 2013, 04:58:03 PM »
thanks

do i have to install nasm on andLinux to run assembly programs compiled on cygwin?

Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: I get segmentation error?
« Reply #8 on: February 24, 2013, 07:34:53 PM »
I can't use andLinux.

I can't even navigate into my Windows folders using Mamba, nor I can get access to any file when I am using CoFs.