Bitcoin scriptSig with two transactions to unlock the scriptPubKey For example, the bitcoin script of a scriptPubKey that consists of OP SUB &lt; 100 &gt; OP EQUAL, and is used to lock an output of a transaction. OP SUB pops the top two elements in the stacks, subtracts them, then pushes back the result. &lt; X &gt; pushes value X on the stack.
Also, the following, a scriptSig script that consists of &lt; 200 &gt;&lt; 100 &gt;, which is used as an unlocking script to spend that output.
In that case, will the scriptSig succeed to unlock the scriptPubKey thus allow the transaction to spend the money? Because of how Bitcoin combines the scripts and how it executes them, like what are the contents of the stack at each step of the execution?
You cannot put your plain script in scriptPubKey hence it's considered non-standard and will not be relayed/mined. You need to hash your custom script, put script hash in P2SH output, and reveal the script later when you spend it (in scriptSig).
Here's a step-by-step demonstration:
OP_SUB &lt; 100 &gt; OP_EQUAL


Convert your script to hexadecimal representation:

a. OP_SUB is encoded as 0x94 (in hex)

0x94

b. 100(dec) is encoded as 0x6400 (in little endian) plus one-byte length prefix

0x94026400

c. OP_EQUAL is encoded as 0x87 (in hex)

0x9402640087


Hash your script in order to get script hash:


RIPEMD160(SHA256(0x9402640087)) =
0xa038dd58be8e9c3bd9f26fe585dcdfe85d70ac4d


Your P2SH scriptPubKey is:


OP_HASH160 a038dd58be8e9c3bd9f26fe585dcdfe85d70ac4d OP_EQUAL

In order to spend/unlock this output your scriptSig should be:

&lt; 200 &gt; &lt; 100 &gt; &lt; OP SUB &lt; 100 &gt; OP EQUAL &gt;

In Hex:

0x02C800026400059402640087

Please note that arithmetic operations like OP_ADD and OP_SUB can only accept 32-bit integers, so if any input encountered longer than 4 bytes, the script must abort and fail.
Feel free to learn more about P2SH here