You are not logged in.

#1 2008-01-02 11:12:11

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

compiling assembly under arch64

Hi @all

I am trying to write an assembly program, ok, let's say I am learning assembly.
But I ran into trouble.
I do not get my code compiled. I tried it with gas and with yasm (nasm) but the result stays the same.
All I get is an: Segmentation fault ,or ./test not found.

So this is my simple code:

section .data
    msg    db "Hello World",10,0
    
section .text
 global main
 extern printf

main:
    push     msg
    call    printf
_exit:    
    mov     rbx,0 ; return value for exit
    mov    rax,1 ; system call to exit
    int    0x80    ; calling the kernel

Ok, I called the entry point main, because when compiling with gcc I need this, else I get errors, complaining
about the missing of the function main.
So this is my code to compile it:

yasm -p nasm -f elf64 -o test.o test.asm
gcc -lc -o test test.o
./test
Segmentation falut

So what is wrong ? If i compile it with these arguments:

yasm -p nasm -f elf64 -o test.o test.asm 
ld --dynamic-linker /lib/libc.so.6 -lc test.o -o test
./test
bash: ./test not found

So what's wrong with it? I don't see it, and I did not find anything on the net, as some compile it with ld, some with gcc.
But I don't get it work.

Offline

#2 2008-01-02 21:08:10

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

Re: compiling assembly under arch64

Ok after spending hour after hour to get this fucking example work, I had a look at the assembly
of gcc (gcc -S <file.c> ), with an input file, which does excatly what I wanted the assembly code to do.

It seems as the base of the problem was, that printf handles strings as 32 bit, but the stack hold 64 bit values.
Why the fuck is this not mentioned anywhere. This is weird. mad
And I do not know why I have to put the 0 on eax. This makes no sense. hmm
Hopefully somebody can explain me.

/*
in C this is:
#include <stdio.h>

int main()
{
 printf("Hello World\n");
 return 0;
}
*/

// Compile: gcc -o test test.s

.section .data
    msg:     .string  "Hello World\n"

.section .text
 .extern printf

 .global main

main:
// moving the beginning of string to edi
    movl        $msg, %edi
// moving 0 to eax
    movl        $0, %eax
// calling printf
    call        printf


// moving return code 0 to rbx
    movq     $0, %rbx
// moving syscall number 1 to rax ( exit )
    movq     $1, %rax
// calling the kernel
    int    $0x80

Last edited by LE_Shatai (2008-01-02 21:09:07)

Offline

#3 2008-01-16 00:29:18

AbacusMonkey
Member
Registered: 2007-11-01
Posts: 7

Re: compiling assembly under arch64

Assembly hey? Next step binary programming! Hahaha,

Sorry I can't be of more help.

Offline

#4 2008-01-16 08:07:51

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: compiling assembly under arch64

Why not just use system calls?

Offline

#5 2008-01-17 20:27:49

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

Re: compiling assembly under arch64

AbacusMonkey wrote:

Assembly hey? Next step binary programming! Hahaha,

This is what assembly actually is, justing using the textual representations in form of mnemonics. wink

Gilneas wrote:

Why not just use system calls?

Actually I was to lazy writing a >printf<, so why invent the wheel again and not using the existing functions.
But short after I posted my correction, I noticed that this solution would only work with my computer, as gcc
assembles it different with every version, an maybe computer too, so this is a terrible solution.

I would have to write my own printf in assembly. But as I am interested in knowing how to write a interpreter,
how the computer works on binary level, how to interact with registers (values, pointers) stacks, how this
fucking computer really works on a very low level, I am writing an small virtual machine for myself, with a very little
programming language, which will then be compiled to pseudo-assembly etc.

Ok, so far I have to prepare for tests, so no asembly 'til finished them. wink

Offline

#6 2008-01-17 20:43:14

X/ax
Member
From: Oost vlaanderen, Belgium
Registered: 2008-01-13
Posts: 275
Website

Re: compiling assembly under arch64

LE_Shatai: would you mind giving the resource in which you are learning the assembly in linux? Thanks in advance.


My coding blog (or an attempt at it)
Archer start page (or an attempt at it)

Offline

#7 2008-01-17 22:10:21

Oxyd
Member
From: Czech Republic
Registered: 2008-01-17
Posts: 167

Re: compiling assembly under arch64

LE_Shatai wrote:

Ok after spending hour after hour to get this fucking example work, I had a look at the assembly
of gcc (gcc -S <file.c> ), with an input file, which does excatly what I wanted the assembly code to do.

It seems as the base of the problem was, that printf handles strings as 32 bit, but the stack hold 64 bit values.
Why the fuck is this not mentioned anywhere. This is weird. mad
And I do not know why I have to put the 0 on eax. This makes no sense. hmm
Hopefully somebody can explain me.

I would gess that the parameter is passed via edi instead of rdi only as an optimisation — I doubt printf does some 32 bit magic on a 64-bit architecture.

However, printf is a variadic argument function — it can take any arbitrary number of arguments, some of which may be floating point values.  Floating point values are passed via SSE registers and the ABI requires that the number of SSE registers used is passed in eax if the function called is variadic.  You are passing no floating point value to printf, so you need to reset eax to zero.  Here you can find more information on the AMD64 ABI.

Offline

#8 2008-01-18 07:18:59

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

Re: compiling assembly under arch64

X/ax wrote:

LE_Shatai: would you mind giving the resource in which you are learning the assembly in linux? Thanks in advance.

Actually I would do, but I do not know what do mean with >the resource in which you are learning the assembly in linux< .
If you mean some websites, I can give you just these ones:

http://www.ibm.com/developerworks/libra … -nasm.html
http://sourceware.org/binutils/docs-2.18/as/index.html
http://en.wikibooks.org/wiki/X86_Assembly
http://www.tortall.net/projects/yasm/wiki
http://home.comcast.net/~fbui/intel.html
http://developer.intel.com/design/
http://www.x86-64.org/documentation/abi.pdf - Thanks to Oxyd

At the Intel website you have to search your own, it much to big, but they have all the information you need
as downloadable PDF.

Actually I use gas which uses the AT&T syntax, but most of the assemblers out there use the Intel syntax.
The main difference is - besides some others -

Intel: mov   target, origin
AT&T:  mov   origin, target
Oxyd wrote:

I would gess that the parameter is passed via edi instead of rdi only as an optimisation — I doubt printf does some 32 bit magic on a 64-bit architecture. [...]

Ok, I realized some time later, that all string work is done via integers, and integers are defaulted to 32 bit, and as mentioned in the ABI document an int64 is an special data type. So it was a mistake by me.

Offline

#9 2008-01-18 10:20:35

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: compiling assembly under arch64

If you want to learn assembly, also check out fasm, http://flatassembler.net/.
It's more powerful than gas, and you'll have a bit more to learn about macros and whatnots, but it should definitely be worth it.

Offline

#10 2008-01-18 11:03:36

X/ax
Member
From: Oost vlaanderen, Belgium
Registered: 2008-01-13
Posts: 275
Website

Re: compiling assembly under arch64

Thanks a lot to both, that's exactly what I wanted! ^^


My coding blog (or an attempt at it)
Archer start page (or an attempt at it)

Offline

Board footer

Powered by FluxBB