Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Control Structures.

Similar presentations


Presentation on theme: "Chapter 4 Control Structures."— Presentation transcript:

1 Chapter 4 Control Structures

2 4.1.1 Fetch-Execute Revisited
The real advantage of a computer is its ability to perform tasks over and over again. Program code is stored as a sequence of successive machine instructions, stored as sequential elements. The computer first “fetches” the current instruction from memory, interprets and executes, then updates its notion of the “current instruction.”

3 4.1.1 Fetch-Execute Revisited
Program Counter. The number of the “current instruction.” Every time that the fetch-execute cycle occurs, the value stored in the PC goes to the next instruction. Some operations are defined by more than one byte.

4 4.1.1 Fetch-Execute Revisited
bipush (0x10) pushes a byte onto the stack The opcode is followed by a single byte-to-be-pushed. sipush (0x11) Followed by two bytes (a short) to be pushed. iload Followed by one or two bytes describing the local variable to be loaded.

5 4.1.1 Fetch-Execute Revisited
The fetch-execute cycle needs to be smart enough to fetch several bytes The PC must be updated to reflect the size of the instruction fetched.

6 4.1.2 Branch instructions and labels
A statement that can cause the PC to change is called a “branch” instruction. A branch instruction can go anywhere. Labels are used to refer to individuals instructions. To adjust the PC, one uses an appropriate statement and then gives the label of the target instruction.

7 4.1.2 Branch instructions and labels
A label is just a line by itself holding an optional part of any instruction, a word followed by a colon [:] that makes a particular instruction. To transfer control to a given location, use that label.

8 4.1.3 “Structured Programming” a Red Herring
The PC is a register. When a particular value is placed into the PC, the machine will “go to” that location and execute this.

9 4.1.3 “Structured Programming” a Red Herring
Modern “structured programming” uses high-order control structures like loops and conditional statements. At the level of the machine code, any change reflecting different computations must inherently be expressed through changes to the PC, thus requiring an implicit “goto”!

10 4.1.3 “Structured Programming” a Red Herring
Programs should be modular and should have a single defined starting point and a single defined exit point. “single-entry/single-exit” High level languages will often prevent you from violating the single-entry/exit rule by their design. The same principles can, and should, be applied to assembly language programming in general.

11 4.1.3 “Structured Programming” a Red Herring

12 4.1.4 High-level Control Structures and their Equivalents

13 4.1.4 High-level Control Structures and their Equivalents
Need to decide at the beginning of each block whether or not the block needs to be executed. If the counter is greater than zero, the block should be executed. If the counter is <= 0, do not execute the loop and go to the remainder of the program. ifgt pop – and – if – > 0 – goto

14 4.1.4 High-level Control Structures and their Equivalents
As long as the initial value for the loop index is greater than zero, it will work. for (i=-1; i>0; i++) never executes.

15 4.2.1 Unconditional Branches
Unconditional branch (or goto) transfers control to the label designated as an argument.

16 4.2.2 Conditional Branches Six basic conditional branches.
Pops an integer off the stack and comparing whether the popped integer is greater, less than, or equal to zero.

17 4.2.3 Comparison Operations
lcmp pops two longs and pushes the integer value 1, 0, or -1.

18 4.2.3 Comparison Operations
Floats and doubles IEEE 754 allows “not a number” NaN. Square root of a negative number, or divide by zero. The JVM and jasmin provide two separate comparison instructions. fcmpg pushes 1, 0 -1 as expected, except that if either value is NaN, the result is 1. If either value is NaN, fcmpl returns -1. dcmpl and dcmpg for doubles.

19 4.2.3 Comparison Operations

20 4.2.4 Combination Operations
Short and boolean must be treated as int types in computation. Jasmin does not provide a way to compute the result of comparing two integers. Uses a built-in conditional branch. Instead of pushing the results of the comparison, the program immediately jumps (or not) to the appropriate location by modifying the program counter.

21 4.2.4 Combination Operations

22 4.3.1 If Statements

23 4.3.1 If Statements

24 4.3.1 If Statements Complex boolean conditionals can be handled through appropriate use of the iand, ior, etc instructions or through repeated comparisons.

25 4.3.1 If Statements

26 4.3.2 Loops There are two basic kinds of loops: Test at the end.
Equivalent to the C, C++, or Java do/while loop.

27 4.3.2 Loops Test the condition at the beginning

28 4.3.2 Loops do/while structure but entering the loop at the bottom via an unconditional branch.

29 4.3.2 Loops The equivalence of while and for loops.

30 4.3.2 Loops

31 4.3.3 The Details of Branch Instructions
Instead of strong labels, the computer actually stores offsets. goto instruction (opcode 0xA7) is followed in bytecode by a signed, two-byte (short) integer. This integer is a change to the program counter, the PC is changed to PC + offset. Negative offsets step control back to a previous statement. Positive jumps forward. An offset value of zero would create an infinite loop.

32 4.3.3 The Details of Branch Instructions

33 4.3.3 The Details of Branch Instructions
With two bytes, no jump can be longer than approximately 32,000 bytes. goto_w instruction “wide goto” (0xC8) Followed by a full-sized integer, jumps forward or back up to two billion or so bytes. JVM methods are not allowed to be more than 216 (about 64,000) bytes long, this new opcode solves the problem. A conditional (wide) branch to a distant location can be simulated by a branch around a branch.

34 4.3.3 The Details of Branch Instructions

35 4.3.3 The Details of Branch Instructions
The most serious problem with using branch statements it that they do not, in any way, provide the programmer with local block structures. All local variables (and the stack) retain their values before, during, and after a jump.

