From 3cf128d6da667d00bb5bb659413ea55a98a02aff Mon Sep 17 00:00:00 2001 From: mjfernez Date: Tue, 12 Oct 2021 20:18:28 -0400 Subject: Added description. x86 solutions for some problems --- 06-Sum-Square-Difference/asm/Makefile | 6 ++++ 06-Sum-Square-Difference/asm/sumsq | Bin 0 -> 17248 bytes 06-Sum-Square-Difference/asm/sumsq.asm | 49 +++++++++++++++++++++++++++++++++ 06-Sum-Square-Difference/asm/sumsq.o | Bin 0 -> 2192 bytes 06-Sum-Square-Difference/sumsq | Bin 5 files changed, 55 insertions(+) create mode 100644 06-Sum-Square-Difference/asm/Makefile create mode 100755 06-Sum-Square-Difference/asm/sumsq create mode 100644 06-Sum-Square-Difference/asm/sumsq.asm create mode 100644 06-Sum-Square-Difference/asm/sumsq.o mode change 100644 => 100755 06-Sum-Square-Difference/sumsq (limited to '06-Sum-Square-Difference') diff --git a/06-Sum-Square-Difference/asm/Makefile b/06-Sum-Square-Difference/asm/Makefile new file mode 100644 index 0000000..68e7f4f --- /dev/null +++ b/06-Sum-Square-Difference/asm/Makefile @@ -0,0 +1,6 @@ +sumsq: sumsq.o + gcc sumsq.o -o sumsq +sumsq.o: sumsq.asm + nasm -w+all -f elf64 -g -F stabs sumsq.asm +clean: + rm -f sumsq sumsq.o diff --git a/06-Sum-Square-Difference/asm/sumsq b/06-Sum-Square-Difference/asm/sumsq new file mode 100755 index 0000000..d282ab2 Binary files /dev/null and b/06-Sum-Square-Difference/asm/sumsq differ diff --git a/06-Sum-Square-Difference/asm/sumsq.asm b/06-Sum-Square-Difference/asm/sumsq.asm new file mode 100644 index 0000000..cc9295a --- /dev/null +++ b/06-Sum-Square-Difference/asm/sumsq.asm @@ -0,0 +1,49 @@ +; Find the difference between (1+2+3+...+100)^2 and (1^2+2^2+3^2+...+100^2) +; I used another math trick here: +; The sum up to a number can be found using Gauss' Trick: +; 1 + 2 + 3 + ... + 10 -> (1+9) + (2+8) + (3+7) + (4+6) + 10 + 5 -> 5*10 +5 +; In general : (n/2)(n+1) + +extern printf +SECTION .data +flag: db "%d",10,0 ; "%d\n\0" + +SECTION .text + global main + global is_divisible + +main: + push rbp ; set up stack + mov rbp, rsp + + ; first square of sum (1+2+...100)^2 + mov rax, 50 ; sum = n/2 + mov rcx, 101 ; * (n + 1) + mul rcx + mul rax ; square it + push rax ; save it + + ; now sum the squares (1^2+2^2 ...) no short cut this time + xor r11, r11 ; This will store the sum, rcx will b n, + ; rax is just used to multiply + mov rcx, 0 ; n = 0, we add one first + +.loop inc rcx ; see? told you :) + mov rax, rcx ; move into multiply register + mul rax ; multiply by itself + add r11, rax ; add to the sum + cmp rcx, 100 + jne .loop + + pop rax ; sum of the squares is on the stack + sub rax, r11 ; take the difference, rax - r11 + ; square of the sum is bigger +.print mov rdi, flag ; arg 1 (format) + mov rsi, rax ; arg 2 (value) + mov rax, 0 ; no xmm registers + call printf wrt ..plt + pop rbp + + mov rax, 0 + ret + diff --git a/06-Sum-Square-Difference/asm/sumsq.o b/06-Sum-Square-Difference/asm/sumsq.o new file mode 100644 index 0000000..9b0a44b Binary files /dev/null and b/06-Sum-Square-Difference/asm/sumsq.o differ diff --git a/06-Sum-Square-Difference/sumsq b/06-Sum-Square-Difference/sumsq old mode 100644 new mode 100755 -- cgit v1.2.3