1

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 ?? ()
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Can you try running it within gdb? `stepi` and `nexti` could be used to narrow it down to exactly which instruction is failing (see https://stackoverflow.com/questions/2420813/using-gdb-to-single-step-assembly-code-outside-specified-executable-causes-error) – Aaron Altman Jun 16 '19 at 00:36
  • I asked about WSL because you can't use the 32-bit `int 0x80` compatibility interface on WSL (which is 64-bit only). If you are on WSL you have to use `syscall` system call interface instead of `int 0x80`. Ryan Chapman has the 64-bit Linux System call table here: https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/ – Michael Petch Jun 16 '19 at 00:45
  • What exactly should I change the int 0x80 to? – erick alderson Jun 16 '19 at 00:53
  • See here: https://stackoverflow.com/questions/20326025/linux-assembly-how-to-call-syscall – John Zwinck Jun 16 '19 at 01:05
  • Are you on WSL (Windows Subsystem For Linux)? If you are I provided a link tot he system call table. Change your code to use the registers for the 64-bit calling convention in that table and replace `int 0x80` with `syscall` instruction. If you are on WSL this is a duplicate og https://stackoverflow.com/a/47737595/3857942 . That answer actually has code on how to use the 64--bit syscall calling convention. – Michael Petch Jun 16 '19 at 01:11

0 Answers0