Skip to content

Commit

Permalink
implement SUB and MLOAD opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor committed Jul 13, 2018
1 parent d026a2d commit e42e7d2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/__tests__/integration.spec.ts
Expand Up @@ -11,7 +11,7 @@ import {
describe("EMV-TS", () => {
it("should work", () => compareWithReferentialImpl("60606040523415600e"));

it.skip("should work with more complicated bytecode", () => {
it("should work with more complicated bytecode", () => {
const bin = fs.readFileSync(join(__dirname, "./contracts/DumbContract.bin"), "utf-8");
const abi = JSON.parse(fs.readFileSync(join(__dirname, "./contracts/DumbContract.abi"), "utf-8"));
const contract = web3.eth.contract(abi).at("0x0");
Expand Down
9 changes: 9 additions & 0 deletions lib/opcodes/__tests__/arithmetic.spec.ts
Expand Up @@ -15,6 +15,15 @@ describe("arithmetic opcodes", () => {
));
});

describe("SUB", () => {
it("SUB with overflow", () =>
compareWithReferentialImpl(
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03",
));

it.skip("SUB with minus sign");
});

describe("DIV", () => {
it("div with overflow", () =>
compareWithReferentialImpl(
Expand Down
4 changes: 4 additions & 0 deletions lib/opcodes/__tests__/memory.spec.ts
Expand Up @@ -7,4 +7,8 @@ describe("memory opcodes", () => {
it("with more than 1 word size data", () =>
compareWithReferentialImpl("7f4e616d655265670000000000000000f0000b0000000000000000000000000000606052"));
});

describe("MLOAD", () => {
it("simple", () => compareWithReferentialImpl("6060600052600051"));
});
});
12 changes: 9 additions & 3 deletions lib/opcodes/arithmetic.ts
@@ -1,4 +1,4 @@
import { Opcode, notImplementedError } from "./common";
import { Opcode } from "./common";
import { IMachineState } from "../VM";
import { MAX_UINT_256 } from "../utils/bytes";
import { BN } from "bn.js";
Expand Down Expand Up @@ -37,8 +37,14 @@ export class SubOpcode extends Opcode {
static id = 0x03;
static type = "SUB";

run(_state: IMachineState): void {
notImplementedError();
run(state: IMachineState): void {
const arg1 = state.stack.pop();
const arg2 = state.stack.pop();

const result = arg1.sub(arg2).mod(MAX_UINT_256);

state.pc += 1;
state.stack.push(result);
}
}

Expand Down
13 changes: 10 additions & 3 deletions lib/opcodes/memory.ts
@@ -1,6 +1,7 @@
import { IMachineState, IEnvironment } from "../VM";
import { Opcode, notImplementedError } from "./common";
import { Opcode } from "./common";
import { arrayCopy } from "../utils/arrays";
import { BN } from "bn.js";

/**
* Stores full word in memory.
Expand Down Expand Up @@ -29,8 +30,14 @@ export class MLoadOpcode extends Opcode {
static id = 0x51;
static type = "MLOAD";

run(_state: IMachineState): void {
notImplementedError();
run(state: IMachineState): void {
const address = state.stack.pop().toNumber();
const valueRaw = state.memory.slice(address, address + 32);

const value = new BN(valueRaw, undefined, "be");

state.stack.push(value);
state.pc += 1;
}
}

Expand Down

0 comments on commit e42e7d2

Please sign in to comment.