Question

In: Computer Science

Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...

Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array.
The size of the array is provided in a separate variable. Use the following design:
 

Sum = 0
FOR i = 0 TO SizeOfArray - 1
    IF Arr[i] % 2 = 1
          Sum = Sum + Arr[i]
    END IF
END FOR

Solutions

Expert Solution

Assume that the array starts from the memory Location 500, size of array = 5 and the RESULT SUM IS stored at location 0.

So We will add the numbers at locations 501 and 503 (odd locations).

Find below a generic MASM Assembler code for adding numbers at odd places in an array!

400 MOV SI, 500 SI is the register storing the memory address!
403 MOV CL, [SI] Register CL stores the value of Arr[i]
405 INC SI
406 MOV CH, 00 CH<-00
408 MOV AL, 00 Register AL stores the value of the variable SUM!
40A MOV BL, [SI] BL<-[SI]
40C TEST SI, 01

SI AND 01 (THE MAIN CATCH)

Explanation:

In order to check if a number is odd we and it with the number 01.

This works because if a number is odd it will have 1 as the LSB in its representation.(Eg. 1-> 0001 3->0011 5->0101 but 8->1000(Even)).

Now, if you bitwise AND a number with 01 (0001):

It will be zero at all places (since 0001 has zeroes at all places except LSB) except maybe at the LSB!  

If the number is even => 0 at LSB => AND will produce the number 0000.

If the number is odd => 1 at LSB => AND will produce the number 0001.

40F JZ 413 Jump to 413 memory location if zero flag is set
411 ADD AL, BL SUM +=Arr[i]
413 INC SI SI<-SI+1
414 LOOP 40A jump to 40A memory location if the content of CX is not equal to zero
416 MOV [600], AL [600], AL FINAL STORAGE OF SUM
41A HLT end

P.S. Please give a thumbs up if you find the answer helpful!


Related Solutions

Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
Write an assembly program that lets the user to input only the word MASM and displays...
Write an assembly program that lets the user to input only the word MASM and displays invalid input for any other user inputs.
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
Write a program that will calculate the sum of the first n odd integers, and the...
Write a program that will calculate the sum of the first n odd integers, and the sum of the first n even integers. Use a Do while loop Use a for loop Here is a sample of how the program should run: How many odd integers would you like to add? 5 Count Odd Integer Sum 1 1 1 2 3 4 3 5 9 4 7 16 5 9 25 The sum of the first 5 odd integers is...
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers...
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
in assembly language x86 Masm, Write a program that calculate the first seven values of the...
in assembly language x86 Masm, Write a program that calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(0) = 0, Fib(1) = 1, Fib(2) = Fib(0)+ Fib(1), Fib(n) = Fib(n-1) + Fib(n-2). You NEED to calculate each value in the series "using registers and the ADD operation" You can also use variables, Have your program print out "The first seven numbers is" Use WriteInt for the printing, Place each value in the EAX...
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
Write an Intel 8085 assembly program to find the largest of N numbers stored in memory...
Write an Intel 8085 assembly program to find the largest of N numbers stored in memory using the algorithm below. Hand trace (execute) the program showing the changes made to all affected registers and memory locations. Max = a(1) For i = n to N If max < a(i) then max = a(i) Next i
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT