Skip to main content

Program 7: Fibonacci Series

 **Program 7: Fibonacci Series**


```assembly

.model small

.stack 100h


.data

    count dw 10

    fib1 dw 0

    fib2 dw 1

    result dw ?


.code

    main proc

        mov cx, count       ; Load 'count' into CX

        

    fib_loop:

        add result, fib1    ; Add fib1 to result

        mov ax, fib1        ; Swap fib1 and fib2

        mov fib1, fib2

        mov fib2, ax

        loop fib_loop       ; Decrement CX and loop if not zero

        

        mov ah, 4Ch          ; Exit program

        int 21h

    main endp

end main

```


Explanation:

1. `.model small` and `.stack 100h`: Memory model and stack size definitions.

2. `.data` section: Declares the data segment.

   - `count dw 10`: Defines a word-sized variable named "count" with value 10.

   - `fib1 dw 0`: Defines a word-sized variable named "fib1" with initial value 0.

   - `fib2 dw 1`: Defines a word-sized variable named "fib2" with initial value 1.

   - `result dw ?`: Defines a word-sized uninitialized variable named "result".

3. `.code` section: Contains the main code.

   - `mov cx, count`: Move the value of "count" into the CX register.

   - `fib_loop:`: Label for


 the Fibonacci loop.

   - `add result, fib1`: Add the value of "fib1" to the "result".

   - `mov ax, fib1`: Swap the values of "fib1" and "fib2".

   - `mov fib1, fib2`: Move the value of "fib2" into "fib1".

   - `mov fib2, ax`: Move the value of "ax" (original "fib1") into "fib2".

   - `loop fib_loop`: Decrement CX and loop if CX is not zero.

   - `mov ah, 4Ch`: Set the exit code for DOS.

   - `int 21h`: Call DOS interrupt to exit the program.


Comments

Popular posts from this blog

12. Write a program to convert NFA to DFA.

Converting a Non-Deterministic Finite Automaton (NFA) to a Deterministic Finite Automaton (DFA) involves creating a new DFA where each state corresponds to a set of NFA states reachable under certain conditions. Below is a simple Python program to perform this conversion. I'll explain each line of code: ```python from collections import defaultdict def epsilon_closure(states, transitions, epsilon):     closure = set(states)     stack = list(states)          while stack:         state = stack.pop()         for next_state in transitions[state].get(epsilon, []):             if next_state not in closure:                 closure.add(next_state)                 stack.append(next_state)          return closure def nfa_to_dfa(nfa_states, nfa_transitions, alphabet, start_state, n...