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

Program 8: Linear Search

 **Program 8: Linear Search** ```assembly .model small .stack 100h .data     array db 10, 20, 30, 40, 50     search_value db 30     found db 0 .code     main proc         mov si, 0           ; Initialize index register SI         mov al, search_value ; Load the search value into AL         mov cx, 5           ; Set the loop counter              search_loop:         cmp al, [array + si] ; Compare AL with array element         je found_element     ; Jump if equal (found)         inc si               ; Increment index         loop search_loop     ; Decrement counter and loop if not zero                ...

Program 10: Matrix Multiplication

     ** Program 10: Matrix Multiplication ** ```assembly .model small .stack 100h .data     matrix1 dw 1, 2, 3, 4     matrix2 dw 5, 6, 7, 8     result dw 0, 0, 0, 0 .code     main proc         mov ax, 0              ; Initialize AX to 0              outer_loop:         mov cx, 2              ; Set loop counter for rows              inner_loop:         mov bx, ax             ; Copy AX to BX (row index)         add bx, cx             ; Add CX to BX (row index + column index)                  mov dx, 0              ; Initialize DX (column index)        ...