160 likes | 263 Views
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.
E N D
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. • 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.
Large Value Filter Solution Outline • Identify which of the five cards contains the largest number. • Copy the contents of that card to a safe place. • Shift all of the contents of later cards over to fill the gap. • Place the largest value at the end.
Task #1 Identify which of the five cards contains the largest number. • Start at first card in list (Card #12). • Keep track of the location of the largest value seen “so far” • Store that value in Card #3 • Update that number if the next card’s value is larger. • Proceed to next card until end of list is reached (Card #16).
Task #2 Copy the contents of that card to a safe place. • 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!
Task #3 Shift all of the contents of later cards over to fill the gap • Start at Card Location having the largest value. • That info is already on Card #3 • If it is not the last card: (i.e., if value on Card #3 is less than 16) • Copy the contents of the next card onto it. • SET *3, *(*3+1) // Can’t quite do this • Move to next card • ADD 3, *3, 1 • Jump back to Task 3.2 to loop around (or hard code later passes)
Task #4 Place the largest value at the end • 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
Review 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”
Skip 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 M • SUB 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)
Task #1 - Identify largest card • 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 NEXTCARD • SUB 2, **4, **3 // Subtract next value from largest so far • SKP 1 // Skip if next value is less than largest so far • SET 3, *4 // Update card with largest value seen “so far” • ADD 4, *4, 1 // Move on to next card • SUB 2, 16, *4 // Subtract next card from last card • SKP 1 // Exit loop if next card > 16 • JMP NEXTCARD // Loop back to process next card
Task #3 - Shift rest of row over • 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_START • LABEL LOOP_EXIT
Large Value Filter • 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 NEXTCARD • SUB 2, **4, **3 // Subtract next value from largest so far • SKP 1 // Skip if next value is less than largest so far • SET 3, *4 // Update card with largest value seen “so far” • ADD 4, *4, 1 // Move on to next card • SUB 2, 16, *4 // Subtract next card from last card • SKP 1 // Exit loop if next card > 16 • JMP NEXTCARD // Loop back to process next card • // TASK #2 • SET 4, **3 // Put Largest value in Safe Place • // TASK #3 • LABEL LOOP_START • SUB 2, *3, 16 // Borrow occurs if (present card < 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_START • LABEL LOOP_EXIT • // TASK #4 • SET 16, *4 // Retrieve largest value and put at end Final Program
Large Value Filter EQU PROGRAMSTART (1) // Cards were the data is located EQU CARDS (5) // Memory Locations used in this program EQU BORROW (1) EQU JUNK (2) // Dummy target for SUB when only borrow result is needed EQU HIGHCARD (3) EQU LEFTCARD (HIGHCARD) // Redefine name to indicate change in use using same value EQU PRESENTCARD (4) EQU SAFEPLACE (4) // Doable because PRESENTCARD never used again. EQU RIGHTCARD (5) // Cards where data is stored EQU FIRSTCARD (12) EQU LASTCARD (FIRSTCARD + CARDS - 1) Use EQU directives to make code more readable
Large Value Filter • ORG PROGRAMSTART • SET PRESENTCARD, FIRSTCARD // TASK 1 - Find largest card • SET HIGHCARD, *PRESENTCARD • ADD PRESENTCARD, *PRESENTCARD, 1 • LABEL LABEL NEXTCARD • SUB JUNK, **PRESENTCARD, **HIGHCARD • SKP BORROW // Update HIGHCARD if PRESENTCARD is larger • SET HIGHCARD, *PRESENTCARD • ADD PRESENTCARD, *PRESENTCARD, 1 • SUB JUNK, LASTCARD, *PRESENTCARD • SKP BORROW // Exit loop if next card > LASTCARD • JMP NEXTCARD • SET SAFEPLACE, **HIGHCARD // TASK 2 - Put largest value in Safe Place • LABEL LOOP_START // TASK 3 - Shift rest of cards left to fill gap • SUB JUNK, *LEFTCARD, LASTCARD • SKP BORROW // Don’t exit loop if haven’t reach last card • JMP LOOP_EXIT • ADD RIGHTCARD, *LEFTCARD, 1 • SET *LEFTCARD, **RIGHTCARD • ADD LEFTCARD, *LEFTCARD, 1 • JMP LOOP_START • LABEL LOOP_EXIT • SET LASTCARD, *SAFEPLACE // TASK 4 - Retrieve largest value and put at end More Readable Version of Code
Large Value Filter • SET 4, 12 • SET 3, *4 • ADD 4, *4, 1 • SUB 2, **4, **3 • SKP 1 • SET 3, *4 • ADD 4, *4, 1 • SUB 2, 16, *4 • SKP 1 • JMP 4 • SET 4, **3 • SUB 2, *3, 16 • SKP 1 • JMP 19 • ADD 5, *3, 1 • SET *3, **5 • ADD 3, *3, 1 • JMP 12 • SET 16, *4 Program Deck