You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
999 B
ArmAsm

.global func
.section .text
func: @ Find the difference between the sum of the squares and the square of the sum, for the first hundred numbers.
@ (a+b)^2 = a^2 + b^2 + 2ab
@ (a+b+c)^2 = a^2 + b^2 + c^2 + 2(ab +bc + ca)
@ Therefore, the difference must be 2(ab + ac + ad .... + bc + bd + be ... + cd + ce .....)
MOV R0,#0
MOV R2,#1 @ 'a' from the equation above
MOV R3,#2 @ 'b' from the equation above
mainloop:
MUL R1,R2,R3 @ 'a' * 'b' - keep in mind we need to do this for all possible 2-number permutations in the sequence
ADD R0,R0,R1 @Add the result to R0, which will be returned
incr_b:
ADD R3,R3,#1 @ Increments 'b'
chk_b:
CMP R3,#100 @ If 'b' is less than or equal to 100, we continue the multiplication
BLE mainloop
incr_a:
ADD R2,R2,#1 @ Increments 'a'
new_b: @This is a way for me to store R2+1 into R3
ADD R2,R2,#1
MOV R3,R2
SUB R2,R2,#1
chk_a:
CMP R2,#100
BLT mainloop
mul2:
LSL R0,R0,#1 @Bitwise left shit by 1 - Multiplies by 2
return:
BX LR
.section .data