2018-04-30 16:09:48 +00:00
|
|
|
;; Hello World Program #2
|
2018-04-26 02:40:23 +00:00
|
|
|
;; Compile with: nasm -f elf64 hello.s
|
2018-04-30 16:09:48 +00:00
|
|
|
;; Link with: ld -m elf_x86_64 -o hello hello.o
|
2018-04-25 14:56:08 +00:00
|
|
|
;; Run with: ./hello
|
|
|
|
|
2018-04-30 16:09:48 +00:00
|
|
|
;; The following includes are derived from:
|
2018-04-25 15:41:35 +00:00
|
|
|
;; sys/unistd_64.h
|
2018-04-25 14:56:08 +00:00
|
|
|
%define SYS_write 1
|
|
|
|
%define SYS_exit 60
|
|
|
|
|
2018-04-30 16:09:48 +00:00
|
|
|
;; The following includes are derived from:
|
2018-04-25 14:56:08 +00:00
|
|
|
;; unistd.h
|
|
|
|
%define STDOUT 1
|
|
|
|
|
2018-04-30 16:09:48 +00:00
|
|
|
;;; 'global' is the directive that tells the NASM the address of the
|
|
|
|
;;; first instruction to run.
|
|
|
|
|
|
|
|
global _start
|
|
|
|
|
2018-04-25 14:56:08 +00:00
|
|
|
section .data
|
|
|
|
msg db "Hello You Beautiful Human", 0Ah
|
2018-04-30 16:09:48 +00:00
|
|
|
len equ $-msg ; The $ is a NASM helper that means
|
|
|
|
; "The address of the current
|
|
|
|
; instruction. 'equ' is a NASM
|
|
|
|
; macro that performs the math and
|
|
|
|
; places in the 'len' constant the
|
|
|
|
; difference between the start of the
|
|
|
|
; current instruction and the 'msg'.
|
2018-04-25 14:56:08 +00:00
|
|
|
|
|
|
|
section .text
|
|
|
|
_start:
|
2018-04-30 16:09:48 +00:00
|
|
|
mov rdx, len ; Mov the address of 'len' to register RDX
|
|
|
|
mov rsi, msg ; Mov the address of the message to RSI (Source Index)
|
2018-04-25 14:56:08 +00:00
|
|
|
mov rdi, STDOUT ; using STDOUT (see definition above)
|
2018-04-30 16:09:48 +00:00
|
|
|
mov rax, SYS_write ; Access WRITE in 64-bit linux
|
|
|
|
syscall ; Call the kernel to run the WRITE command.
|
|
|
|
|
|
|
|
;; Note that it's always register AX that we use to tell the kernel
|
|
|
|
;; what we want it to do. Depending on the kernel instruction, other
|
|
|
|
;; registers may be used to fill out the command.
|
2018-04-25 14:56:08 +00:00
|
|
|
|
2018-04-30 16:09:48 +00:00
|
|
|
mov rdi, 0 ; Mov '0' to be our exit code
|
|
|
|
mov rax, SYS_exit ; Access the exit command
|
|
|
|
syscall ; Call the kernel to run the EXIT command.
|
2018-04-25 14:56:08 +00:00
|
|
|
|