Skip to main content

Program 4: Factorial Calculation

 **Program 4: Factorial Calculation**


```assembly

.model small

.stack 100h


.data

    num dw 5

    result dw 1


.code

    main proc

        mov cx, num          ; Load 'num' into CX

        mov ax, 1            ; Initialize 'result' to 1


    loop_start:

        mul cx               ; Multiply AX by CX (result *= num)

        loop loop_start      ; Decrement CX and loop if not zero


        mov result, ax       ; Store result in 'result'

        

        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.

   - `num dw 5`: Defines a word-sized variable named "num" with value 5.

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

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

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

   - `mov ax, 1`: Initialize AX to 1 (for the result).

   - `loop_start:`: Label for the loop.

   - `mul cx`: Multiply AX by CX (result *= num).

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

   - `mov result, ax`: Store the result in the "result" variable.

   - `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...