Fanny Fan
Fanny Fan

Reputation: 21

How to Store the elements of array at memory addresses in 8086

Here is the questionI'm trying to write a program in emu8086.This program about memory operations and arrays . Memory transfer operations must be done at data segment. And I have to store my elements at memory addresses.(for example: from DS:[2000h] to DS:[2009h])

    data segment   
    arr db 1,2,3,4,5,6,7,8,9,10  
    ends

code segment  
start:

    xor ax  , ax 
    lea si ,arr

    mov cx , 10   
add: 
    add ax,[si]
    add si,2
    loop add 
ends

I'm confused about addressing elements of array.

Upvotes: 2

Views: 5604

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

The calculation of the sum will be correct once you change the data sizes to BYTE.

 xor  al, al
 lea  si, arr
 mov  cx, 10
Sum:
 add  al, [si]
 inc  si
 loop Sum

I'm confused about addressing elements of array.

To address memory you need to setup a segment register and specify an offset (mostly using an address register like SI, DI, or BX).
To store the array and the sum at DS:[2000h], you will have to setup the DS segment register first. And because you need to address the array at its original place at the same time, you'll need to setup 2 segment registers.

mov ax, data    ;Source is the array in the program
mov es, ax
mov ax, 3000h   ;Destination is the designated memory
mov ds, ax

We can move the array and calculate the sum at the same time!

 xor  al, al
 lea  si, arr     ;Source offset (using ES)
 mov  di, 2000h   ;Destination offset (using DS)
 mov  cx, 10
MoveAndSum:
 mov  dl, es:[si]
 inc  si
 mov  [di], dl
 inc  di
 add  al, dl
 loop MoveAndSum
 mov  [di], al    ;Store the sum at DS:[200Ah]    ?????

The original task description tells you to store the sum at DS:[2010h]

That's not what the accompanying drawing is showing!

You might need to write mov [2010h], al instead of mov [di], al.

Upvotes: 1

Related Questions