From e4c05570709db2718a983c27322942769eab4c86 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Sat, 5 Jul 2014 18:05:46 +0800 Subject: [PATCH] Add assembly sources to git --- src/add_digits_asm.s | 28 ++++++++++++++++++++++++++++ src/sub_digits_asm.s | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/add_digits_asm.s create mode 100644 src/sub_digits_asm.s diff --git a/src/add_digits_asm.s b/src/add_digits_asm.s new file mode 100644 index 0000000..39f4af9 --- /dev/null +++ b/src/add_digits_asm.s @@ -0,0 +1,28 @@ +.section .text +.globl add_digits +.type add_digits, @function + +# Add two arrays of 64 bit digits, with carry, modifying the first argument +# Address at first argument %rdi is array to add and modify +# Address at second %rsi will be added (not modified) +# Third argument is counter of number of digits +# Result in %rax is the final result in the carry flag +# Exploits the fact that inc and dec do not affect the carry flag +add_digits: + loop: + movq (%rsi), %rax # Temporarily store digit from second array + adcq %rax, (%rdi) # Add digits in second and first array, store in first + dec %rdx # Decrement counter + jz end_loop # We are done + + # Move to next element in the first array + leaq 8(,%rdi,1), %rdi + # Move to next element in the second array + leaq 8(,%rsi,1), %rsi + jmp loop # Repeat + end_loop: + movq $0, %rax + jnc end + movq $1, %rax + end: + ret # We are done diff --git a/src/sub_digits_asm.s b/src/sub_digits_asm.s new file mode 100644 index 0000000..1a6ee3b --- /dev/null +++ b/src/sub_digits_asm.s @@ -0,0 +1,28 @@ +.section .text +.globl sub_digits +.type sub_digits, @function + +# Subtract two arrays of 64 bit digits, with carry, modifying the first argument +# Address at first argument %rdi is array to add and modify +# Address at second %rsi will be added (not modified) +# Third argument is counter of number of digits +# Result in %rax is the final result in the carry flag +# Exploits the fact that inc and dec do not affect the carry flag +sub_digits: + loop: + movq (%rsi), %rax # Temporarily store digit from second array + sbbq %rax, (%rdi) # Subtract digits in second and first array, store in first + dec %rdx # Decrement counter + jz end_loop # We are done + + # Move to next element in the first array + leaq 8(,%rdi,1), %rdi + # Move to next element in the second array + leaq 8(,%rsi,1), %rsi + jmp loop # Repeat + end_loop: + movq $0, %rax + jnc end + movq $1, %rax + end: + ret # We are done -- 2.20.1