150 likes | 237 Views
Computer Science 101. Sequencing, Selection, and Loops in Machine Language. 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
E N D
Computer Science 101 Sequencing, Selection, and Loops in Machine Language
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
If <condition> Begin <statement 1> … <statement n> End Selection Selection (one-way if) statements use two types of instructions • A comparison • A conditional jump
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
The Condition Code Register ComparisonState of CCR CON(X) > R CON(X) = R CON(X) < R 1 0 0 0 1 0 0 0 1 CON(X) means contents of memory cell at address X
Example: Compare X and Y IR 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 1 Y X R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 CCR 0 1 0 X = Y RAM 000000011001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
Example: Compare X and Y IR 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 1 Y X R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 CCR 1 0 0 X > Y RAM 000000011001 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
Example: Compare X and Y IR 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 1 Y X R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 CCR 0 0 1 X < Y RAM 000000011001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
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
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 opcodeOperationCCR 1000 JUMP X Any value 1001 JUMPGT X 100 1010 JUMPEQ X 010 1011 JUMPLT X 001 1100 JUMPNEQ X Any value but 010
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
In Machine Code LOAD 000000000000 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 COMPARE 000000000001 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 JUMPGT 000000000010 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 SUBTRACT 000000000011 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 1 STORE 000000000100 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 OUT 000000000101 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 HALT 000000000110 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 X 000000000111 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 ZERO 000000001000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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
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
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 000000000000 1101 000000001111 000000000001 0000 000000001101 000000000010 0001 000000010000 000000000011 0000 000000010000 000000000100 0111 000000011110 000000000101 1010 000000001010 000000000110 0011 000000001111 000000000111 0001 000000001111 000000001000 0100 000000010000 000000001001 1000 000000000011 000000001010 1110 000000001111 000000001011 1111 000000000000 000000001100 0000 000000000000 (zero) 000000001101 0000 000000000001 (one) 000000001110 0000 000000001011 (eleven) 000000001111 0000 000000000000 (sum) 000000010000 0000 000000000000 (count)