I would like to call a contract and deal with the return values manually using EVM assembly in Solidity. For example, this should simply add two numbers together.
contract Test1 {
function add(int a, int b) returns(int){ //Simply add the two arguments and return
return a+b;
}
function() returns (int){ //If the function signature doesn't check out, return -1
return -1;
}
}
contract Test2 {
Test1 test1;
function Test2(){ //Constructor function
test1 = new Test1(); //Create new "Test1" function
}
function test(int a, int b) constant returns (int c){
address addr = address(test1); //Place the test1 address on the stack
bytes4 sig = bytes4(sha3("add(int256,int256)")); //Function signature
assembly {
let x := mload(0x40) //Find empty storage location using "free memory pointer"
mstore(x,sig) //Place signature at begining of empty storage
mstore(add(x,0x04),a) //Place first argument directly next to signature
mstore(add(x,0x24),b) //Place second argument next to first, padded to 32 bytes
call(5000, addr, 0, //Issue call, providing 5k gas and 0 value to "addr"
x, 0x44, add(x,0x80), 0x20) //Inputs start at location "x" and are 68 bytes long, outputs start 128 bytes after x, and are 32 bytes long
c := mload(add(x,0x80)) //Assign output value to c
mstore(0x40,add(x,0x100)) // Set storage pointer to empty space
}
}
function test2(int a, int b) constant returns(int c){ //Make sure the Test1 function works properly
return test1.add(a,b); // (It does)
}
}
The issue is that this is returning an Out-of-Gas error, originating from the call...
line.
How can I fix this issue?