Find all pairs of real numbers $(a,b)$ so that there exists a polynomial $P(x)$ with real coefficients and $P(P(x))=x^4-8x^3+ax^2+bx+40$.
1. Suppose \( P(x) \) is a polynomial of degree \( n \). Then \( P(P(x)) \) will be a polynomial of degree \( n^2 \). Given that \( P(P(x)) = x^4 - 8x^3 + ax^2 + bx + 40 \), we know that the degree of \( P(P(x)) \) is 4. Therefore, \( n^2 = 4 \) which implies \( n = 2 \). Hence, \( P(x) \) is a quadratic polynomial.

2. Since \( P(P(x)) \) is monic (leading coefficient is 1), \( P(x) \) must also be monic. Let \( P(x) = x^2 + cx + d \).

3. We need to expand \( P(P(x)) \):
   \[
   P(P(x)) = P(x^2 + cx + d) = (x^2 + cx + d)^2 + c(x^2 + cx + d) + d
   \]
   Expanding \( (x^2 + cx + d)^2 \):
   \[
   (x^2 + cx + d)^2 = x^4 + 2cx^3 + (c^2 + 2d)x^2 + 2cdx + d^2
   \]
   Expanding \( c(x^2 + cx + d) \):
   \[
   c(x^2 + cx + d) = cx^2 + c^2x + cd
   \]
   Adding these together:
   \[
   P(P(x)) = x^4 + 2cx^3 + (c^2 + 2d)x^2 + 2cdx + d^2 + cx^2 + c^2x + cd + d
   \]
   Simplifying:
   \[
   P(P(x)) = x^4 + 2cx^3 + (c^2 + c + 2d)x^2 + (2cd + c^2)x + (d^2 + cd + d)
   \]

4. We are given that \( P(P(x)) = x^4 - 8x^3 + ax^2 + bx + 40 \). Equate the coefficients of corresponding powers of \( x \):
   \[
   \begin{cases}
   2c = -8 \\
   c^2 + c + 2d = a \\
   2cd + c^2 = b \\
   d^2 + cd + d = 40
   \end{cases}
   \]

5. From the first equation, solve for \( c \):
   \[
   2c = -8 \implies c = -4
   \]

6. Substitute \( c = -4 \) into the remaining equations:
   \[
   \begin{cases}
   c^2 + c + 2d = a \implies 16 - 4 + 2d = a \implies 2d + 12 = a \\
   2cd + c^2 = b \implies 2(-4)d + 16 = b \implies -8d + 16 = b \\
   d^2 + cd + d = 40 \implies d^2 - 4d + d = 40 \implies d^2 - 3d = 40
   \end{cases}
   \]

7. Solve the quadratic equation \( d^2 - 3d - 40 = 0 \):
   \[
   d^2 - 3d - 40 = 0 \implies (d - 8)(d + 5) = 0 \implies d = 8 \text{ or } d = -5
   \]

8. For \( d = 8 \):
   \[
   a = 2d + 12 = 2(8) + 12 = 28 \\
   b = -8d + 16 = -8(8) + 16 = -64 + 16 = -48
   \]
   Thus, one pair is \( (a, b) = (28, -48) \).

9. For \( d = -5 \):
   \[
   a = 2d + 12 = 2(-5) + 12 = -10 + 12 = 2 \\
   b = -8d + 16 = -8(-5) + 16 = 40 + 16 = 56
   \]
   Thus, another pair is \( (a, b) = (2, 56) \).

The final answer is \( \boxed{(28, -48), (2, 56)} \).