Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequencing, Selection, and Loops in Machine Language

Similar presentations


Presentation on theme: "Sequencing, Selection, and Loops in Machine Language"— Presentation transcript:

1 Sequencing, Selection, and Loops in Machine Language
Computer Science 101 Sequencing, Selection, and Loops in Machine Language

2 Sequencing Supported by the linear structure of memory
Begin <statement 1> <statement n> End Sequencing Supported by the linear structure of memory After the PC is incremented, the instruction fetched is the the next one in a linear sequence A normal termination occurs when the HALT instruction is fetched and executed

3 If <condition> Begin <statement 1> <statement n> End Selection Selection (one-way if) statements use two types of instructions A comparison A conditional jump

4 Comparisons Require two operands (example, X > Y)
The COMPARE opcode 0111 compares the data in the memory cell at the given address to the contents of the data register R The result of the comparison is deposited in the condition code register CCR

5 The Condition Code Register
Comparison State of CCR CON(X) > R CON(X) = R CON(X) < R 1 1 1 CON(X) means contents of memory cell at address X

6 Example: Compare X and Y
IR 1 1 1 1 1 1 Y X R 1 1 CCR 1 X = Y RAM 1 1

7 Example: Compare X and Y
IR 1 1 1 1 1 1 Y X R 1 1 CCR 1 X > Y RAM 1

8 Example: Compare X and Y
IR 1 1 1 1 1 1 Y X R 1 1 CCR 1 X < Y RAM 1

9 Steps Required Comparisons require two steps:
Load the second operand into register R Compare to the first operand (a memory address) LOAD Y COMPARE X

10 JUMP Instructions The JUMP instructions check the condition code and transfer control to the instruction at location X if the code is set in a certain way. Binary opcode Operation CCR JUMP X Any value JUMPGT X 100 JUMPEQ X 010 JUMPLT X 001 JUMPNEQ X Any value but 010

11 Example If Statement High-level pseudocode Low-level pseudocode
// Absolute value If X < 0 Set X to -X // Absolute value 1. LOAD ZERO 2. COMPARE X 3. JUMPGT LINE 6 4. SUBTRACT X 5. STORE X 6. Rest of code These two lines are skipped if X > 0

12 In Machine Code LOAD 000000000000 1 COMPARE 000000000001 1 1 1 1 1 1
1 COMPARE 1 1 1 1 1 1 JUMPGT 1 1 1 1 SUBTRACT 1 1 1 1 1 STORE 1 1 1 1 OUT 1 1 1 1 1 1 HALT 1 1 1 1 X 1 1 1 ZERO

13 If/Else and Loops Comparisons and jumps are also used for if/else statements and loops Typically use two jumps, one conditional and the other unconditional

14 Example: Output Sum of 1 .. 10 Set sum to 0 Set count to 1
While count <= 10 do Set sum to sum + count Increment count Output sum 1 clear sum 2 load one 3 store count 4 load count 5 compare eleven 6 jumpeq line 12 8 add sum 9 store sum 10 increment count 11 jump line 4 12 out sum 13 halt

15 Example: Output Sum of 1 .. 10 1 clear sum 2 load one 3 store count
4 load count 5 compare eleven 6 jumpeq line 12 8 add sum 9 store sum 10 increment count 11 jump line 4 12 out sum 13 halt (zero) (one) (eleven) (sum) (count)


Download ppt "Sequencing, Selection, and Loops in Machine Language"

Similar presentations


Ads by Google