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

3 answers

Assuming that the array is stored starting at memory address 1000, and the 'RESULT' memory location will be at address 2000, you can write the following assembly program to achieve the desired result:

```
DATA SEGMENT
ARR DW 5, 9, 11, 16, 20, 25, 19, 7, 30, 13
SIZE DW 10 ; Number of elements in the array
RESULT DW ?
DATA ENDS

CODE SEGMENT
ASSUME DS:DATA, CS:CODE

START:
MOV AX, DATA
MOV DS, AX ; Initialize DS segment register

MOV SI, 0 ; Initialize source index
MOV DI, 0 ; Initialize destination index
MOV BX, 0 ; Initialize sum register

LOOP_START:
MOV AX, ARR[SI] ; Load next element from the array
CMP AX, 0 ; Check if the element is zero (end of array)
JE END_LOOP ; If yes, jump to the end

TEST AX, 1 ; Check if the number is odd or even
JNZ ODD ; If odd, jump to ODD label

EVEN:
INC AX ; Increment even number to the next odd number
MOV ARR[DI], AX ; Store the modified number back to the array
ADD BX, AX ; Add the modified number to the sum
ADD DI, 2 ; Increment destination index
ADD SI, 2 ; Increment source index
JMP LOOP_START ; Jump to the start of the loop

ODD:
DEC AX ; Decrement odd number to the previous even number
MOV ARR[DI], AX ; Store the modified number back to the array
ADD BX, AX ; Add the modified number to the sum
ADD DI, 2 ; Increment destination index
ADD SI, 2 ; Increment source index
JMP LOOP_START ; Jump to the start of the loop

END_LOOP:
MOV RESULT, BX ; Store the sum in the RESULT memory location

MOV AX, 4C00H ; Terminate the program
INT 21H

CODE ENDS
END START
```

This program uses a loop that iterates over each element in the array. It checks if the current element is odd or even using the `TEST` instruction. If it's even, it increments it to the next odd number, stores it back into the array, adds it to the sum in the `BX` register, and increments the source and destination indices. If it's odd, it decrements it to the previous even number, performs the same operations, and increments the indices.

After the loop, the program stores the final sum in the `RESULT` memory location and terminates.
display the RESULT
To display the value stored in the 'RESULT' memory location, you can add the following code snippet to the end of the previous assembly program:

```
DISPLAY_RESULT:
MOV AH, 02H ; Set the function to display a character
MOV DL, 'R' ; Move the ASCII value of 'R' to the DL register
INT 21H ; Display the character

MOV AH, 09H ; Set the function to display a string
LEA DX, RESULT ; Load the address of the 'RESULT' memory location
INT 21H ; Display the value

MOV AH, 02H ; Set the function to display a character
MOV DL, ' ' ; Move a space character to the DL register
INT 21H ; Display the space character

MOV AH, 02H ; Set the function to display a character
MOV DL, 'H' ; Move the ASCII value of 'H' to the DL register
INT 21H ; Display the character

MOV AX, 4C00H ; Terminate the program
INT 21H

CODE ENDS
END START
```

This additional code snippet will display the value stored in the 'RESULT' memory location using the INT 21H interrupt. Firstly, it displays the character 'R', then it displays the value stored in 'RESULT', followed by a space character, and finally, it displays the character 'H'. After that, the program terminates.

By incorporating this code, you can display the value stored in the 'RESULT' memory location at the end of the program execution.
Similar Questions
    1. answers icon 1 answer
    1. answers icon 1 answer
  1. Video Game Player ProgramWrite a program to do the following. In main declare a Player Name Array and a Score Array. Declare the
    1. answers icon 2 answers
  2. Write assembly program using array with the contents given:5 5 5 5 5 5 5 5 5 5 Define 'Array' and 'RESULT'. Code: MOV AX, 0 MOV
    1. answers icon 1 answer
more similar questions