Header Ads

Write an assembly language program to store your Roll Number and calculate their sum.

Write an assembly language program to store your Roll Number(Number part only) and calculate their sum.

Solution:

section .data

    roll_number db "MC160123456"   ; Example roll number (modify as needed)


section .text

    global _start

_start:

    mov esi, 0         ; Start at index 0 of the roll_number string

    mov ecx, 12        ; Set the loop counter for the 12 characters


    xor eax, eax       ; Clear the accumulator (sum)


sum_loop:

    mov dl, [roll_number + esi]     ; Load the current character


    cmp dl, '0'     ; Compare with the ASCII value of '0'

    jl skip_character ; If less than '0', jump to skip_character

    cmp dl, '9'     ; Compare with the ASCII value of '9'

    jg skip_character ; If greater than '9', jump to skip_character


    sub dl, '0'                     ; Convert the ASCII character to its numerical value

    add al, dl                      ; Add the current digit to the sum


skip_character:

    inc esi                         ; Move to the next character

    loop sum_loop                   ; Repeat until all characters are processed


    ; The sum is now stored in the AL register


    ; Display the sum

    mov dl, al       ; Move the sum to DL register for display

    add dl, '0'      ; Convert the sum to ASCII

    mov ah, 0x0E     ; Function code for displaying a character

    int 0x10         ; BIOS interrupt to display the sum


    ; Terminate the program

    mov eax, 1       ; Exit system call number

    xor ebx, ebx     ; Error code 0

    int 0x80         ; Interrupt to terminate the program


No comments

Powered by Blogger.