2

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Fanny Fan
  • 21
  • 1
  • 3
  • 2
    Your array is declared as a byte array, whereas your code is thinking of it as a word array. Such a mismatch won't work. Either declare the array as word (dw), or, use byte operations in the code. If you want a better answer, you might update with a question. – Erik Eidt Oct 23 '19 at 15:24

1 Answers1

1

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.

Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • I don't think the assignment wants them to *copy* to `ds:2000`, I think it wants them to use `data segment` syntax to declare that the segment starts at offset `2000`, or something like that. Although the part about setting `ds=3000` is odd; you don't normally get a choice. The .exe program loader normally rewrites segment register stuff. But anyway, the only "transfers" I see required in the spec in the OP's image are the loads to calculate the sum. Nowhere does it actually say "copy". – Peter Cordes Oct 24 '19 at 11:15