Consider the following array with the contents given (you have to declare it in your program):

5 9 11 16 20 25 19 7 30 13

Write an assembly program to convert each of the even numbers in the array to its next higher odd numbers and each of the odd numbers in the array to its immediate lower even numbers. then add all modified numbers in the array and sotre result in a memory location 'RESULT'. consider each of the element is of WORD size.
Write the code using index addressing.

1 answer

Here's an assembly program using index addressing to solve the given problem:

```assembly
.DATA
array WORD 5, 9, 11, 16, 20, 25, 19, 7, 30, 13
RESULT WORD ?

.CODE
MAIN PROC

MOV AX, @DATA
MOV DS, AX ; Initialize data segment

LEA SI, array ; Load effective address of array into SI
MOV CX, 10 ; Set the loop counter to the number of elements in the array

LOOP_START:
MOV AX, [SI] ; Load the current element into AX

TEST AX, 1 ; Check if the current element is odd or even
JZ EVEN ; If it's even, jump to the EVEN label

DEC AX ; Decrement the odd number by 1

JMP CONTINUE

EVEN:
ADD AX, 2 ; Add 2 to the even number to get the next higher odd number

CONTINUE:
ADD [SI], AX ; Add the modified number back into the array

ADD SI, 2 ; Move to the next element in the array

LOOP LOOP_START ; Loop until all elements have been processed

MOV SI, OFFSET array ; Reset SI to the start of the array
MOV CX, 10 ; Set the loop counter to the number of elements in the array

MOV AX, 0 ; Initialize AX with 0

SUM_LOOP:
ADD AX, [SI] ; Add each element in the array to AX

ADD SI, 2 ; Move to the next element in the array

LOOP SUM_LOOP ; Loop until all elements have been added

MOV RESULT, AX ; Store the result in memory location 'RESULT'

MOV AX, 4C00h ; Set the exit code to 0
INT 21h

MAIN ENDP
END MAIN
```

This program uses the `array` variable to store the given array, and the `RESULT` variable to store the final result. It uses the `SI` register with index addressing to access each element of the array.

In the first loop, the program checks if each element is odd or even using the `TEST` instruction. If it's odd, it decrements the number by 1 to obtain the immediate lower even number. If it's even, it adds 2 to the number to obtain the next higher odd number. The modified number is then added back into the array.

After modifying all the numbers in the array, the program initializes `AX` with 0 and loops through the array again to add all the modified numbers together. The final sum is stored in the `RESULT` variable.

Finally, the program sets the exit code to 0 and terminates.