I tried to get a better grip on michelson but found myself fail at a simple piece of michelson code. Is anyone willing to help on the logic of below code? Thanks a lot!
parameter (pair nat nat);
storage (pair nat nat);
code{
CAR; UNPAIR;
DUP; DIP { ADD };
PAIR;
NIL operation;
PAIR};
It says "If called with the initial storage (Pair 0 0) and the parameter (Pair 15 9) then the final storage will be (Pair 15 24).", however I cannot think it through.
==========
My attempt:
- CAR, take the parameter (15,9);
- UNPAIR, gets it unpair meaning to separate the pair as two numbers 15 9; so that's 15:9:(0,0):[]
- DUP, means 15:15:9:(0,0):[]
- DIP, means protect the top stack,i.e. the 15. In plain language, that is, keep it there, don't do anything with it.
- ADD, means take from the second left number 15, add all the numbers down to the bottom of the stack. (It seems illegal, as integer adds on pair)
- Head gets exploded...
Reference: https://tezos.gitlab.io/michelson-reference/#instr-DIP