NASM - The Netwide Assembler
NASM Forum => Using NASM => Topic started by: maplesirop on February 24, 2013, 12:53:33 AM
-
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?
-
I see data, but no code.
-
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
-
; "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...
-
Hi maplesirop,
I think one error is that:
length: equ $-message
It should be:
length equ $-message
See, for example, http://www.nasm.us/doc/nasmdoc3.html#section-3.2.4 (http://www.nasm.us/doc/nasmdoc3.html#section-3.2.4)
Gerhard
-
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
-
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...
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
-
thanks
do i have to install nasm on andLinux to run assembly programs compiled on cygwin?
-
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.