full subtractor
0 Stars     5 Views    

Author: GIDEONSAMUEL M

Project access type: Public

Description:

odd/even 


; You may customize this and other start-up templates; 

; The location of this template is c:\emu8086\inc\0_com_template.txt


org 100h


numbers db 4,5,7,1,66

count db 5


XOR BX,BX

XOR DX,DX     


LEA SI,numbers

MOV CL,count


PROCESS:

    MOV AL,[SI]

    TEST AL,1

    JZ EVEN

    

    INC BX

    JMP CONT

    

EVEN:

    INC DX


CONT:

    INC SI

    DEC CL

    

    JNZ PROCESS

    

    MOV AH, 4CH

    INT 21H


---------------------------------------------------------------------------------------------------------------------------------------

ORG 100h  ; Start at offset 100h (mandatory for .COM file format)   


; Data section

numbers DB 1,3,5,7,9,7,12 ; Example series of numbers

count   DB 7                       ; Total numbers in the series


; Initialize counters

XOR BX, BX         ; BX = 0 (odd count)

XOR DX, DX         ; DX = 0 (even count)


; Load the address of the numbers array

LEA SI, numbers    ; SI = offset of numbers array

MOV CL, count      ; CL = number of elements in the series


PROCESS_NUMBERS:

    ; Load the current number from the array

    MOV AL, [SI]


    ; Check if the number is odd or even

    test AL, 1      ; Perform AL AND 1

    JZ EVEN_NUMBER  ; If result is 0, the number is even

    ; Odd number

    INC BX          ; Increment odd count

    JMP CONTINUE


EVEN_NUMBER:

    INC DX          ; Increment even count


CONTINUE:

    INC SI          ; Move to the next number in the array

    DEC CL          ; Decrement the counter

    JNZ PROCESS_NUMBERS ; Repeat until all numbers are processed


    ; Terminate program

    MOV AH, 4CH     ; DOS interrupt to terminate program

    INT 21H

-----------------------------------------------------------------------------------

LCM

org 100h


; Input values for GCD and LCM

mov AX, 18      ; First number

mov BX, 6       ; Second number


; Preserve original values of AX and BX for LCM calculation

mov CX, AX       ; Store AX in CX

mov DX, BX       ; Store BX in DX


; Calculate GCD

GCD:

    cmp AX, BX

    je GCD_DONE   ; If AX == BX, GCD is found

    jg GREATER    ; If AX > BX, subtract BX from AX

    

    sub BX, AX    ; BX = BX - AX

    jmp GCD


GREATER:

    sub AX, BX    ; AX = AX - BX

    jmp GCD


GCD_DONE:

    ; At this point, AX holds the GCD

    ; Calculate LCM using: LCM = (CX * DX) / GCD

    mul DX         ; AX = CX * DX (product of the original numbers)

    div BX         ; DX = (AX / GCD), where GCD is stored in BX

                   ; Resulting LCM is now in AX

    

    mov DX, AX     ; Store the LCM in DX for final result


; Exit program

    mov AH, 4CH    ; DOS interrupt to exit program

    int 21H




Created: Nov 24, 2024

Updated: Nov 24, 2024


Comments

You must login before you can post a comment.