1. Consider the following arrays with the contents given (you have to declare them in your program).
Array1:
3, 8, 13, 12, 20, 25, 19, 17, 30, 13
Array2:
, , , , , , , , ,
Array3:
E0A1+E9A2, E1A1+E8A2, E2A1+E7A2, , , , , , , E9A1+E0A2
Write an assembly program to do the followings:
a. Convert each of the even number in Array1 to its immediate next higher odd number and each of the odd number in the array to its immediate lower even number. This conversion will be done in a subroutine named Modifier. The modification will be done using logic so that the modifier works for any data in the given array. Before modifications, all the data in the array needs to be saved in the stack so that in future we can use the original data again.
b. Now, populate Array 2 with the saved data in the stack. Multiply each of the data (only the even numbers) by 4. This modification will be done by another subroutine named Incrementor.
c. Now populate Array3 such that element 0 of Array3 is the summation of element 0 0f Array1 and element 9 of Array2. Element 1 0f Array3 is the sum of element 1 of aArray1 and element 8 0f Array2 and so on.
d. Finally, send the content of element 0 of Array3 to PORT 0 of the trainer kit.
1 answer
```assembly
ORG 0000h
; Declare the arrays
Array1 DB 3, 8, 13, 12, 20, 25, 19, 17, 30, 13
Array2 DB 10 DUP (0)
Array3 DB 10 DUP (0)
; Subroutine to convert each number in Array1 and save original data in the stack
Modifier:
PUSH AX ; Save AX register
PUSH BX ; Save BX register
PUSH CX ; Save CX register
PUSH DX ; Save DX register
PUSH SI ; Save SI register
MOV CX, 10 ; Number of elements in Array1
LEA SI, Array1
Loop1:
MOV AL, [SI] ; Load current element into AL register
TEST AL, 1 ; Check if the number is odd or even
JNZ Odd ; If odd, jump to Odd
INC AL ; If even, increment the number to make it odd
JMP Continue
Odd:
DEC AL ; If odd, decrement the number to make it even
Continue:
MOV [SI], AL ; Store modified number back into Array1
INC SI ; Move to the next element
LOOP Loop1
POP SI ; Restore SI register
POP DX ; Restore DX register
POP CX ; Restore CX register
POP BX ; Restore BX register
POP AX ; Restore AX register
RET ; Return from the subroutine
; Subroutine to populate Array2 with saved data from the stack and multiply even numbers by 4
Incrementor:
PUSH AX ; Save AX register
PUSH BX ; Save BX register
PUSH CX ; Save CX register
PUSH DX ; Save DX register
PUSH SI ; Save SI register
PUSH DI ; Save DI register
MOV CX, 10 ; Number of elements in Array1
LEA SI, Array1
LEA DI, Array2
Loop2:
MOV AL, [SI] ; Load current element from Array1 into AL register
CMP AL, 0 ; Check if the element is empty (0 means empty)
JZ Skip ; If empty, skip to the next element
TEST AL, 1 ; Check if the number is odd or even
JNZ Skip ; If odd, skip to the next element
MOV BL, AL ; Move the even number into BL register
MOV AL, 4 ; Multiply the even number by 4
MUL BL ; Multiply AL by BL
MOV [DI], AL ; Store the multiplied number into Array2
Skip:
INC SI ; Move to the next element in Array1
INC DI ; Move to the next element in Array2
LOOP Loop2
POP DI ; Restore DI register
POP SI ; Restore SI register
POP DX ; Restore DX register
POP CX ; Restore CX register
POP BX ; Restore BX register
POP AX ; Restore AX register
RET ; Return from the subroutine
MAIN:
; Call Modifier subroutine to modify Array1
CALL Modifier
; Call Incrementor subroutine to populate Array2
CALL Incrementor
; Populate Array3 with the summation of corresponding elements from Array1 and Array2
LEA SI, Array1
LEA DI, Array2
LEA BX, Array3
MOV CX, 10 ; Number of elements in Array1
XOR AX, AX ; Clear AX register
Loop3:
MOV AL, [SI] ; Load element from Array1 into AL register
ADD AL, [DI] ; Add corresponding element from Array2
MOV [BX], AL ; Store the summation into Array3
INC SI ; Move to the next element in Array1
INC DI ; Move to the next element in Array2
INC BX ; Move to the next element in Array3
LOOP Loop3
; Send the content of element 0 of Array3 to PORT 0
MOV AL, [Array3] ; Load content of element 0 of Array3 into AL register
OUT 0, AL ; Send AL to PORT 0
HLT ; Halt the program
END
```
Please note that this assembly program assumes it is running on an 8-bit microprocessor with limited memory and minimal instruction set. The program may need to be adjusted depending on the specific microprocessor and assembler being used.