Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
2
Indexable
Never
ORG 100h

include 'emu8086.inc'

; Define macros for checking perfect and Armstrong numbers
; Macro to check if a number is a perfect number
define PERFECT_NUM
    local num, sum, count
    mov num, ax           ; Move the number to be checked into num
    mov sum, 0            ; Initialize the sum to zero
    mov cx, num           ; Initialize count to num
    dec cx
    loop1:
        mov bx, cx        ; Move count into bx
        xor dx, dx        ; Clear dx for division
        div bx            ; Divide num by bx, remainder in dx
        cmp dx, 0         ; Check if remainder is zero
        jne not_divisible ; If not, skip adding bx to sum
        add sum, bx       ; Add bx to sum if it is a factor of num
        not_divisible:
        dec cx            ; Decrement count
        cmp cx, 0         ; Check if count is zero
        jne loop1         ; If not, repeat loop
    cmp sum, num          ; Compare the sum of the factors to num
    je perfect            ; If sum equals num, jump to perfect label
    jmp not_perfect       ; Otherwise, jump to not_perfect label
    perfect:
        mov ax, 1         ; Set ax to 1 to indicate perfect number
        jmp end_macro
    not_perfect:
        mov ax, 0         ; Set ax to 0 to indicate not a perfect number
    end_macro:
endm

; Macro to check if a number is an Armstrong number
define ARMSTRONG_NUM
    local num, sum
    mov num, ax           ; Move the number to be checked into num
    mov bx, num           ; Move num into bx
    xor cx, cx            ; Clear cx for counting digits
    digit_loop:
        shr bx, 1         ; Shift bx right by 1 bit
        jnc digit_loop    ; If the carry flag is not set, repeat loop
        inc cx            ; Increment digit count
    mov bx, num           ; Move num back into bx
    mov sum, 0            ; Initialize sum to zero
    armstrong_loop:
        mov dx, 0         ; Clear dx for division
        div 10            ; Divide bx by 10, quotient in ax, remainder in dx
        mov si, ax        ; Move quotient into si
        mov ax, dx        ; Move remainder back into ax
        mov bx, si        ; Move quotient back into bx
        mul si            ; Multiply remainder by itself, result in ax
        add sum, ax       ; Add result to sum
        cmp bx, 0         ; Check if quotient is zero
        jne armstrong_loop ; If not, repeat loop
    cmp sum, num          ; Compare the sum to num
    je armstrong          ; If sum equals num, jump to armstrong label
    jmp not_armstrong     ; Otherwise, jump to not_armstrong label
    armstrong:
        mov ax, 1         ; Set ax to 1 to indicate Armstrong number
        jmp end_macro
    not_armstrong:
        mov ax, 0         ; Set ax to 0 to indicate not an Armstrong number
    end_macro:
endm

start:
    ; Ask user to input a number
    mov ah, 09h
    lea dx, prompt1
    int 21h