GabourX
GabourX

Reputation: 283

Wrong result in multiplying in 8086 assembly

The program must calculate the following:

in VP array , each index will have the value of Multiplying V1[INDEX] WITH V2[INDEX] in VPROD array , the program ADDS all the Values of Array VP

Example:

VP = { V1[0]*V2[0] , V1[1]*V2[1] , V1[2]*V2[2] , V1[3]*V2[3] ,V1[4]*V2[4] }
VPROD = { VP[0] + VP[1] + VP[2] + VP[3] + VP[4] }

The problem is that when the result is bigger than 16 bits (a word), it does not give the right results. But when the result is smaller than 16 bits, it gives the right result.

I thought about Carry and fixed my code, but still it gives me bad results.

My code:

    dseg segment

    v1 DW 255,5,255,9,21
    v2 DW 4,4,255,13,5
    vprod DW 10 DUP (0)
    vp DW 10 DUP (?)
    N DB 5
    dseg ends

    sseg segment stack
            dw 100h dup(?)
    sseg ends

    cseg segment
    assume  ds:dseg,cs:cseg,ss:sseg
    start:      mov ax,dseg
                MOV DS,AX
            MOV SI,0
            MOV CX,0
            MOV CL,N
            MOV DI, 0
LOVP:       MOV AX,v1[si]    
            MUL v2[si]
            MOV vp[di],AX
            MOV vp[di+2],DX
            ADD SI,2
            ADD DI,4
            LOOP LOVP
            MOV CX,0
            MOV CL,N
            MOV SI,0
            MOV DI,0
LOVPROD:    MOV AX,vp[SI]
            ADD vprod[DI],AX
            ADC VPROD[DI+2],0
            ADD SI,2
            LOOP LOVPROD

    SOF:
                mov ah,4ch
            int 21h
    cseg ends
    end start

Upvotes: 0

Views: 831

Answers (1)

Jester
Jester

Reputation: 58762

Since you are getting a 32 bit result from the multiply, you should use 4 bytes for every element of the vp array. You will need to use a separate index for that, and of course store the high word at offset +2 instead of +1. For the summation part, you should add up both low and high words. Fixed code for the multiplication could look like:

            MOV DI, 0
LOVP:       MOV AX,v1[si]    
            MUL v2[si]
            MOV vp[di],AX
            MOV vp[di+2],DX
            ADD SI,2
            ADD DI,4
            LOOP LOVP

I trust you can do the summation yourself, using ADD and ADC.

UPDATE: Since your post says you want to sum the vp array, I don't see why you expect the result in another array (vprod). The sum is a scalar value, do something like:

            MOV SI, 0
            MOV AX, 0
            MOV DX, 0
LOVPROD:    ADD AX,vp[SI]
            ADC DX,vp[SI+2]
            ADD SI,4
            LOOP LOVPROD

The result will be in DX:AX

Also, for the LOOP to work properly, you should put the number of iterations into CX, not 0 as you are doing.

Upvotes: 1

Related Questions