S&P 500, Nasdaq close higher to end days-long slump