1 / 15

Computer Science 101

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

Download Presentation

Computer Science 101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 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

  6. 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

  7. 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

  8. 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

  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 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

  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 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

  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 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)

More Related