1. Consider the following array with the contents given:

5 5 5 5 5 5 5 5 5 5

Do the the following:
a) Write an assembly code using index addressing to add all the elements in the array and store the result in a memory location 'RESULT'. consider each of the element is of WORD size.

1 answer

Here's an example of assembly code using index addressing to add all the elements in the array and store the result in a memory location 'RESULT':

```assembly
section .data
array dw 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
result dw 0

section .text
global _start
_start:
mov ecx, 10 ; initialize loop counter
mov esi, array ; point to the start of the array

xor eax, eax ; clear accumulator
add_loop:
add ax, [esi] ; add current element to accumulator
add esi, 2 ; move to next element (WORD = 2 bytes)
loop add_loop ; repeat until loop counter becomes zero

mov [result], ax ; store the result in 'RESULT'

; your program continues here
```

In this code, we define an array called 'array' with the given elements and initialize a variable 'result' to store the sum.

The assembly code starts at the _start label. We load the loop counter with the number of elements in the array (10 in this case) and use the mov instruction to point the esi register to the start of the array.

Next, we initialize the eax register to zero using the xor instruction as an accumulator for the sum. Inside the add_loop label, the value at the memory location pointed by esi is added to the accumulator using the add instruction. Then, the esi register is incremented by 2 to move to the next element in the array.

The loop instruction is used to repeat the add_loop block until the loop counter becomes zero. As the add_loop block will be repeated 10 times (number of elements in the array), each element will be added to the accumulator.

Finally, after the loop completes, we store the final result in the 'RESULT' memory location using the mov instruction.

Please note that this code assumes that the array is located in the writable data section (.data) and the 'RESULT' memory location is also in the data section. Also, the code snippet provided is for x86 assembly using NASM syntax, so it may need to be adjusted for different architectures or assembler syntaxes.