Finding the smallest value in memory
- $500 contains an array of words, with the number of elements in D0. Write a routine that finds the smallest
value and stores it the memory pointed to be A0.
- The program should start at $2000.
- Fix any syntax errors after you assemble.
- Start the simulator, and set registers and add edit the array.
- Run the program and check that result is the result intended.
- You need to reset PC if you want to rerun the program.
- Show the screenshots of the program in action, to show the workings.
ORG $2000 SMALLEST: ; smallest subroutine ; address of place to save the minimum is in A0 ; number of elements in the array is in D0 MOVEM.L D0-D2/A0-A1,-(SP) ; save used registers in the stack MOVE.L #$500,A1 ; load A1 with the array address $500 MOVE.W (A1)+,D1 ; load first value from the array into D1, which will hold the minimum SUBI.L #1,D0 ; we used the first number, decrement element count LOOP: CMP.L #0,D0 ; if the count is zero BEQ DONE ; we are done MOVE.W (A1)+,D2 ; load a number from the array CMP.W D1,D2 ; compare the current element with the minimum BGT NEXT ; if the element is bigger than the minimum, skip MOVE.W D2,D1 ; otherwise, set element as new minimum NEXT: SUBI.L #1,D0 ; decrement number of elements to compare BRA LOOP ; repeat DONE: MOVE.W D1,(A0) ; at the end D1 has the minimum, save it in the given address MOVEM.L (SP)+,D0-D2/A0-A1 ; recover saved registers from the stack RTS ; return from subroutine END SMALLEST
For running it I used the following starting values:
D0 = 10
Memory data in $500: 8, 4, 7, 3, 6, 2, 5, 9, 1, 2, all words (2 bytes each)
The file “screenshot-start.png” shows the initial screen for the simulator.
After running you can see in the “screenshot-end.png” that memory position $1000 has the value 1, which is the minimum, or smallest value in the array.
The code is written as a subroutine as requested, it saves the registers it uses at the start of the subroutine and it restores their value at the end of it, the subroutine exits with the instruction RTS or return from subroutine.
I hope that after reading through these examples you decide to contact us to offer you help with Assembly language homework.