Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

64 assembly language using nasm on ubuntu

I am trying to write a simple hello world program in 64-bit assembly and run on Ubuntu 64 bit. The program is as follows :

global _start           ; entry point export for ld section .text   
_start:     ; system call to write message to stdout
    mov rax, 1      ; sys_write
    mov rdi, 1      ; stdout
    mov rsi, mes    ; message address
    mov rdx, len    ; message length
    syscall     ; exit sys call
    mov rax, 60     ; exit call id
    mov rdi, 0      ; return success
    syscall
section .data
    mes: db 'Hello, world!',0x0A    ; message
    len :   equ $-mes   

I assembled it using nasm -f elf64 hello64.asm and tried linking it using ld -o hello64 hello64.o it gives me following error -

ld: i386:x86-64 architecture of input file `hello64.o' is incompatible with i386 output

I get same error even when using flags --oformat elf64-x86-64 or elf64-little or elf64-big.

can someone help out ?

like image 573
user2037008 Avatar asked Jan 21 '26 06:01

user2037008


1 Answers

The following works on my system:

nasm -f elf64 hello64.asm
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello64 hello64.o
like image 187
Michael Spector Avatar answered Jan 23 '26 06:01

Michael Spector



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!