Just as the title says I have a question with a program I am doing. I am just starting out using MASM assembly and kind of stuck any help would be great.

I need to calculate the sums and differences of arrays that are shown below. The little bit of code i have now is

.data
x sword 10,20,30,40,50,60,70,80
y sword -80,-70,-60,-50,-40,-30,-20,-10
a sword 8 dup (?)
b sword 8 dup (?)

.code
begin:
mov esi,offset x
mov edi,offset a
mov ecx,lengthof x

mov ebx,offset y
mov edi,offset b
mov ecx,lengthof y

next:
mov ah,[esi]
mov [edi],ah
add esi
add edi
loop next

else:
move al,[ebx]
mov [edi],al
add ebx
add edi
loop else

invoke exitProcess,0
end begin

Dani AI

Generated

Quick summary: the original snippet overwrote pointers, used byte-sized moves on word arrays, and had several typos (for example move instead of mov, and add esi without an immediate). , the easiest fixes are (1) keep separate registers for the two destination arrays so you don't clobber the address of a with b, (2) load words (not bytes) and increment pointers by 2, and (3) use a proper loop counter. is right to do both operations in one pass — below is a compact, robust loop that does that while also sign-extending the 16-bit values into 32-bit regs to avoid surprises with negative numbers.

; assume x,y,a,b are WORD arrays of equal length
    mov  esi, OFFSET x        ; source X
    mov  ebx, OFFSET y        ; source Y
    mov  edi, OFFSET a        ; destination sums
    mov  edx, OFFSET b        ; destination diffs
    mov  ecx, LENGTHOF x      ; element count
    cld

.loop:
    movsx eax, WORD PTR [esi] ; eax = signed x[k]
    movsx ebp, WORD PTR [ebx] ; ebp = signed y[k]

    add   eax, ebp            ; sum
    mov   WORD PTR [edi], ax

    movsx eax, WORD PTR [esi] ; reload x (cheap for small arrays)
    sub   eax, ebp            ; difference x - y
    mov   WORD PTR [edx], ax

    add   esi, 2
    add   ebx, 2
    add   edi, 2
    add   edx, 2

    dec   ecx
    jnz   .loop

Troubleshooting notes: always specify operand size (WORD PTR) when memory size might be ambiguous; LENGTHOF returns element count (not bytes), so increment pointers by the element size (2 bytes here). Use movsx when promoting signed 16-bit values to 32-bit registers. Check for simple typos (mov vs move) and remember the loop instruction implicitly uses ECX — I used dec/jnz to keep intent explicit. Finally, watch for 16-bit overflow: if sums/differences may exceed signed 16-bit range, do the math in 32-bit and handle or detect overflow before truncating back to a WORD.

First, line 10 uses EDI for a, in line 14 EDI is b. Overwriting the command in line 10.


I would have written

begin procedure near
mov ESI, offset X     ; or   LEA ESI, X
mov EDI, offset Y     ; or   LEA EBX, Y
mov EBX, offset a
mov EDX, offset b
mov ECX, lengthof X   ;  or mov ecx,8   ;counter

next:
mov AX,[ESI]        ; X[k]
add AX,[EDI]        ; add Y[k]
mov [EBX],AX        ; store sum

mov AX,[ESI]
sub AX,[EDI]
mov [EDX],AX       ; store X[k]-Y[k],

add ESI,2
add EDI,2
add EBX,2
add EDX,2
  dec ECX
  jg  next           ; do again if ecx >0

invoke exitProcess,0
begin endp
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.