From 20834dcc57537cd95260a4a22f5d91a027adfd35 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:20:01 +0200 Subject: Add x86_64 asm Signed-off-by: Carlos Maiolino --- x86_64/Makefile | 13 +++++++++++++ x86_64/arithmetic | Bin 0 -> 8824 bytes x86_64/arithmetic.o | Bin 0 -> 808 bytes x86_64/arithmetic.s | 22 ++++++++++++++++++++++ x86_64/exit | Bin 0 -> 8824 bytes x86_64/exit.o | Bin 0 -> 760 bytes x86_64/exit.s | 10 ++++++++++ x86_64/exponent | Bin 0 -> 8928 bytes x86_64/exponent.o | Bin 0 -> 864 bytes x86_64/exponent.s | 37 +++++++++++++++++++++++++++++++++++++ x86_64/jmp_havoc | Bin 0 -> 9120 bytes x86_64/jmp_havoc.o | Bin 0 -> 1056 bytes x86_64/jmp_havoc.s | 39 +++++++++++++++++++++++++++++++++++++++ x86_64/mul_by_add | Bin 0 -> 8976 bytes x86_64/mul_by_add.o | Bin 0 -> 904 bytes x86_64/mul_by_add.s | 25 +++++++++++++++++++++++++ x86_64/reg_size | Bin 0 -> 8896 bytes x86_64/reg_size.o | Bin 0 -> 800 bytes x86_64/reg_size.s | 17 +++++++++++++++++ 19 files changed, 163 insertions(+) create mode 100644 x86_64/Makefile create mode 100755 x86_64/arithmetic create mode 100644 x86_64/arithmetic.o create mode 100644 x86_64/arithmetic.s create mode 100755 x86_64/exit create mode 100644 x86_64/exit.o create mode 100644 x86_64/exit.s create mode 100755 x86_64/exponent create mode 100644 x86_64/exponent.o create mode 100644 x86_64/exponent.s create mode 100755 x86_64/jmp_havoc create mode 100644 x86_64/jmp_havoc.o create mode 100644 x86_64/jmp_havoc.s create mode 100755 x86_64/mul_by_add create mode 100644 x86_64/mul_by_add.o create mode 100644 x86_64/mul_by_add.s create mode 100755 x86_64/reg_size create mode 100644 x86_64/reg_size.o create mode 100644 x86_64/reg_size.s 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 Binary files /dev/null and b/x86_64/arithmetic differ diff --git a/x86_64/arithmetic.o b/x86_64/arithmetic.o new file mode 100644 index 0000000..9d258f5 Binary files /dev/null and b/x86_64/arithmetic.o 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 Binary files /dev/null and b/x86_64/exit differ diff --git a/x86_64/exit.o b/x86_64/exit.o new file mode 100644 index 0000000..44033df Binary files /dev/null and b/x86_64/exit.o 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 Binary files /dev/null and b/x86_64/exponent differ diff --git a/x86_64/exponent.o b/x86_64/exponent.o new file mode 100644 index 0000000..516d6c5 Binary files /dev/null and b/x86_64/exponent.o 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 Binary files /dev/null and b/x86_64/jmp_havoc differ diff --git a/x86_64/jmp_havoc.o b/x86_64/jmp_havoc.o new file mode 100644 index 0000000..1e65228 Binary files /dev/null and b/x86_64/jmp_havoc.o 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 Binary files /dev/null and b/x86_64/mul_by_add differ diff --git a/x86_64/mul_by_add.o b/x86_64/mul_by_add.o new file mode 100644 index 0000000..7d7e01a Binary files /dev/null and b/x86_64/mul_by_add.o 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 Binary files /dev/null and b/x86_64/reg_size differ diff --git a/x86_64/reg_size.o b/x86_64/reg_size.o new file mode 100644 index 0000000..1622ccf Binary files /dev/null and b/x86_64/reg_size.o 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 -- cgit v1.2.3