I want to split bytes32 into multiples of 8 (in assembly). I have the following code(After referring here):
function testAssembly(bytes32 _msg) public pure returns (bytes8 i, bytes8 j, bytes8 k, uint256 m, bytes memory size){
assembly {
let i_ptr := mload(0x40)
let j_ptr := mload(0x62)
let k_ptr := mload(0x92)
m := calldataload(4)
//size := msize
calldatacopy(i_ptr,0x4,8)
calldatacopy(j_ptr,0x9,8)
calldatacopy(k_ptr,0x11,8)
i := mload(i_ptr)
j := mload(j_ptr)
k := mload(k_ptr)
}
return (i,j,k,m,size);
}
function testAssemblyArr(bytes32 _msg) public pure returns (bytes8 i, bytes8 j, bytes8 k, uint256 m, bytes memory size){
bytes8[3] memory y = [bytes8(0), 0,0];
assembly {
m := calldataload(4)
//size := msize //(*)
mstore(y,_msg)
mstore(add(y,0x8),_msg)
mstore(add(y,0x10),_msg)
}
return (y[0],y[1],y[2],m,size);
}
I have following input (Remix): "0x1212121212121212343434343434343456565656565656567878787878787878"
Expected output(for both):
0: bytes8: i 0x1212121212121212
1: bytes8: j 0x3434343434343434
2: bytes8: k 0x5656565656565656
Observed output:
testAssembly:
0: bytes8: i 0x1212121212121212
1: bytes8: j 0x3434345656565656
2: bytes8: k 0x3434345656565656
3: uint256: m 8173559240281143207206663486900115636630769273905500857318033424334900983928
4: bytes: size 0x
testAssemblyArr:
0: bytes8: i 0x1212121212121212
1: bytes8: j 0x5656565656565656
2: bytes8: k 0x0000000000000000
3: uint256: m 8173559240281143207206663486900115636630769273905500857318033424334900983928
4: bytes: size 0x
If i change function parameter of testAssemblyArr to bytes then it gives the following output:
0: bytes8: i 0x0000000000000000
1: bytes8: j 0x0000000000000000
2: bytes8: k 0x0000000000000000
3: uint256: m 32
4: bytes: size 0x
In the above output, (Ignore m, it gives correct output if its return type is changed to bytes32). For calldatacopy() function, I used both integer and hexadecimal values but it gives incorrect values. How should i go about this to get expected results?
Also when you uncomment line *(msize), it gives "out of gas" error in ganache and crashes the browser(tab) in Remix.