36 4.4.1Problem Definition Example: Syracuse Numbers
If the number N you have is 1, stop. If the number N you have is odd, let N be 3N + 1 and repeat. If the number N you have is even, let N be N/2 and repeat.

37 4.4.1 Problem Numbers No solution for how many steps it takes or even whether it will always go to 1. All numbers less than several billion will converge to one and thus end.

38 4.4.1 Problem Numbers

39 4.4.2 Design

40 4.4.2 Design

41 4.4.2 Design This entire block of code will in turn be used inside a while-loop structure.

42 4.4.3 Solution and Implementation

43 4.5 Table Jumps A Java switch statement.
Any multiway branch can be treated as equivalent to a set of two-way branches (such as if/else statements) and written accordingly. JVM has two shortcuts that can make the code simpler and faster to execute under certain conditions. The computer will go to any of several destinations, depending upon the value at the top of the stack. lookupswitch instructions consists of a set of value:Label pairs.

44 4.5 Table Jumps

45 4.5 Table Jumps

46 4.5 Table Jumps

47 4.5 Table Jumps The 'default' branch is mandatory in JVM machine code (unlike in Java). The branch statements. The values stored are offsets from the current program counter; unlike most other statements, the offsets are stored as four byte quantities, allowing for jumps to “distant” locations within the method. tableswitch Another shortcut operation for multiway decisions. By defining the low and high ends of the spectrum, the rest can be filled in as a table.

48 4.5 Table Jumps

49 4.5 Table Jumps

50 4.5 Table Jumps

51 4.5 Table Jumps lookupswitch opcode (0xAB)
Involves a four-byte integer (value) and then a corresponding four-byte offset to be taken if the integer matches the top of the stack. tableswitch opcode byte (0xAA), The values can be computed from the starting and ending values. Branches are ordered low, low + 1, low +2, ... high.

52 4.6.1 Basic Instructions Subroutines
After a block of code is executed, it will automatically transfer control back to a single and unchangeable point. Two modifications: A branch instruction that also stores (somewhere) the value of the program counter before the jump. Another kind of branch instruction that will return to a variable location.

53 4.6.1 Basic Instructions jsr (Jump to SubRoutine) instruction
jsr and jsr_w Control is immediately passes (as with a goto) to the label whose offset is stored in the bytecode. The machine calculates the value (PC +3), the address of the next instruction that immediately follows the jar instruction itself. Pushed onto the stack as a normal 32-bit (4-byte) quantity is controlled then passed to the label.

54 4.6.1 Basic Instructions Returning from subroutines.
The return instruction examines the top of the stack and use the value stored there, which was pushed by a jsr instruction as the location to which to return. Control returns to the main program, and computation proceeds.

55 4.6.1 Basic Instructions On the JVM the ret instruction does not examine the stack for the location to return, but instead accepts as an argument the number of a local variable. Once done, it leaves this location untouched. Trying to perform computations on memory addresses is dangerous In Java it is illegal.

56 4.6.2 Examples of Subroutines
Subroutine max(int A, int B)

57 4.6.2 Examples of Subroutines

58 4.6.2 Examples of Subroutines
PrintString example

59 4.6.2 Examples of Subroutines
astore_1 Example of the ?store_N for an address. For any subroutine, it is important to be aware of what local variables are and aren't used by the subroutine, sine they're in the same set of local variables as used by the main program.

60 4.6.2 Examples of Subroutines
Using subroutines

61 4.6.2 Examples of Subroutines

62 4.6.2 Examples of Subroutines
Complete program (with one error).

63 4.6.2 Examples of Subroutines

64 4.6.2 Examples of Subroutines

65 4.7.1 Problem Definition Monte Carlo Estimation of pi (3.14159).
How did mathematicians figure out its value? The circle has an area of pi. The proportion of darts that land inside the circle with be pi/4.

66 4.7.1 Problem Definition Monte Carlo Estimation of pi (3.14159).
If we threw 10,000 darts at the diagram, we would expect about 7,854 of them to land inside the circle. Monte Carlo simulation A very powerful way of exploring a large probability space when you're not sure of the exact parameters of the space.

67 4.7.2 Design We will need random integers.
Need two counters, one for the number of darts thrown and one for the number of darts that land inside the circle. Inside the circle if and only if x2 + y2 ≤ 13 After this block has been executed a large enough number of times, the ratio of successes to total executions should approximate pi/4.

68 4.7.2 Design

69 4.7.2 Design

70 4.7.2 Design

71 4.7.2 Design

72 4.7.3 Solution and Implementation

73 4.7.3 Solution and Implementation

74 4.7.3 Solution and Implementation

75 4.7.3 Solution and Implementation

76 4.7.3 Solution and Implementation

77 4.7.3 Solution and Implementation

78 4.7.3 Solution and Implementation

79 Chapter Review The location of the instruction currently being executed is stored in the program counter (PC). Certain instructions can alter the contents of the PC. These statements are often called branch statements or goto statements. Targets of a branch must have a label. goto statement executes an unconditional branch.

80 Chapter Review if?? family of conditional branches may or may not transfer control to their target. The ?cmp family of statements are used to compare types other than integers. Pushes an integer with value 1, 0 or -1, depending on whether the first argument is greater then, equal to, or less than the second. The if_icmp?? instructions combine the functions of an icmp statement with the conditional branches of the if?? family into a single instruction.

81 Chapter Review if/else statements and while loops must be implemented at the assembly language using conditional and unconditional branches. lookupswitch and tableswitch statements provide a way to perform multiway branch statements. Subroutines work by pushing the current value on the program counter onto the stack, and then by returning to the previously saved location at the end of the subroutine.


Download ppt "Chapter 4 Control Structures."

Similar presentations


Ads by Google