1

I'm writng a basic x86-64 assembly program on my linux system(x86-64) to print "Trollll" on execution, and it doesn't seem to work.

This is the code I've written. The file name is hello1.asm

section .data
msg db 'Trollll',0xa

section .text
global _start

_start:

;write(int fd,char* msg,unsigned int len)
mov rax,1       ; for write system call
mov rbx,1       ; first parameter (for output on stdout)
mov rcx,msg     ;second parameter
mov rdx,8
int 0x80

;exit(int return)

mov rax,60      ;for exit system call
mov rbx,0       ;first parameter
int 0x80

I first complied the file with:

nasm -f elf64 hello1.asm

I then linked it with:

ld -o hello1 hello1.o

Next, I executed it with:

./hello1

I was expecting the output to be: Trollll

but, nothing showed on the screen? Where am I missing something?

Note: I found the system call numbers in the file:

/usr/include/x86-64-linux-gnu/asm/unistd_64.h
Arav Garg
  • 11
  • 2
  • 1
    https://stackoverflow.com/questions/12806584/what-is-better-int-0x80-or-syscall – Mat Oct 07 '19 at 08:09
  • Oh, you're using 64-bit call numbers with int 0x80. It would have worked with `int 0x80` if you hadn't broken that because your static data can be addressed using a 32-bit pointer. Of course using 64-bit syscalls would have been better. Note that the 64-bit `syscall` ABI puts args in different registers, very close to the x86-64 SysV function-calling convention. – Peter Cordes Oct 07 '19 at 08:18
  • @Mat @Peter Tried replacing int `0x80` with `syscall`, didn't work – Arav Garg Oct 07 '19 at 08:22
  • @PeterCordes Yeah it works when I replace the 64-bit call numbers with the 32-bit call numbers. But I can't get it working with the 64-bit call numbers?! – Arav Garg Oct 07 '19 at 08:28
  • Of course not if you keep invoking the 32-bit `int 0x80` ABI. Read the linked duplicates. – Peter Cordes Oct 07 '19 at 08:28
  • 1
    @PeterCordes Ahh damn, didn't consider that. I've only worked on 32-bit assembly codes so far, so I assumed that the 64-bit translation would just be the `e to r`. It's working now, cheers! – Arav Garg Oct 07 '19 at 08:42

0 Answers0