Flylib.com

Books Software

 
 
 

Java Virtual Machine (Java Series) - page 49

prev next contents
astore
store object reference in local variable
Jasmin Syntax

astore <varnum>
or
    wide
    astore <varnum>

In the first form, <varnum> is an unsigned integer in the range 0 to 0xFF. In the second (wide) form, <varnum> is an unsigned integer in the range 0 to 0xFFFF.

Stack

B efore

After
objectref
...
...

Description

Pops objectref (a reference to an object or array) off the stack and stores it in local variable <varnum>. The astore instruction takes a single parameter, <varnum>, an unsigned integer which indicates which local variable is used. <varnum> must be a valid local variable number in the current frame.

Example


aload 1    ; Push object reference in local variable 1 onto stack
astore 3	; and store it in local variable 3

Bytecode

For local variable numbers in the range 0-255, use:

Type

Description
u1
astore opcode = 0x3A (58)
u1
<varnum>
There is also a wide format for this instruction, which supports access to all local variables from 0 to 65535:

Type

Description
u1
wide opcode = 0xC4 (196)
u1
astore opcode = 0x3A (58)
u2
<varnum>
See Also

lstore , istore , dstore , fstore , wide

Notes

astore can also be used to store a returnAddress in a local variable. See the jsr instruction for more details.


prev next contents
Java Virtual Machine, by Jon Meyer and Troy Downing, O'Reilly Associates
prev next contents
baload
retrieve byte/boolean from array
Jasmin Syntax

baload

Stack

B efore

After
index
value
arrayref
...
...
...
Description

Retrieves a byte from a byte array, expands it to an integer and places it on the operand stack. arrayref is a reference to an array of bytes. index is an int. The arrayref and index are removed from the stack, and the 8-bit signed byte at the given index in the array is retrieved, sign-extended to a 32-bit int, and pushed onto the stack.

baload is also used to retrieve values from boolean arrays. In this case, arrayref is a reference to an array of booleans (see the newarray instruction) . If the entry at the given index is true , then the int 1 is pushed onto the stack, otherwise the int 0 is pushed onto the stack. In Sun's implementation, boolean arrays are actually stored as byte arrays, using one byte per boolean value. Other implementations might use packed arrays - or even int arrays - this is invisible to programs running on the JVM, which always use baload and bastore to access and store values in boolean arrays.

Example


; This is like the Java code:
;     byte x = arr[0];
; where x is local variable 2 and arr is a byte array in local variable 1
aload_1       ; load local variable 1 onto the stack
iconst_0      ; push the integer 0 onto the stack
baload        ; retrieve the entry
istore_2      ; store the entry in local variable 2

Exceptions

NullPointerException - arrayref is null

ArrayIndexOutOfBoundsException - index is < 0 or >= arrayref.length

Bytecode

Type

Description
u1
baload opcode = 0x33 (51)
See Also

iaload , laload , faload , daload , aaload , caload , saload , iastore , lastore , fastore , dastore , aastore , bastore , castore , sastore , newarray


prev next contents
Java Virtual Machine, by Jon Meyer and Troy Downing, O'Reilly Associates