Compile/Run Assembler in Linux

Compile/run assembler in Linux?

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 (guide: https://stackoverflow.com/tags/att/info):

mnemonic    source, destination

nasm uses Intel style (guide: https://stackoverflow.com/tags/intel-syntax/info):

mnemonic    destination, source

Either one will probably do what you need. GAS also has an Intel-syntax mode, which is a lot like MASM, not NASM.


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

See also more links to guides and docs in Stack Overflow's x86 tag wiki

Minimal example to compile & run assembly with gcc?


.text
.align 4
.globl main
main:
pushl %ebp
movl %esp,%ebp
xorl %eax,%eax
leave
ret

To compile and run:

$ gcc -m32 asm.S
$ ./a.out

How do you assemble, link and run a .s file in linux?

Linux executables require an entry point to be specified. The entry point is the address of the first instruction to be executed in your program. If not specified otherwise, the link editor looks for a symbol named _start to use as an entry point. Your program does not contain such a symbol, thus the linker complains and picks the beginning of the .text section as the entry point. To fix this problem, rename main to _start.

Note further that unlike on DOS, there is nothing to return to from _start. So your attempt to return is going to cause a crash. Instead, call the system call sys_exit to exit the program:

mov $0, %edi  # exit status
mov $60, %eax # system call number
syscall # perform exit call

Alternatively, if you want to use the C runtime environment and call functions from the C library, leave your program as is and instead assemble and link using the C compiler driver cc:

cc -o yea yea.s

If you do so, the C runtime environment provides the entry point for you and eventually tries to call a function main which is where your code comes in. This approach is required if you want to call functions from the C library. If you do it this way, make sure that main follows the SysV ABI (calling convention).

Note that even then your code is incorrect. The return value of a function is given in the eax (resp. rax) register and not pushed on the stack. To return zero from main, write

mov $0, %eax   # exit status
ret # return from function

How to compile and run assembler source from the command prompt?

It is just your .c file in another programming language you still need to assemble and link it. The gnu tools actually allow you to do this using the c compiler you should be able to feed this into gcc and it will understand it is assembly language and not C and once assembled will then call the linker for you as it does when you feed it a .c file. And ideally link it for your target host so it just runs. You can then for example in the assembly language modify the ascii string add some more characters to the a + b, re-run through gcc to see that it is really this asm that is being used.

Commands to compile ASM file with C program

I resolved the problem by using your @Jester solution :

gcc -no-pie -o executable main.o hello.o

and thanks Ped7g for explanation.



Related Topics



Leave a reply



Submit