summaryrefslogtreecommitdiff
path: root/x86_64
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:20:01 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:20:01 +0200
commit20834dcc57537cd95260a4a22f5d91a027adfd35 (patch)
tree60f21143382380c3cd54110b4134f6fa98000a9b /x86_64
parent8c6fc0c15415b32080a848bbde640e104098cf13 (diff)
Add x86_64 asm
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'x86_64')
-rw-r--r--x86_64/Makefile13
-rwxr-xr-xx86_64/arithmeticbin0 -> 8824 bytes
-rw-r--r--x86_64/arithmetic.obin0 -> 808 bytes
-rw-r--r--x86_64/arithmetic.s22
-rwxr-xr-xx86_64/exitbin0 -> 8824 bytes
-rw-r--r--x86_64/exit.obin0 -> 760 bytes
-rw-r--r--x86_64/exit.s10
-rwxr-xr-xx86_64/exponentbin0 -> 8928 bytes
-rw-r--r--x86_64/exponent.obin0 -> 864 bytes
-rw-r--r--x86_64/exponent.s37
-rwxr-xr-xx86_64/jmp_havocbin0 -> 9120 bytes
-rw-r--r--x86_64/jmp_havoc.obin0 -> 1056 bytes
-rw-r--r--x86_64/jmp_havoc.s39
-rwxr-xr-xx86_64/mul_by_addbin0 -> 8976 bytes
-rw-r--r--x86_64/mul_by_add.obin0 -> 904 bytes
-rw-r--r--x86_64/mul_by_add.s25
-rwxr-xr-xx86_64/reg_sizebin0 -> 8896 bytes
-rw-r--r--x86_64/reg_size.obin0 -> 800 bytes
-rw-r--r--x86_64/reg_size.s17
19 files changed, 163 insertions, 0 deletions
diff --git a/x86_64/Makefile b/x86_64/Makefile
new file mode 100644
index 0000000..4096a79
--- /dev/null
+++ b/x86_64/Makefile
@@ -0,0 +1,13 @@
+
+OBJS = exit.o arithmetic.o reg_size.o jmp_havoc.o exponent.o mul_by_add.o
+
+all: $(OBJS)
+ ld -o exit exit.o
+ ld -o arithmetic arithmetic.o
+ ld -o reg_size reg_size.o
+ ld -o jmp_havoc jmp_havoc.o
+ ld -o exponent exponent.o
+ ld -o mul_by_add mul_by_add.o
+
+clean:
+ rm $(OBJS)
diff --git a/x86_64/arithmetic b/x86_64/arithmetic
new file mode 100755
index 0000000..b8fd40a
--- /dev/null
+++ b/x86_64/arithmetic
Binary files differ
diff --git a/x86_64/arithmetic.o b/x86_64/arithmetic.o
new file mode 100644
index 0000000..9d258f5
--- /dev/null
+++ b/x86_64/arithmetic.o
Binary files differ
diff --git a/x86_64/arithmetic.s b/x86_64/arithmetic.s
new file mode 100644
index 0000000..34216f1
--- /dev/null
+++ b/x86_64/arithmetic.s
@@ -0,0 +1,22 @@
+# Simple program exercising CPU's arithmetic instructions
+
+.globl _start
+
+.section .text
+
+_start:
+ movq $3, %rdi
+ movq %rdi, %rax
+ mulq %rdi
+ movq $2, %rdi
+ addq %rdi, %rax
+ movq $4, %rdi
+ mulq %rdi
+
+ movq $10, %rax
+ movq $3, %rdi
+ divq %rdi # Remainder goes to %rdx
+ movq %rax, %rdi
+
+ mov $60, %rax
+ syscall
diff --git a/x86_64/exit b/x86_64/exit
new file mode 100755
index 0000000..c9bb3ab
--- /dev/null
+++ b/x86_64/exit
Binary files differ
diff --git a/x86_64/exit.o b/x86_64/exit.o
new file mode 100644
index 0000000..44033df
--- /dev/null
+++ b/x86_64/exit.o
Binary files differ
diff --git a/x86_64/exit.s b/x86_64/exit.s
new file mode 100644
index 0000000..d9c594f
--- /dev/null
+++ b/x86_64/exit.s
@@ -0,0 +1,10 @@
+# Simple program that just exits with a simple status code
+
+.globl _start
+
+.section .text
+
+_start:
+ movq $60, %rax # sys_exit
+ movq $66, %rdi
+ syscall
diff --git a/x86_64/exponent b/x86_64/exponent
new file mode 100755
index 0000000..ce3f124
--- /dev/null
+++ b/x86_64/exponent
Binary files differ
diff --git a/x86_64/exponent.o b/x86_64/exponent.o
new file mode 100644
index 0000000..516d6c5
--- /dev/null
+++ b/x86_64/exponent.o
Binary files differ
diff --git a/x86_64/exponent.s b/x86_64/exponent.s
new file mode 100644
index 0000000..e469d26
--- /dev/null
+++ b/x86_64/exponent.s
@@ -0,0 +1,37 @@
+# Compute a^b, where %rbx=a and %rcx=b
+#
+# Result is added to the accumulator (%rax)
+
+
+.globl _start
+
+.section .text
+
+_start:
+ movq $2, %rbx # Base
+ movq $5, %rcx # Exponent
+
+ movq $1, %rax # Initialize accumulator, take advantage of the fact
+ # that anything to the power of 0 is 1.
+
+ # We just need to run this once, as decq will set ZF if %rcx reaches
+ # zero, and the jmp instruction does nothing with the ZF
+ cmpq $0, %rcx
+ je complete
+
+mainloop:
+
+ # Multiply %rbx by %rax, and stores the result in %rax
+ # Hence %rax is the accumulator register
+ mulq %rbx
+
+ # loopq instruction is very nice, in a single instruction it does:
+ # - decrement %rcx - The counter register -
+ # - compare %rcx to zero
+ # - Jump to the location specified if after decrementing %rcx is not 0
+ loopq mainloop
+
+complete:
+ movq %rax, %rdi
+ movq $60, %rax
+ syscall
diff --git a/x86_64/jmp_havoc b/x86_64/jmp_havoc
new file mode 100755
index 0000000..6c22ca7
--- /dev/null
+++ b/x86_64/jmp_havoc
Binary files differ
diff --git a/x86_64/jmp_havoc.o b/x86_64/jmp_havoc.o
new file mode 100644
index 0000000..1e65228
--- /dev/null
+++ b/x86_64/jmp_havoc.o
Binary files differ
diff --git a/x86_64/jmp_havoc.s b/x86_64/jmp_havoc.s
new file mode 100644
index 0000000..6dd734d
--- /dev/null
+++ b/x86_64/jmp_havoc.s
@@ -0,0 +1,39 @@
+# Play around with jumps
+
+.section .text
+.globl _start
+
+_start:
+ movq $25, %rax
+ jmp thelabel
+
+somewhere:
+ movq %rax, %rdi
+ jmp anotherlabel
+
+label1:
+ addq %rbx, %rax
+ movq $5, %rbx
+ jmp here
+
+labellabel:
+ syscall
+
+anotherlabel:
+ movq $60, %rax
+ jmp labellabel
+
+thelabel:
+ movq %rax, %rbx
+ jmp there
+
+here:
+ divq %rbx
+ jmp somewhere
+
+there:
+ addq $5, %rbx
+ jmp label1
+
+anywhere:
+ jmp thelabel
diff --git a/x86_64/mul_by_add b/x86_64/mul_by_add
new file mode 100755
index 0000000..1850861
--- /dev/null
+++ b/x86_64/mul_by_add
Binary files differ
diff --git a/x86_64/mul_by_add.o b/x86_64/mul_by_add.o
new file mode 100644
index 0000000..7d7e01a
--- /dev/null
+++ b/x86_64/mul_by_add.o
Binary files differ
diff --git a/x86_64/mul_by_add.s b/x86_64/mul_by_add.s
new file mode 100644
index 0000000..3e8c360
--- /dev/null
+++ b/x86_64/mul_by_add.s
@@ -0,0 +1,25 @@
+# Do a multiplication without using mulq, resulting in a * b
+.section .text
+.globl _start
+
+.set OP, 1 # Set here the numbers to
+.set OP2, 1 # be multiplied
+
+_start:
+ movq $OP2, %rcx
+ movq $0, %rax
+
+ # We can't rely on loopq to drop exit the loop in case OP2
+ # is 0, because the first iteraction will cause %rcx to be -1
+ cmpq $OP2, %rax
+ je exit
+
+
+addloop:
+ addq $OP, %rax
+ loopq addloop
+
+exit:
+ movq %rax, %rdi
+ movq $60, %rax
+ syscall
diff --git a/x86_64/reg_size b/x86_64/reg_size
new file mode 100755
index 0000000..eec38b7
--- /dev/null
+++ b/x86_64/reg_size
Binary files differ
diff --git a/x86_64/reg_size.o b/x86_64/reg_size.o
new file mode 100644
index 0000000..1622ccf
--- /dev/null
+++ b/x86_64/reg_size.o
Binary files differ
diff --git a/x86_64/reg_size.s b/x86_64/reg_size.s
new file mode 100644
index 0000000..e374922
--- /dev/null
+++ b/x86_64/reg_size.s
@@ -0,0 +1,17 @@
+# Program to play around with different register sizes
+
+.set exit_num, 60
+
+.section .text
+.globl _start
+
+_start:
+ movw $0b0000101000000101, %bx #10d and 5d into high and low registers
+ addb %bh, %bl
+ movb $0, %bh
+
+ # %bh now contains 10+5, use it as an argument to exit()
+
+ movq %rbx, %rdi
+ movq $exit_num, %rax
+ syscall