$ cat > hello.s
# ----------------------------------------------------------------------------------------
# Writes "Hello, World" to the console using only system calls. Runs on 64-bit Linux only.
# To assemble and run:
#
# gcc -c hello.s && ld hello.o && ./a.out
#
# or
#
# gcc -nostdlib hello.s && ./a.out
# ---------------------------------------------------------------------------------------- .global _start
.text
_start:
# write(1, message, 13)
mov $1, %rax # system call 1 is write
mov $1, %rdi # file handle 1 is stdout
mov $message, %rsi # address of string to output
mov $13, %rdx # number of bytes
syscall # invoke operating system to do the write
# exit(0)
mov $60, %rax # system call 60 is exit
xor %rdi, %rdi # we want return code 0
syscall # invoke operating system to exit
message:
.ascii "Hello, world\n"
^D
$ gcc -g -no-pie -nostdlib hello.s
$ gdb a.out
GNU gdb (Debian 8.2.1-2) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) break hello.s:20
Breakpoint 1 at 0x401015: file hello.s, line 20.
(gdb) run
Starting program: /home/anon/a.out
Breakpoint 1, _start () at hello.s:20
20 mov $13, %rdx # number of bytes
(gdb) list
15 _start:
16 # write(1, message, 13)
17 mov $1, %rax # system call 1 is write
18 mov $1, %rdi # file handle 1 is stdout
19 mov $message, %rsi # address of string to output
20 mov $13, %rdx # number of bytes
21 syscall # invoke operating system to do the write
22
23 # exit(0)
24 mov $60, %rax # system call 60 is exit
(gdb) detach
Detaching from program: /home/anon/a.out, process 28910
[Inferior 1 (process 28910) detached]
Hello, world
(gdb) quit
$