large value filter

16
Large Value Filter Large Value Filter Problem: Five random values are stored in consecutive memory locations starting at Card #12. GOAL: Move the largest value to the end of the list.

Upload: jerom

Post on 08-Jan-2016

15 views

Category:

Documents


0 download

DESCRIPTION

Large Value Filter. Problem: Five random values are stored in consecutive memory locations starting at Card #12. GOAL: Move the largest value to the end of the list. Large Value Filter. Solution Approach: Break problem down into a sequence of tasks. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Large Value Filter

Large Value FilterLarge Value Filter Problem:

Five random values are stored in consecutive memory locations starting at Card #12.

GOAL:Move the largest value to the end of the list.

Page 2: Large Value Filter

Large Value FilterLarge Value Filter Solution Approach:

Break problem down into a sequence of tasks.Solve each task (or portion of task) if solution obvious.Come back and break down the non-obvious tasks.Us a Memory Map to keep track of the (test) values.

Values at start of Task listed at top. Values at end of Task listed at bottom. Empty cells represent “don’t cares” - values no longer needed.

Page 3: Large Value Filter

Large Value FilterLarge Value Filter

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

16 12 87 42 39

16 12 42 39 87

Solution Outline

1) Identify which of the five cards contains the largest number.2) Copy the contents of that card to a safe place.3) Shift all of the contents of later cards over to fill the gap.4) Place the largest value at the end.

Page 4: Large Value Filter

Task #1Task #1

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

16 12 87 42 39

14 16 12 87 42 39

Identify which of the five cards contains the largest number.

1) Start at first card in list (Card #12).2) Keep track of the location of the largest value seen “so far”

1) Store that value in Card #33) Update that number if the next card’s value is larger.4) Proceed to next card until end of list is reached (Card #16).

Page 5: Large Value Filter

Task #2Task #2

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

14 16 12 87 42 39

87

14 87 16 12 87 42 39

Copy the contents of that card to a safe place.

1) Use Card #4 to hold the value so that it doesn’t get lost.• SET 4, *(Card # that has the largest value on it)

so...• SET 4, **3 // *3 is the Card # w/ the largest value.

This task is done!

Page 6: Large Value Filter

Task #3Task #3

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

14 87 16 12 87 42 39

42 39

87 16 12 42 39 39

Shift all of the contents of later cards over to fill the gap

1) Start at Card Location having the largest value.• That info is already on Card #3

2) If it is not the last card: (i.e., if value on Card #3 is less than 16)1) Copy the contents of the next card onto it.

• SET *3, *(*3+1) // Can’t quite do this2) Move to next card

• ADD 3, *3, 13) Jump back to Task 3.2 to loop around (or hard code later passes)

Page 7: Large Value Filter

Task #4Task #4

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

87 16 12 42 39 39

87

16 12 42 39 87

Place the largest value at the end

1) Copy largest value from safe location to end of list.• SET 16, *4

This task is done!

Problem is solved once we flesh out parts of Tasks #1 and #3

Page 8: Large Value Filter

Review Unsolved TasksReview Unsolved Tasks Still to be done:

Task #1 Task 1.1 - Use another card to keep track of the present card Task 1.2 - Initially, the largest value seen is on the first card. Task 1.3 - Test if card in list is larger, if so replace index. Task 1.4 - Increment “present card” and test if <16 then loop accordingly

Task #3 Task 3.2 - Implementing the test. Task 3.2.1 - Writing it in a legal way. Task 3.2.2 - Looping back.

In two places, we need to test if one value is larger than another value. Figure out a generic way to do “skip next instruction if A < B”

Page 9: Large Value Filter

Skip if Less ThanSkip if Less Than In two places, we need to test if one value is larger

than another value.If A < B, then (A / B) is 0 r ??If A < B, then (A - B) produces a borrow.

Want a code segment that will skip a line if the value on Card N is less than the value on Card MSUB 2, *N, *M // Card 2 is irrelevant and not used.SKP 1 // Skip if borrow is True (i.e., *N < *M) ???? // Instruction to execute only if (*N >= *M)???? // Instruction to jump to if (*N < *M)

Page 10: Large Value Filter

Task #1 - Identify largest cardTask #1 - Identify largest card0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

16 12 87 42 390 96 12 121 2 14 130 71 140 1 150 55 160 0 170 521 99101

14 16 12 87 42 39

1) SET 4, 12 // Start at first card in list (Card #12). 2) SET 3, *4 // Card with largest value seen “so far” 3) ADD 4, *4, 1 // Move to next cardLABEL NEXTCARD4) SUB 2, **4, **3 // Subtract next value from largest so far5) SKP 1 // Skip if next value is less than largest so far6) SET 3, *4 // Update card with largest value seen “so far”7) ADD 4, *4, 1 // Move on to next card8) SUB 2, 16, *4 // Subtract next card from last card9) SKP 1 // Exit loop if next card > 1610) JMP NEXTCARD // Loop back to process next card

