From: Sam Moore Date: Sat, 5 Jul 2014 10:05:46 +0000 (+0800) Subject: Add assembly sources to git X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=commitdiff_plain;h=e4c05570709db2718a983c27322942769eab4c86 Add assembly sources to git --- 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