2

Suppose we need to make a sum of two binary numbers in ijvm, for example:

100 + 11 = 111

Translating all in ijvm:

ILOAD arg1 //100
ILOAD arg2 // 11                
IADD
ISTORE i

Without making any changes to the code, what the content of the variable i ? as they represented in ijvm numbers? A simple add enough to make a sum? Why do I need a shift right or left?

1 Answers1

1

The byte code would look like

0x15 0x02
0x15 0x03
0x60
0x36 0x01

where 0x02 is the offset from LV to arg1, 0x03 is the offset from LV to arg2, and 0x01 is the offset from LV to i.

The local variable i would have 111 in it, if local variable arg1 has 100 and local variable arg2 has 11.

The stack would look like this before the IADD

011 <- SP
100 <- operand stack starts here
011 <- LV + 3
100 <- LV + 2
xxx <- LV + 1
xxx <- LV

The stack would look like this after the ISTORE

011 <- LV + 3
100 <- LV + 2
111 <- LV + 1
xxx <- LV

The code you supplied will perform the addition. You do not need to shift.

Response to comment about overflow:

Create a grid for adding two 2-bit numbers. 00 01 10 11 across the top and along the side. | 00 01 10 11| 00| | 01| | 10| | 11| |

In the grid, show the result of the arithmetic, excluding overflow. Circle the entries that cause overflow. Such a table might give you a clue for detecting overflow, without looking at the carry bit.

downeyt
  • 1,206
  • 2
  • 12
  • 23
  • Assuming that you enter two values that exceed the permitted 32-bit, how can I manage the phenomenon of overflow? –  Nov 20 '15 at 09:42