while loop construct - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

while loop construct

Description:

general structure of a while loop control construct in byte code ... the result of dividing 0 by 0 - this means that rather than crashing a program ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 25
Provided by: socSta
Category:

less

Transcript and Presenter's Notes

Title: while loop construct


1
while loop construct
  • while loop construct has the form
  • while (condition)
  • loop statements

2
  • general structure of a while loop control
    construct in byte code
  • push values to be compared onto stack (1 or 2
    values depending)
  • use ifcc or if_xcmpcc as appropriate with label
    that labels code following loop statements
  • then code for loop statement block
  • then unconditional branch back to start of
    conditional test code (i.e. where we pushed
    comparison values onto stack)
  • then label before first instruction of rest of
    code following while loop
  • then rest of code that follows while loop

3
  • x is local var 1 and y is local var 2
  • while
  • iload 1 push x
  • bipush 10 push 10
  • if_icmpge next x gt 10 branch
  • iload 1 push x
  • iload 2 push y
  • iadd x y
  • istore 1 assign to x
  • goto while branch back to top
  • next
  • rest of code
  • example
  • while (x lt 10)
  • x x y

4
Implementation of loop counters
  • In many loop constructs - not just for loops - we
    use a counter
  • the obvious place for a counter value in byte
    code is in a local variable slot
  • on the basis of what we know this would seem to
    imply that to increment a local var by 1 that we
    would need to load the local var onto the stack,
    load constant 1 onto the stack and then add them
    together and then store value back into local var
    slot - 4 instructions required - yet we would
    need to execute updating of the counter every
    time round the loop

5
  • luckily byte code has an instruction that allows
    us to increment a local variable integer by some
    constant value directly, i.e. without moving
    local var onto stack
  • it has the form
  • iinc local_var increment integer increment
  • local_var is the number of the local var integer
    that we want to increment and increment is a byte
    value (-128-127) by which we want to increment
    the local var value

6
  • since it is a signed value we can use iinc to
    decrement values by using negative increment
    values
  • so we can use iinc to perform addition or
    subtraction of small values without using the
    stack, although we still need the stack for most
    arithmetic operations

7
For loop construct
8
  • For loop construct has the form
  • for (counter initialise counter test counter
    inc/dec)
  • loop statements
  • equivalent to
  • counter init
  • while (counter test)
  • loop statements
  • inc or dec counter

9
  • general structure of a for loop control construct
    in byte code
  • initialise counter value (load and store a value)
  • push values to be compared onto stack (2 values
    normally)
  • use ifcc or if_icmpcc as appropriate with label
    that labels code following loop statements
  • then code for loop statement block
  • then using iinc increment loop counter by
    appropriate amount

10
  • then unconditional branch back to start of
    conditional test code (i.e. where we pushed
    comparison values onto stack)
  • then label before first instruction of rest of
    code following while loop
  • then rest of code that follows while loop

11
Example for loop
  • for (i0 i lt 20 i)
  • // loop statements
  • equivalent to
  • i 0
  • while (i lt 20)
  • // loop statements
  • i
  • iconst_0
  • istore_1 i in local var 1
  • ForLoop
  • iload_1
  • bipush 20
  • if_icmpgt next
  • loop statements here
  • iinc 1 1
  • goto ForLoop
  • next

12
other comparison operations
  • we can compare values other than integer and
    reference values - also floats, doubles, or longs
  • the form of the instructions are
  • dcmpg or dcmpl compare doubles
  • fcmpg or fcmpl compare floats
  • lcmp compare longs
  • these instructions pop the top 2 values, of the
    same type, off the stack, compare them and places
    a result back on the stack

13
  • Comparison order as before
  • ltfirst pushedgt comp Op ltsecond pushedgt
  • where stack is
  • value2 lt-- stack top
  • value1
  • comparison instruction puts
  • 1 on top of stack if value1 gt value2
  • 0 on top of stack if value1 value2
  • -1 on top of stack if value1 lt value2

14
  • for dcmpg and fcmpg, if either of the 2 values
    represents the floating point value for
    Not-a-Number (NaN), then 1 is placed on the stack
  • for dcmpl and fcmpl, if either of the 2 values
    represents NaN, then a -1 is placed on the stack
  • NaN is not defined for longs so long comparisons
    only have the instruction lcmp

