views:

317

answers:

7

I'm fairly new to linux(ubuntu 10.04) and a total novice to assembler. I was following some tutorials and I couldn't find anything specific to linux. So, my question is, what is a good package to compile/run assembler and what are the command line commands to compile/run for that package?

+2  A: 

The assembler(GNU) is as(1)

Recursion
+8  A: 

The GNU assembler (gas) and NASM are both good choices. However, they have some differences, the big one being the order you put operations and their operands.

gas uses AT&T syntax:

mnemonic    source, destination

nasm uses intel style:

mnemonic    destination, source

Either one will probably do what you need.

Try out this tutorial: http://asm.sourceforge.net/intro/Assembly-Intro.html

George
Thanks, that tutorial is great since it's linux specific. Also, the tutorial had very specific instructions on how to compile and run assembler progs in linux
Rafe Kettler
gas has supported ".intel_syntax" for a while - I'd personally still use fasm, yasm or nasm though.
snemarch
+2  A: 

If you are using NASM, the command-line is just

nasm file.asm -o outfile

where 'file.asm' is your assembly file (code) and 'outfile' is the executable you want.

Here is some more info:

http://www.nasm.us/doc/nasmdoc2.html#section-2.1

You can install NASM in Ubuntu with the following command:

apt-get install nasm

Here is a basic Hello World in Linux assembly to wet your appetite:

http://www.cin.ufpe.br/~if817/arquivos/asmtut/index.html

I hope this is what you were asking...

Justin
+1  A: 

The GNU assembler is probably already installed on your system. Try man as to see full usage information. You can use as to compile individual files and ld to link if you really, really want to.

However, GCC makes a great front-end. It can assemble .s files for you. For example:

$ cat >hello.s <<EOF
.data
.globl hello
hello:
.string "Hello, world!"

.text
.global main
main:
    pushq   %rbp
    movq    %rsp,       %rbp
    movq    $hello,     %rdi
    call    puts
    movq    $0,         %rax
    leave
    ret
EOF
$ gcc hello.s -o hello
$ ./hello
Hello, world!

The code above is AMD64. It would be different if you're still on a 32-bit machine.

You can also compile C/C++ code directly to assembly if you're curious how something works:

$ cat >hello.c <<EOF
#include <stdio.h>
int main(void) {
    printf("Hello, world!\n");
    return 0;
}
EOF
$ gcc -S hello.c -o hello.s
Jay Conrod
+1  A: 

There is also FASM for Linux.

format ELF executable

segment readable executable

start:
mov eax, 4
mov ebx, 1
mov ecx, hello_msg
mov edx, hello_size
int 80h

mov eax, 1
mov ebx, 0
int 80h

segment readable writeable

hello_msg db "Hello World!",10,0
hello_size = $-hello_msg

It comiles with

fasm hello.asm hello
Artur Artamonov
A: 

My suggestion would be to get the book Programming From Ground Up:

https://docs.google.com/viewer?url=http://nongnu.askapache.com/pgubook/ProgrammingGroundUp-1-0-booksize.pdf

That is a very good starting point for getting into assembler programming under linux and it explains a lot of the basics you need to understand to get started.

gilligan
A: 

3 syntax (nasm, tasm, gas ) in 1 assembler, yasm.

http://www.tortall.net/projects/yasm/

plan9assembler