You are on page 1of 5

Example 9.

7
Write an 8086 assembly program to implement the following C language program loop:
sum = 0;
for (i = 0; i <=99; i = i + 1)
sum = sum + x[i] * y[i];
The assembly language program will compute SUM (xi * yi) where i = 100 and, xi and yi are signed 8-bit numbers
stored at offsets 4000H and 5000H respectively. Initialize DS at 2000H. Store the 16-bit result in DX. Assume no
overflow.

List File:

Microsoft (R) Macro Assembler Version 6.11 11/03/04 13:44:38


ex97.asm Page 1 - 1

0000 CODE SEGMENT


ASSUME CS:CODE,DS:DATA
0000 B8 2000 MOV AX,2000H ;Initialize
0003 8E D8 MOV DS,AX ;Data Segment
0005 B9 0064 MOV CX,100 ;Initialize loop count
0008 BB 4000 MOV BX,4000H ;Initialize pointer of Xi
000B BE 5000 MOV SI,5000H ;Initialize pointer of Yi
000E BA 0000 MOV DX,0000H ;Initialize sum to 0
0011 8A 07 START: MOV AL,[BX] ;Load data into AL
0013 F6 2C IMUL BYTE PTR [SI] ;Signed 8x8 multiplication
0015 03 D0 ADD DX,AX ;Sum XiYi
0017 43 INC BX ;Update pointer
0018 46 INC SI ;Update pointer
0019 E2 F6 LOOP START ;Decrement CX & loop
001B F4 HLT
001C CODE ENDS
0000 DATA SEGMENT
0000 DATA ENDS
END ;End program
Microsoft (R) Macro Assembler Version 6.11 11/03/04 13:44:38
ex97.asm Symbols 2 - 1

Segments and Groups:

N a m e Size Length Align Combine Class

CODE . . . . . . . . . . . . . . 16 Bit 001C Para Private


DATA . . . . . . . . . . . . . . 16 Bit 0000 Para Private

Symbols:

N a m e Type Value Attr

START . . . . . . . . . . . . . L Near 0011 CODE

0 Warnings
0 Errors
Debug Screen shots:
Show program at default CS=0BF0H.

Fill command is used to place a value (in this case 2) in 100 memory locations starting at
DS:4000 and value (in this case 3) in 100 memory locations starting at DS:5000.

Memory dump is used to verify placing 100 memory locations at DS:4000H with 2(Xi’s) and
DS:5000H with 3(Yi’s).
Start program. Initialize CX = 100 (64H) but changed to 2 to demonstrate program functionality.
The loop will iterate only 2 times. If Xi’s and Yi’s are 2 and 3 respectively, then Xi * Yi = 6 and
summing 6 twice will result into 12. The final answer should be DX = 12 which is shown in the
last screen shot below.

Multiply Xi and Yi and sum is in DX.


Loop decrements CX and enters second iteration.

CX = 0 and Loop terminates. (DX) = 000CH which is 12 in decimal. The correct answer is
stored.

You might also like