; Data reverser.asm ; This SimpSim assembly language program reverses the order of data. ; Input: 1,12,23,34,45,56 ; Output: 56, 45, 34, 23, 12, 1 ;; 1. Load the Lo and Hi pointers with the start and end of the data load R1,Data ; Lo Pointer (initially the start of the data) load R2,EndOfData ; Hi Pointer (initially the end of the data) load R3,1 ; needed to increment Lo pointer load R4,-1 ; needed to decrement Hi pointer addi R2,R2,R4 ; Decrement Hi pointer to last data element Loop: ;; 2. Check if we are done (if Hi <= Lo) move R0,R1 ; Copy Lo Pointer in R1 into R0 jmpLE R2<=R0,Done ; If (Hi <= Lo) jump to Done ;; 3. Exchange [Hi] and [Lo] load R5,[R1] ; Load [Lo] in R5 load R6,[R2] ; Load [Hi] in R6 store R5,[R2] ; Store R5 (what was [Lo]) in [Hi] store R6,[R1] ; Store R6 (what was [Hi]) in [Lo] ;; 4. Move Lo up one and Hi down one addi R1,R1,R3 ; Add one to Lo addi R2,R2,R4 ; Subtract one from Hi ;; 5. Continue looping jmp Loop Done: ;; 6. Halt -- we're done! halt org 20h ; start our data segment at memory location 20h Data: db 1h,12h,23h,34h,45h,56h,67h,78h,89h,9Ah EndOfData: db 0 ; Data terminator