.TITLE ReverseList ; Program to reverse the order of items in a list ; Algorithm: ; 1. Initialize ; Put the address of the list and the count each into ; two registers (one copy for pushing, one for popping). ; 2. Push list onto user stack ; Push next item onto the stack and decrement counter ; until finished ; 3. Pop list from user stack ; Pop next item from the stack and decrement counter ; until finished. ; Register usage: ; R2, R3 address of current item ; R4, R5 counter (decreases) .PSECT Variables, NOEXE, WRT ArraySize = 20 Number: .WORD 6 ; Number of items in list List: .LONG 16, 1, 14, 18, 2, 10 ; Items in list .BLKL ArraySize - 6 ; Leave room for unused ; elements .PSECT Code, EXE, NOWRT .ENTRY ReverseList, 0 ; Initialize Begin: MOVAL List, R2 ; Put address of List in R2 MOVL R2, R3 ; and R3 MOVW Number, R4 ; Put number of items in R4 MOVW R4, R5 ; and R5 ; Push items onto user stack PushLoop: PUSHL (R2)+ ; Push items onto stack DECW R4 ; Decrement counter and BGTR PushLoop ; repeat, if needed ; Pop items off user stack back into original list PopLoop: POPL (R3)+ ; Pop items off stack DECW R5 ; Decrement counter and BGTR PopLoop ; repeat, if needed Finished: $EXIT_S .END ReverseList