PeterPaul
PeterPaul

Reputation: 21

x86-64 Assembly code not running

I am new to assembly. I'm attempting to learn by referencing from a book named "Writing Security Tools and Exploits". Anyone familiar with this book would know that the assembly code is written for 32bit but I'm build this on a 64bit system.

Due to this I have rewritten the code to run on 64bit. After successfully compiling the code the desired output is not accomplished when I attempt to run the program. Instead I receive no output.

I am building this on an AMD64 Debian Linux system. Here is the code I am trying to receive output from:

global _start
_start:
xor             rax,rax

jmp short string
code:
pop             rsi
push byte       15
push            rsi
push byte       1
mov             al,4
push            rax
int             0x80

xor             rax,rax
push            rax
push            rax
mov             al,1
int             0x80

string:
call code
db  'Hello, world !',0x0a

I compile it using the following commands

$> nasm -f elf64 hello.asm $> ld -s -o hello hello.o

When I attempt to run there is no output.

Any suggestions as to where I am going wrong?

Upvotes: 2

Views: 251

Answers (1)

perror
perror

Reputation: 7386

There is mainly one big problem in your code, you seems to believe that the calling convention (passing arguments) for the system call through int 0x80 is passed by pushing it onto the stack. But, in fact, you need to go through the registers eax, ebx and ecx (see here for more details).

So, the proper way to write the code would be:

global _start
_start:
xor             rax,rax

jmp short string
code:
pop           rsi
mov           rdx, 15   
mov             rcx, rsi
mov             rbx, 1
mov             al,4
int             0x80

xor             rax,rax
mov             rbx, rax
mov             al,1
int             0x80

string:
call code
db  'Hello, world !',0x0a

Then, just do:

$> nasm -f elf64 hello.asm
$> gcc -nostdlib -o hello hello.o

Upvotes: 1

Related Questions