Program to Calculate the Sum of Ten Numbers in Assembly Language

Program to Calculate the Sum of Ten Numbers in Assembly Language

Program to Calculate the Sum of Ten Numbers in Assembly Language

Code

[org 0x100]
mov ax,0
mov bx,0

l1:
add ax,[num1+bx] ;Addition will be stored in ax and in num1 array wise iteration will take place
add bx,2
cmp bx,20
jne l1

mov [total],ax

mov ax,0x4c00
int 0x21

num1: dw 10,20,30,40,50,10,20,30,40,50

total: dw 0

Explanation:

  • This assembly code calculates the sum of ten numbers stored in the num1 array.

  • The registers ax and bx are initialized to zero.

  • The program enters a loop labeled l1 to iterate through the num1 array.

  • Inside the loop, it adds the value at the current index of the num1 array to the accumulator ax.

  • The index register bx is incremented by 2 to move to the next index of the array.

  • After each iteration, the program compares the value of bx with 20 to check if it has reached the last index.

  • If bx is not equal to 20, the program jumps back to the l1 label to continue adding the remaining numbers.

  • Once all ten numbers have been processed, the final sum in ax is stored in the memory location total.

  • The program then terminates by setting the value of ax to 0x4c00 (indicating program termination) and making an interrupt call to the operating system.

  • The num1 array contains the numbers to be added, and the total memory location is used to store the final sum.

  • The code efficiently calculates the sum without using a separate counter variable.

Instructions:

To run the program in a debugger write

  • nasm file_name.asm -o file_name.com

  • then afd file_name.com it will run the code written in that file

To View the code Written

  • The .lst file is generated during assembly and provides a detailed listing of the assembly source code, including memory addresses, machine code, symbol table, and debugging information, aiding in code review, debugging, and optimization.

  • nasm file_name.asm -l file_name.lst to make .lst file and do code review and debugging.