15
Not-a-Number
  • the standard for floating point numbers IEEE 754
    allows floating point values to take on a special
    value that represents values that are not genuine
    numbers e.g. the result of dividing 0 by 0 - this
    means that rather than crashing a program with a
    hardware error, the rest of a program can carry
    on even though one of the values is strictly
    invalid. The invalid value is represented by the
    floating point value used to represent
    Not-a-Number. The software can deal with that
    value as it sees fit, but without terminating the
    whole software

16
  • notice that the comparison operation places a
    comparison result value onto the stack you will
    still then need to check to see what that value
    is using one of the conditional branch
    instructions we covered last week
  • to use a long/double/float comparison in a
    conditional branch thus requires you to
  • put 2 values to be compared onto stack
  • use xcmpg or xcmpl, lcmp, etc as appropriate
  • then use conditional branch instruction ifcc to
    branch using condition in cc appropriate to the
    relational comparison you are using

17
  • Example
  • dload 2 push double at local var 2 onto
    stack
  • dconst_1 push double value 1.0
  • dcmpg compare them
  • ifeq same if value on stack 0 then local var
  • 21.0 and branch to label same
  • or could use ifge to test whether value on stack
    0 or 1, and thus whether local var 2 gt 1.0, etc.

18
Arithmetic operations
  • byte code supports the standard arithmetic
    operations of
  • addition, subtraction, multiplication, division,
    remainder and negation (-1)
  • these operations are supported on all the
    arithmetic types of int, long, float and double
  • all the operations except negation expect their
    operands to occupy the top 2 entries on the stack
    and place the result on the top of the stack
  • negation works on only one operand value on the
    stack and places result back on top of the stack

19
  • so before you execute an operation you will need
    to ensure that the values to be operated on are
    on the stack
  • in subtraction, division and remainder the order
    of the values on the stack is important
  • order is as usual
  • ltfirst pushedgt operation ltsecond pushedgt
  • Assume stack is
  • value2 lt---- top of stack
  • value1
  • thus subtraction gives value1 - value2
  • division and remainder value1 (div or rem) value2

20
  • operations only work on values of the same type
    e.g. both floats or ints
  • the type of the result is always the same as the
    type of the operands
  • the byte code mnemonics for the arithmetic
    operations are
  • xadd where in each case x stands for
  • xsub one of i, l, f or d (int, long, float or
  • xmul double)
  • xdiv
  • xrem
  • xneg

21
Bitwise operations
  • Bitwise operations manipulate the bits that make
    up an int or long value - some apply a standard
    boolean operation to each of the bits.
  • The following operations are supported the
    boolean operations of and, or, exclusive or, and
    also shift bits right, shift bits left and
    arithmetic shift right
  • all expect 2 operands to be on the top of the
    operand stack
  • the order of operations for and, or, xor is
    unimportant

22
  • with the shift operations
  • for longs - the top of the stack is an int whose
    bottom 6 bits represents the number of times the
    shift operation is to take place (thus maximum of
    63 shifts) , the next entry is the long that is
    to be shifted
  • for ints - the top of the stack is an int whose
    bottom 5 bits represents the number of times the
    shift operation is to take place (thus maximum of
    31 shifts) , the next entry is the int that is to
    be shifted

23
  • shift left - shift bits to left by one place
    losing top most bit and pushing bit 0 into least
    significant bit position - equivalent to
    multiplication by 2
  • unsigned shift right - shift bits to right by one
    losing least significant bit and pushing 0 into
    most significant bit position
  • shift right (arithmetic shift) - shift bits right
    as before but repeats the most significant bit
    into the new most significant bit position - this
    preserves the sign of the binary value and is
    equivalent to division by 2

24
  • the form of the instructions are
  • xor (bitwise or), xand (bitwise and), xxor
    (bitwise exclusive or), xshl (shift left), xshr
    (arithmetic shift right), xushr (unsigned shift
    right)
  • in each case the first italicised x is either an
    i (for int) or l (for long)
Write a Comment
User Comments (0)
About PowerShow.com