In: Statistics and Probability
A block of memory contains 40 random words of data. Assume that the values are in two’s
complement representations and are stored in the Read/Write memory area. You need to provide the
40 random words and to include them in an initialization file: assign.ini. Write a program using the
ARM assembly programming language to do the following:
a) You are required to reverse the word order in a block of 40 random words (Hint: the last word
stored in the memory becomes the first and vice versa).
b) Next, find the minimum value in the 40 random words of data.
The algorithm for reversing the order of words in memory is as follows: We have to first load the first and the last words into two registers, here R3 and R4 registers have been used. The LDR R3, [R0] instruction loads the contents of memory specified by the register R0 into the resgister R3. Then we have to swap their places. This could be done by storing the words in the registers to the respective memory locations directly. The STR R3, [R1] stores the word in register R3 into the to the memory location specified by resgister R1. This process should be repeated until we reach the middle of the list of the words that means until we run the loop for 20th time. So the loop has to be run for twenty times. The register R2 is used for this operation. The value 20 in hexadecimal is 14. The value is decremented by one everytime the loop is run and when it reaches zero the loop exits and the program is stopped. The conditional instruction BNE jumps to the specified location if the zero flag is not set.
(a)
MOV R0, #D0 // Data stored in memory location starting from D0
MOV R1, #F8 // storing the location of last word.
MOV R2, #14 // number of times the loop has to run = 20 times = 14 times in hex.
LOOP LDR R3, [R0] // load the content of memory into the register
LDR R4, [R1]
STR R3, [R1] // store into the memory
STR R4, [R0]
ADD R0, R0, #1 // increment register to go to the next location
SUB R1, R1, #1 // decement register to go to the previous location
SUB R2, R2, #1 // Decrement the counter
BNE LOOP // repeat the loop if counter is not zero
For finding the minimum value we take the first value in the last and store it in register R3. Then we compare this value with all the values in the list and check if any number is less the value in register R3. If there is a lesser number then copy that number to R3 register and coontinue the process. The loop runs for 39 times to compare all the numbers. The conditional instruction BCS jumps to the specified location is the carry flag is set. The conditional instruction BNE jumps to the specified location if the zero flag is not set.
(b)
MOV R0, #D0 // Data stored in memory location starting from D0
MOV R2, #27 // number of times the loop has to run = 39 times = 27 times in hex.
LDR R3, [R0]
LOOP ADD R0, R0, #1 // increment register to go to the next location
LDR R4, [R0]
CMP R3, R4 // compare the register to check if the value in R4 is less
BCS NEXT // If R3 value is less then continue the loop
MOV R3, R4 // If R4 is less then copy the content of of R4 to R3
NEXT SUB R2, R2, #1 // Decrement the counter
BNE LOOP // repeat the loop if counter is not zero