yasm compile error

quandary82 at hailmail.net quandary82 at hailmail.net
Mon Jul 7 02:06:52 PDT 2008


2 errors:

| yasm --version
| yasm 0.5.0.1591
| Compiled on Mar 28 2007.


If I compile the below program with: 
as hello.asm -o hello.o
ld hello.o -o hello

then it compiles and works fine.

If I try to use yasm, which should be compatible to GAS syntax:
yasm -p gas -f elf32 hello.asm -o hello.o
ld -o hello hello.o

I only get the following error:
hello.asm:2: label or instruction expected at start of line
What does that mean? I don't get it, what shall be incorrect ???
Code:
------------------------------------------------------------------
.section .data
 mystring: .ascii "Hello World!\n"
.section .text
.globl _start
_start:
    mov $4, %eax        # Syscall No. 4: = 'write'
    mov $1, %ebx        # File Descriptor
    mov $mystring, %ecx # Memory address of string
    mov $13, %edx       # strlen
    int $0x80           # call eax (=write), argument is memory address & strlen
    mov $1, %eax        # Syscall No. 1 = 'exit'
    mov $0, %ebx        # clear ebx
    int $0x80           # call eax (=exit), return value in ebx
------------------------------------------------------------------------------

Hmm... then I changed this:
---------------------------------------
.section .data 
 mystring: .ascii "Hello World!\n"  
---------------------------------------
 
to this:

---------------------------------------
.section .data 
 mystring: 
 .ascii "Hello World!\n"  
---------------------------------------

...and it assembled... But 'readelf' shows me that it didn't set the
section flags correctly... so it won't execute...


**********************************************
Another error with this program:

-------------------------------------------------------
# as useglibc.gas -o useglibc.o
# ld --dynamic-linker /lib/ld-linux.so.2 /lib/libc.so.6 useglibc.o -o
useglibc

# http://linuxreviews.org/man/ld.so/
# root at all:/lib# ls | grep "libc.so"
# libc.so.6
# root at all:/lib# ls | grep "ld-linux*"
# ld-linux.so.2


.section .data
msg: 
.ascii "Hello World!\n\0"
.section .text
.globl _start
_start:
        # Textausgabe mit der Funktion "printf" - kennt jeder ;).
        pushl $msg
        call printf
        # Nun muss der Stackpointer zurückgesetzt werden, indem
        # ich 4 Bytes (die Größe der Adresse msg) addiere
        addl $4, %esp
        # Programm beenden mit der Funktion "exit".
        # 1. und einziger Parameter: Rueckgabewert
        pushl $0
        call exit
--------------------------------------------------------------------
Output:
~/Desktop# yasm -p gas -f elf32 useglibc.gas -o bla.o
root at all:~/Desktop# ld
--dynamic-linker /lib/ld-linux.so.2 /lib/libc.so.6 bla.o -o bla
root at all:~/Desktop# ./bla
Segmentation fault
root at all:~/Desktop# as useglibc.gas -o bla.o
root at all:~/Desktop# ld
--dynamic-linker /lib/ld-linux.so.2 /lib/libc.so.6 bla.o -o bla
root at all:~/Desktop# ./bla
Hello World!
root at all:~/Desktop# 


*********************************************************

Another thing is this one:
GAS assembly produced by the GNU c compiler:
compile bla.c with: gcc -S bla.c
------------------------------
#include <stdlib.h>

// # gcc -S bla.c


int main()
{
	write(0, "Hello Nixcoders.org!\n", 21);
}
-----------------------------------------------------

root at all:~/Desktop# gcc -S bla.c
root at all:~/Desktop# as bla.s -o bla.o
root at all:~/Desktop# ld
--dynamic-linker /lib/ld-linux.so.2 /lib/libc.so.6 bla.o -o bla
ld: warning: cannot find entry symbol _start; defaulting to
00000000080481a4
root at all:~/Desktop# ./bla
Hello Nixcoders.org!
Segmentation fault

root at all:~/Desktop# yasm -p gas -f elf32 bla.s -o bla.o
root at all:~/Desktop# ld
--dynamic-linker /lib/ld-linux.so.2 /lib/libc.so.6 bla.o -o bla
ld: warning: cannot find entry symbol _start; defaulting to
00000000080481b0
root at all:~/Desktop# ./bla
Segmentation fault
root at all:~/Desktop# 


And note that changing every occurrence of main with _start won't solve
the problem...
PS: It should also recognize main as start of a program, since it is
convenient to compile first from C to gas assembly, and then modify it. 




More information about the yasm-devel mailing list