-->

MIC IJVM simple sum of 2 digits

2019-08-30 13:30发布

问题:

just a simple question regarding calculations in IJVM as I couldn't find the solution in their documentation.

Suppose we need to perform the following calculation:

BIPUSH 0x32 // PUSH 2
BIPUSH 0x33 // PUSH 3
IADD // PUSH sum(2,3)
OUT // output: "e"

IADD ----> Pop two words from stack; push their sum

I know the solution is likely straight forward, but for the life of me I can't recall on how to convert the addition/output to the actual digits. How to make it output "5" instead of this stupid "e"? :)

Cheers.

回答1:

As expected, the answer to this problem was very simple. That's very much contradictory to actually finding the answer as no one seem to bother to mention this anywhere in the docs. Awesome.

Solution:

BIPUSH 0x32 // PUSH 2
BIPUSH 0x33 // PUSH 3
IADD // PUSH sum(2,3)
DUP
BIPUSH 0x30 // PUSH 0
ISUB // subtract 0
OUT 

Or the actual code:

plus:
    ILOAD X
    ILOAD Y
    IADD
    DUP
    BIPUSH 0x30
    ISUB
    GOTO return // Dominykas Tautkus. Linkėjimai prodekanui. :)

Subtracting 0 after performing addition forces it to treat this as an actual mathematical task with integers so to speak.