Page 11: Large Value Filter

Task #3 - Shift rest of row overTask #3 - Shift rest of row over0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

14 87 16 12 87 42 391 98 15 42 39

0 99 15 160 00 161001

87 16 12 42 39 39

LABEL LOOP_START• SUB 2, *3, 16 // Borrow occurs if (*3 < 16)• SKP 1• JMP LOOP_EXIT• ADD 5, *3, 1 // Set Card 5 to the number of the “next card” • SET *3, **5 // Copy contents of next card to current card.• ADD 3, *3, 1 // Increment the card we are pointing to• JMP LOOP_STARTLABEL LOOP_EXIT

Page 12: Large Value Filter

Large Value FilterLarge Value FilterFinal Program

ORG 1// TASK #1

SET 4, 12 // Start at first card in list (Card #12). SET 3, *4 // Card with largest value seen “so far” ADD 4, *4, 1 // Move to next card

LABEL NEXTCARDSUB 2, **4, **3 // Subtract next value from largest so farSKP 1 // Skip if next value is less than largest so farSET 3, *4 // Update card with largest value seen “so far”ADD 4, *4, 1 // Move on to next cardSUB 2, 16, *4 // Subtract next card from last cardSKP 1 // Exit loop if next card > 16JMP NEXTCARD // Loop back to process next card

// TASK #2SET 4, **3 // Put Largest value in Safe Place

// TASK #3LABEL LOOP_START

SUB 2, *3, 16 // Borrow occurs if (present card < 16)SKP 1JMP LOOP_EXITADD 5, *3, 1 // Set Card 5 to the number of the “next card” SET *3, **5 // Copy contents of next card to current card.ADD 3, *3, 1 // Increment the card we are pointing toJMP LOOP_START

LABEL LOOP_EXIT// TASK #4

SET 16, *4 // Retrieve largest value and put at end

Page 13: Large Value Filter

Large Value FilterLarge Value FilterUse EQU directives to make code more readable

EQU PROGRAMSTART (1)// Cards were the data is locatedEQU CARDS (5)// Memory Locations used in this programEQU BORROW (1)EQU JUNK (2) // Dummy target for SUB when only borrow result is neededEQU HIGHCARD (3)EQU LEFTCARD (HIGHCARD) // Redefine name to indicate change in use using same valueEQU PRESENTCARD (4)EQU SAFEPLACE (4) // Doable because PRESENTCARD never used again.EQU RIGHTCARD (5)// Cards where data is storedEQU FIRSTCARD (12)EQU LASTCARD (FIRSTCARD + CARDS - 1)

Page 14: Large Value Filter

Large Value FilterLarge Value FilterMore Readable Version of Code

ORG PROGRAMSTARTSET PRESENTCARD, FIRSTCARD // TASK 1 - Find largest cardSET HIGHCARD, *PRESENTCARDADD PRESENTCARD, *PRESENTCARD, 1

LABEL LABEL NEXTCARDSUB JUNK, **PRESENTCARD, **HIGHCARDSKP BORROW // Update HIGHCARD if PRESENTCARD is largerSET HIGHCARD, *PRESENTCARDADD PRESENTCARD, *PRESENTCARD, 1SUB JUNK, LASTCARD, *PRESENTCARDSKP BORROW // Exit loop if next card > LASTCARDJMP NEXTCARDSET SAFEPLACE, **HIGHCARD // TASK 2 - Put largest value in Safe Place

LABEL LOOP_START // TASK 3 - Shift rest of cards left to fill gapSUB JUNK, *LEFTCARD, LASTCARD SKP BORROW // Don’t exit loop if haven’t reach last cardJMP LOOP_EXITADD RIGHTCARD, *LEFTCARD, 1 SET *LEFTCARD, **RIGHTCARD ADD LEFTCARD, *LEFTCARD, 1JMP LOOP_START

LABEL LOOP_EXITSET LASTCARD, *SAFEPLACE // TASK 4 - Retrieve largest value and put at end

Page 15: Large Value Filter

Large Value FilterLarge Value FilterProgram Deck

1) SET 4, 12

2) SET 3, *43) ADD 4, *4, 14) SUB 2, **4, **3

5) SKP 1

6) SET 3, *4

7) ADD 4, *4, 18) SUB 2, 16, *49) SKP 110) JMP 411) SET 4, **312) SUB 2, *3, 16

13) SKP 114) JMP 1915) ADD 5, *3, 1

16) SET *3, **5

17) ADD 3, *3, 1

18) JMP 1219) SET 16, *4

Page 16: Large Value Filter

Memory Map after executionMemory Map after execution0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

16 12 87 42 39

0 96 12 12 15 42 39 87

1 2 14 13 16

0 71 15 14

0 1 16 15

0 55 16

0 0 17

0 52 87

1 99

1 98

0 99

1 00

1

0

0

1

0

0

0

16 12 42 39 87