I am trying to run the following code, which I found as an example of macros in assembly language at https://www.tutorialspoint.com/assembly_programming/assembly_macros.htm
; A macro with two parameters
; Implements the write system call
%macro write_string 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 80h
%endmacro
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
write_string msg1, len1
write_string msg2, len2
write_string msg3, len3
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg1 db 'Hello, programmers!',0xA,0xD
len1 equ $ - msg1
msg2 db 'Welcome to the world of,', 0xA,0xD
len2 equ $- msg2
msg3 db 'Linux assembly programming! '
len3 equ $- msg3
I saved this code as example.asm and tried to compile and run it as follows:
nasm -f elf64 example.asm
ld -s -o example example.o
./example
However, I get a segmentation fault. What may be the reason for this?
Update: As soon as I run within gdb I get:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004000c4 in ?? ()
And when I do backtrace I get:
#0 0x00000000004000c4 in ?? ()
#1 0x0000000000000001 in ?? ()
#2 0x00007ffffffee1bf in ?? ()
#3 0x0000000000000000 in ?? ()