Question

Write a program in MASM (32 bit) that calculates the sum of all odd numbers in...

Write a program in MASM (32 bit) that calculates the sum of all odd numbers in the Fibonacci sequence between 0 and 1,000,000.

***Please Complete in MASM 32 bit!!***

Homework Answers

Answer #1

Program:

Sample output:

Code to copy:

;fib.asm

global _main

extern _printf

SECTION .data                ; data section

     msg1:     db 'All odd numbers in the Fibonacci sequence between 0 and 1,000,000:',10,0     ; the string to print, 10=cr

     msg2:     db 'Sum of all these odd numbers : ',10,0    ; the string to print, 10=cr

     fmt:    db "%d", 10, 0 ; The printf format, "\n",'0'

     f1: dd 0

     f2: dd 1

     sum: dd 0

     oddsum: dd 1

section .text

_main:

    

     push      msg1      ; print the label msg1

     call      _printf

     add     esp, 4      ; pop stack 4 bytes

    

     push    ebp             ; set up stack frame

    mov     ebp,esp

    

     mov       eax, [f2]

     push      eax            ; the 1st odd number (1) of the sequence

     push    dword fmt   ; push format string

     call    _printf          ; print the 1st odd number (1) of the sequence

     add     esp, 8      ; pop stack 2 push times 4 bytes

    

_printnext:

     ; take prevoius two terms into eax and ebx

     mov       eax, [f1] ; put a from store into register

     mov ebx,[f2]

    

     add eax,ebx        ; generate next term

     mov [sum],eax ; save the next term into sum

    

     mov ecx,1000000        

     cmp ecx,eax        ; compare whether this term is less than 1000000

     jl        _exit          ; if the term exceeds 1000000, exit the loop

    

     ; otherwise, check the term is odd or even

     mov ebx,eax       

     mov ecx ,2

     div ecx            ; divide the term by 2. eax= quetient , edx=remainder

    

     mov eax,1

     cmp edx,eax        ; check whether the remainder is 1 or 0

     jnz _next          ; if the term is even, go to next iteration

    

     mov eax,[oddsum]  

     add eax,ebx             ; if the term is odd, add it to oddsum

     mov [oddsum],eax ; update oddsum

     push ebx            ; pass term to printf

    push    dword fmt    ; pass format string to printf

    call    _printf      ; Call C function to print the term

    add     esp, 8       ; pop stack 2 push times 4 bytes   

    

_next:

     mov ebx,[f2] ; update the prevoius terms

     mov [f1],ebx

     mov ebx,[sum]

     mov [f2],ebx

    

     jmp _printnext      ; go to _printnext

    

_exit:   

     push      msg2      ; print the label msg2

     call      _printf

     add     esp, 4      ; pop stack 1 push times 4 bytes

    

     mov eax,[oddsum] ; mov the oddsum into eax

     push      eax            ; pass oddsum to push

     push      dword fmt ; pass the format string to printf

     call      _printf        ; Call C function to print the term

     add esp,8          ; pop stack 2 push times 4 bytes

         

     mov     esp, ebp    ; takedown stack frame

    pop     ebp             

    

     mov       eax,0          ; normal, no error, return value

    ret

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
Python code Write a complete program with for loop which calculates the sum of the numbers...
Python code Write a complete program with for loop which calculates the sum of the numbers from 1 to 100
Write a program to input a sequence of 8 integers, count all the odd numbers and...
Write a program to input a sequence of 8 integers, count all the odd numbers and sum all the negative integers. One integer is input and processed each time around the loop. Python Programming
Write a C program that calculates and displays the sum of all fourdigit numbers in the...
Write a C program that calculates and displays the sum of all fourdigit numbers in the line. The only include to use is <stdio.h>. The only keywords:void, int, char, while,if, else, and return. The only library functions:printf(), getchar().The only predefined constant:EOF. Input            output 3153\n           3153 00020005\n 7 999578585    error 456 5443       error 6574y435      error
Write a Java program that gets a 4x4 array of numbers and that calculates the sum...
Write a Java program that gets a 4x4 array of numbers and that calculates the sum and average of numbers on the main diagonal, ie the diagonal that goes from the left corner down to the right corner. Examples of resulting programs are: Enter a 4x4 matrix row by row: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 The sum of numbers in the diagonal is: 16 The average of numbers in...
1) Use a loop structure to calculate the sum of all odd numbers from 1 to...
1) Use a loop structure to calculate the sum of all odd numbers from 1 to 15 2) Use a loop structure to calculate the product of all odd numbers from 1 to 9. 3) Write a program to convert US dollars to Bangaldeshi taka
Please code C# 7. Write a program that can calculate the sum of all numbers between...
Please code C# 7. Write a program that can calculate the sum of all numbers between 1 and 1000 inclusive (including 1 and 1000)
Write a program in C that calculate and print the summation of odd numbers between 1-15...
Write a program in C that calculate and print the summation of odd numbers between 1-15 and then find the average of the summation of these numbers, please use double data type to declare all variables. Use for or do/while loop, please explain the steps aswell
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them.         [0.5 mark] (BY USING C PROGRAM)
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT