summaryrefslogtreecommitdiff
path: root/PGU/CHAP8
diff options
context:
space:
mode:
Diffstat (limited to 'PGU/CHAP8')
-rw-r--r--PGU/CHAP8/helloworld-lib.s20
-rw-r--r--PGU/CHAP8/helloworld-nolib.s25
-rw-r--r--PGU/CHAP8/linux.s16
-rw-r--r--PGU/CHAP8/printcall.s34
4 files changed, 95 insertions, 0 deletions
diff --git a/PGU/CHAP8/helloworld-lib.s b/PGU/CHAP8/helloworld-lib.s
new file mode 100644
index 0000000..6e04968
--- /dev/null
+++ b/PGU/CHAP8/helloworld-lib.s
@@ -0,0 +1,20 @@
+# Hello world using DSOs
+
+.section .data
+ helloworld:
+ .ascii "hello world\n\0"
+
+.section .text
+
+.globl _start
+
+_start:
+
+ # This differs from the book. In x86_64, arguments are passed through
+ # registers most of the time, in contrast with i386, where we can push
+ # them in the stack.
+ movq $helloworld, %rdi
+ call printf
+
+ movq $50, %rdi
+ call exit
diff --git a/PGU/CHAP8/helloworld-nolib.s b/PGU/CHAP8/helloworld-nolib.s
new file mode 100644
index 0000000..83d52ce
--- /dev/null
+++ b/PGU/CHAP8/helloworld-nolib.s
@@ -0,0 +1,25 @@
+# Write "hello world" and exit
+
+.include "linux.s"
+
+.section .data
+ helloworld:
+ .ascii "hello world\n"
+ helloworld_end:
+
+ .equ helloworld_len, helloworld_end - helloworld
+
+.section .text
+
+.globl _start
+
+_start:
+ movq $STDOUT, %rdi
+ movq $helloworld, %rsi
+ movq $helloworld_len, %rdx
+ movq $SYS_WRITE, %rax
+ syscall
+
+ movq $0, %rdi
+ movq $SYS_EXIT, %rax
+ syscall
diff --git a/PGU/CHAP8/linux.s b/PGU/CHAP8/linux.s
new file mode 100644
index 0000000..9ab8243
--- /dev/null
+++ b/PGU/CHAP8/linux.s
@@ -0,0 +1,16 @@
+# Syscall numbers (x86_64)
+
+.equ SYS_EXIT, 60
+.equ SYS_READ, 0
+.equ SYS_WRITE, 1
+.equ SYS_OPEN, 2
+.equ SYS_CLOSE, 3
+.equ SYS_BRK, 12
+
+# Default File Descriptors
+.equ STDIN, 0
+.equ STDOUT, 1
+.equ STDERR, 2
+
+# Common Status Codes
+.equ END_OF_FILE, 0
diff --git a/PGU/CHAP8/printcall.s b/PGU/CHAP8/printcall.s
new file mode 100644
index 0000000..2d50e73
--- /dev/null
+++ b/PGU/CHAP8/printcall.s
@@ -0,0 +1,34 @@
+# Just an example of calling printf() using ASM
+
+.section .data
+ firststring:
+ .ascii "Hello! %s is a %s who lives the number %d\n\0"
+ name:
+ .ascii "Maiolino\0"
+ personstring:
+ .ascii "person\0"
+
+ numberloved:
+ .long 123
+
+.section .text
+.globl _start
+
+ # Recall we are using x86_64 ABI here, so we pass arguments through
+ # registers instead of on the stack.
+
+_start:
+ movq $firststring, %rdi
+ movq $name, %rsi
+ movq $personstring, %rdx
+
+ # We don't use $numberloved here because $ sign means 'immediate'
+ # addressing. Here we want the 'CONTENT' pointed by label 'numberloved'
+ # so, we use 'direct' addressing, by ommiting the $ sign, so we get the
+ # information "pointed by" the label numberloved itself.
+ movq numberloved, %rcx
+ call printf
+
+ movq $0, %rdi
+ call exit
+