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 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.
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.
Hi this is Assembly Language MASM x86 program. Please write it in the language and please...
Hi this is Assembly Language MASM x86 program. Please write it in the language and please explain it with comments thank you Please answer it I really need help this question was refunded before so please answer. Thank you so much also these are two separate programs thank you. 1) Write a procedure to read in decimal or hex number (byte-sized) Then write a procedure using shifts and ANDS to convert the string to a binary number (if is backward,...
Write a MASM program that uses a loop to multiply 2 numbers. Use the equal sign...
Write a MASM program that uses a loop to multiply 2 numbers. Use the equal sign directive to define the two numbers. Save the product in the EAX register. Hint 4 x 5 = 4 + 4 +4 + 4 + 4 = 5 + 5 + 5 + 5 Language (Assembly) ASAP
Assembly using x86 irvine (masm) Write a complete program that will input values for num1 ,num2,...
Assembly using x86 irvine (masm) Write a complete program that will input values for num1 ,num2, and num3 and display the value of the expression ( (num1 ^ 3) * num2 + 5 * ( num2 ^ 2) ) / num3. assume that the user enters only numbers that are greater than zero and the calculation never exceed 4 bytes size. Sample run: num1 = 1 num2 = 2 num3 = 3 ((num1 ^ 3) * num2 + 5 *...
Using visual studio in MASM x86, Write a program to add the following word size numbers:...
Using visual studio in MASM x86, Write a program to add the following word size numbers: 15F2, 9E89, 8342, 99FF, 7130 using adc instruction and a loop. The result must be in DX, AX. Show the result in debug window.
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT