Section 06 Part 08 – Homework Results 05 “Home computers are being called upon to perform many new functions, including the consumption of homework formerly eaten by the dog." ~Doug Larson

Introduction

The homework set was a rather tricky one, which required carefully following the code, and keeping track of register data.  But, I have no doubt that you've done well in at least understanding the flow of it, even if you didn't get it 100% correct.

Results

 move.w    #\$0100,d0           clr.l     d1           move.w    #\$0400,d4           clr.l     d2           move.w    #\$1000,d3   NotReached:           addi.b    #\$10,d2           add.w     d0,d1           cmp.w     d1,d4           bgt.s     NotReached           sub.w     d2,d1           subi.w    #\$1000,d3           bpl.s     NotReached           move.w    d1,d0           swap      d0           move.w    d3,d0

 move.w    #\$0100,d0           clr.l     d1           move.w    #\$0400,d4           clr.l     d2           move.w    #\$1000,d3

d0 now contains 00000100.

d1 now contains 00000000.

d2 now contains 00000000.

d3 now contains 00001000.

d4 now contains 00000400.

d2 now contains 00000010, and d1 now contains 00000100.

 cmp.w     d1,d4           bgt.s     NotReached

d1 is compared with d4, 0400 - 0100 = 0300.  The CCR flags are set accordingly:

 C V Z N X 0 0 0 0 -

C is cleared (No carrying outside the word range), V is cleared (positive positive = positive), Z is cleared (Not zero), N is cleared (Not negative), X is ignored (Not changed by CMP).

The instruction BGT will branch if the destination operand is greater than the source operand (CCR: so long as Z is cleared, and N and V are both cleared or both set).  Since 0400 is greater than 0100 (Z, N and V are all cleared), the 68k will branch to the lable "NotReached:", and continue there.

Once again:

d2 now contains 00000020, and d1 now contains 00000200.

 cmp.w     d1,d4           bgt.s     NotReached

d1 is compared with d4, 0400 - 0200 = 0200. Since 0400 is greater than 0200, branch to "NotReached:".

d2 now contains 00000030, and d1 now contains 00000300.

 cmp.w     d1,d4           bgt.s     NotReached

d1 is compared with d4, 0400 - 0300 = 0100. Since 0400 is greater than 0300, branch to "NotReached:".

d2 now contains 00000040, and d1 now contains 00000400.

 cmp.w     d1,d4           bgt.s     NotReached

d1 is compared with d4, 0400 - 0400 = 0000. Since 0400 is NOT greater than 0400 (Z flag is now set because the result is zero), the branch will not occur, the 68k will now continue:

 sub.w     d2,d1

0400 - 0040 = 03C0.  d1 now contains 000003C0.

 subi.w    #\$1000,d3           bpl.s     NotReached

1000 - 1000 = 0000.  d3 now contains 00000000.

The BPL flag will branch because the result is still positive (The N flag is clear).  branch to "NotReached:".

d2 now contains 00000050, and d1 now contains 000004C0.

 cmp.w     d1,d4           bgt.s     NotReached

d1 is compared with d4, 0400 - 04C0 = FF40. 0400 is NOT greater than 04C0 (N flag is now set because the result is negative, the V flag is cleared because the answer is perfectly valid), because N and V are not the same, the branch will NOT occur, the 68k will now continue:

 sub.w     d2,d1

04C0 - 0050 = 0470.  d1 now contains 00000470.

 subi.w    #\$1000,d3           bpl.s     NotReached

0000 - 1000 = F000.  d3 now contains 0000F000.

The BPL flag will NOT branch because the result is negative (The N flag is set), so the 68k continues:

 move.w    d1,d0

0470 is copied from d1 to d0.  d0 now contains 00000470.

 swap      d0

d0 now contains 04700000.

 move.w    d3,d0

F000 is copied from d3 to d0.  d0 now contains 0470F000.

The result of d0 is 0470F000.  And as you can see, the 68k looped several times there while making the calculations necessary.  This shows you not only how conditional branches work, but how they can be used to make certain specific calculations using the same instructions several times over.