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 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)
Please complete in MASM (x86 assembly language). Use the code below to get started. Write a...
Please complete in MASM (x86 assembly language). Use the code below to get started. Write a program that uses a loop to calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n – 1) + Fib(n – 2). .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD .data    ; define your variables here .code main PROC    ; write your assembly code here    INVOKE ExitProcess,0 main...
Write an ARM assembly program that calculates the number of leading ones in a 64-bit integer.
Write an ARM assembly program that calculates the number of leading ones in a 64-bit integer.
Write an assembly language program that reverses all the bits of a 32-bit number. (Without using...
Write an assembly language program that reverses all the bits of a 32-bit number. (Without using RBIT instruction) Write an assembly language program that checks whether an unsigned number is perfect square or not.
Write a MASM program that computes the sum of the integers from 1 to N where...
Write a MASM program that computes the sum of the integers from 1 to N where N is a positive integer. Use the equal sign directive to define N. Save the sum in the EAX register. You must use loops. For example, 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Language ( Assembly)...
Please Write the whole program in assembly i am able to calculate the fibonacci series but...
Please Write the whole program in assembly i am able to calculate the fibonacci series but not sure how to print it in reverse order. Please give a Complete code !! Programming Exercise 1 (10 points): [call it Ass2-Q1.asm] Write an ASM program that reads an integer number N and then displays the first N values of the Fibonacci number sequence, described by: Fib(0) = 0, Fib(1) = 1, Fib(N) = Fib(N-2) + Fib(N-1) Thus, if the input is N...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn with seed values F1 = 1 F2 = 1 For more information on...