Skip to main content

Program 5: Counting Digits in a Number

 **Program 5: Counting Digits in a Number**


```assembly

.model small

.stack 100h


.data

    num dw 12345

    count db 0


.code

    main proc

        mov ax, num           ; Load the number into AX

        mov bx, 10


            ; Divisor for digits

        

    count_digits:

        xor dx, dx             ; Clear DX for division

        div bx                 ; Divide AX by BX

        inc count              ; Increment digit count

        test ax, ax            ; Check if quotient is zero

        jnz count_digits       ; If not zero, loop again

        

        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 12345`: Defines a word-sized variable named "num" with value 12345.

   - `count db 0`: Defines a byte-sized variable named "count" with initial value 0.

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

   - `mov ax, num`: Move the value of "num" into the AX register.

   - `mov bx, 10`: Load the divisor 10 into BX.

   - `count_digits:`: Label for the loop.

   - `xor dx, dx`: Clear DX register for division.

   - `div bx`: Divide AX by BX (quotient in AX, remainder in DX).

   - `inc count`: Increment the "count" variable for each digit encountered.

   - `test ax, ax`: Test if quotient (AX) is zero.

   - `jnz count_digits`: Jump to "count_digits" label if quotient 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

Program 6: Finding Prime Numbers in a Range

  ** Program 6: Finding Prime Numbers in a Range ** ```assembly .model small .stack 100h .data     start dw 1     end dw 20 .code     main proc         mov ax, start           ; Load 'start' into AX              find_primes:         inc ax                  ; Move to the next number         cmp ax, end             ; Compare with 'end'         jg end_prog             ; Jump to end if AX > 'end'                  mov cx, ax              ; Copy AX to CX   ...

ktu 2019 Cse Complier programing Lab Syllabus

 1Design and implement a lexical analyzer using C language to recognize all valid tokens  in the input program. The lexical analyzer should ignore redundant spaces, tabs and  newlines. It should also ignore comments. 2. Implement a Lexical Analyzer for a given program using Lex Tool. 3. Write a lex program to display the number of lines, words and characters in an input text. 4. Write a LEX Program to convert the substring abc to ABC from the given input string. 5. Write a lex program to find out the total number of vowels and consonants from the given  input string. 6. Generate a YACC specification to recognize a valid arithmetic expression that uses  operators +, – , *,/ and parenthesis. 7. Generate a YACC specification to recognize a valid identifier which starts with a letter  followed by any number of letters or digits.   8. Implementation of Calculator using LEX and YACC  9. Convert the BNF rules into YACC form and write code to generat...

11. Write a program to convert NFA with ε transition to NFA without ε transition

  Converting an NFA with ε transitions to an NFA without ε transitions involves removing ε transitions and appropriately updating the transitions in the resulting NFA. Below is an example program in Python that performs this conversion. I'll explain each line of the code: ```python class NFA:     def __init__(self, states, alphabet, transitions, start_state, accept_states):         self.states = states         self.alphabet = alphabet         self.transitions = transitions         self.start_state = start_state         self.accept_states = accept_states def epsilon_closure(states, transitions):     epsilon_states = set(states)     stack = list(states)          while stack:   ...