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

Computer Science 101

Embed Size (px)

DESCRIPTION

Computer Science 101. Sequencing, Selection, and Loops in Machine Language. Begin … 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Computer Science 101

Computer Science 101

Sequencing, Selection, and Loops

in Machine Language

Page 2: Computer Science 101

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

Begin<statement 1>…<statement n>End

Page 3: Computer Science 101

Selection

Selection (one-way if) statements use two types of instructions– A comparison– A conditional jump

If <condition> Begin <statement 1> … <statement n> End

Page 4: Computer Science 101

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

Page 5: Computer Science 101

The Condition Code Register

1 0 0CON(X) > R

CON(X) = R

CON(X) < R

0 1 0

0 0 1

CON(X) means contents of memory cell at address X

Comparison State of CCR

Page 6: Computer Science 101

Example: Compare X and Y

0 1 1 1 0 0 0 0IR 0 0 0 1 1 0 0 1

0 0 0 0 0 0 0 0R 0 0 0 0 0 0 1 1

0 1 0CCR

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1000000011001

RAM

Y

X

X = Y

Page 7: Computer Science 101

Example: Compare X and Y

0 1 1 1 0 0 0 0IR 0 0 0 1 1 0 0 1

0 0 0 0 0 0 0 0R 0 0 0 0 0 0 1 1

1 0 0CCR

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0000000011001

RAM

Y

X

X > Y

Page 8: Computer Science 101

Example: Compare X and Y

0 1 1 1 0 0 0 0IR 0 0 0 1 1 0 0 1

0 0 0 0 0 0 0 0R 0 0 0 0 0 0 1 1

0 0 1CCR

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0000000011001

RAM

Y

X

X < Y

Page 9: Computer Science 101

Steps Required

• Comparisons require two steps:– Load the second operand into register R– Compare to the first operand (a memory

address)

LOAD YCOMPARE X

Page 10: Computer Science 101

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

1000 JUMP X Any value1001 JUMPGT X 1001010 JUMPEQ X 0101011 JUMPLT X 0011100 JUMPNEQ X Any value but

010

Page 11: Computer Science 101

Example If Statement

// Absolute valueIf X < 0 Set X to -X

// Absolute value1. LOAD ZERO2. COMPARE X3. JUMPGT LINE 64. SUBTRACT X5. STORE X6. Rest of code

High-level pseudocode Low-level pseudocode

These two lines are skipped if X > 0

Page 12: Computer Science 101

In Machine Code

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0000000000000

0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1000000000001

1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1000000000010

0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 1000000000011

0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1000000000100

1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1000000000101

1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0000000000110

1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1000000000111

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000000001000

X

ZERO

LOAD

COMPARE

JUMPGT

SUBTRACT

STORE

OUT

HALT

Page 13: Computer Science 101

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

Page 14: Computer Science 101

Example: Output Sum of 1 .. 10

Set sum to 0Set count to 1While count <= 10 do Set sum to sum + count Increment countOutput sum

1 clear sum2 load one3 store count4 load count5 compare eleven6 jumpeq line 128 add sum9 store sum10 increment count11 jump line 412 out sum13 halt

Page 15: Computer Science 101

Example: Output Sum of 1 .. 101 clear sum2 load one3 store count4 load count5 compare eleven6 jumpeq line 128 add sum9 store sum10 increment count11 jump line 412 out sum13 halt

000000000000 1101 000000001111000000000001 0000 000000001101000000000010 0001 000000010000000000000011 0000 000000010000000000000100 0111 000000011110000000000101 1010 000000001010000000000110 0011 000000001111000000000111 0001 000000001111000000001000 0100 000000010000000000001001 1000 000000000011000000001010 1110 000000001111000000001011 1111 000000000000 000000001100 0000 000000000000 (zero)000000001101 0000 000000000001 (one)000000001110 0000 000000001011 (eleven)000000001111 0000 000000000000 (sum)000000010000 0000 000000000000 (count)