Simple example:
Let's say I have a function that takes a fixed size array, finds the largest number in it, and returns another fixed size array in memory with the largest number as the last index:
function sortFixedArray(uint256[6] memory array) public pure returns
(uint256[6] memory sortedArray) {
sortedArray = array; // duplicate parameter array in memory
uint ceiling;
uint last;
uint highIndex;
for (uint i = 0; i < 6;) {
last = sortedArray[5];
if (sortedArray[i] > 1 && sortedArray[i] > ceiling) {
ceiling = sortedArray[i];
highIndex = i;
}
unchecked { i++; }
}
sortedArray[5] = ceiling;
if (highIndex < 5) {
sortedArray[highIndex] = last;
}
sortedArray[5] = ceiling;
ceiling = 0;
return sortedArray;
}
As you can see, I am checking the index values of the array with a for loop and
sortedArray[i]
. But assembly doesn't recognize sortedArray[i]
, so how would I access each index's value?