CS 3005-1 Microprocessor and Embedded Systems
ADDITIONAL PROGRAMMING EXAMPLES
1. Write an ALP to find LCM of two 8-bit numbers
DATA SEGMENT
NUM1 DB 12 ; First 8-bit number (change as needed)
NUM2 DB 18 ; Second 8-bit number (change as needed)
RESULT DW ? ; Variable to store the result
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA ; Initialize DS register (Data segment)
MOV DS, AX
; Calculate GCD (Greatest Common Divisor)
MOV AL, NUM1
MOV BL, NUM2
CALL GCD
; Calculate LCM (Least Common Multiple) using formula LCM(a, b) = (a * b) / GCD(a, b)
MOV AX, NUM1 ; Load AX with the first number
MUL NUM2 ; Multiply by the second number
DIV RESULT ; Divide by the GCD to get LCM
MOV RESULT, AX ; Store the result in RESULT variable
; Display the result
MOV AH, 4CH ; Function to terminate program
INT 21H
; Subroutine to calculate GCD using Euclidean algorithm
GCD PROC NEAR
XOR DX, DX ; Clear DX register
EUCLID_LOOP:
DIV BL ; Divide AL by BL, result in AX, remainder in DX
MOV AL, BL ; Move BL to AL
MOV BL, DL ; Move remainder to BL
TEST BL, BL ; Test if remainder is zero
JNZ EUCLID_LOOP ; If not zero, continue the loop
MOV RESULT, AL ; GCD stored in RESULT
RET
GCD ENDP
CODE ENDS
END START
Akhilraj V. Gadagkar, Asst. Prof. Dept. of CSE, NMAMIT, Nitte Page 1
CS 3005-1 Microprocessor and Embedded Systems
2. Write an ALP to find maximum element in a byte array.
DATA SEGMENT
arr db 02H,03H,12H,13H,14H ; byte array
max db ? ; Variable to store max element, initialized to zero as current max value
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA ; initialize data segment
MOV DS, AX
MOV BL, max
MOV CL, 05H ; initialize count (CL Register) to 5
LEA SI, arr ; Load effective address of arr to SI
AGAIN:
CMP BL, [SI]
JA L1
MOV BL, [SI]
L1:
INC SI
LOOP AGAIN ; loop unit count (CL Register) is zero
MOV max, BL ; store the maximum element in location max
MOV AH, 4CH ; program termination
INT 21H
CODE ENDS
END START
Akhilraj V. Gadagkar, Asst. Prof. Dept. of CSE, NMAMIT, Nitte Page 2
CS 3005-1 Microprocessor and Embedded Systems
3. Write an ALP to find minimum element in a byte array.
DATA SEGMENT
arr db 02H,03H,12H,13H,14H ; byte array
max db 20H ; Variable to store minimum element, initialized with a maximum value
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA ; initialize data segment
MOV DS, AX
MOV BL, max
MOV CL, 05H ; initialize count (CL Register) to 5
LEA SI, arr ; Load effective address of arr to SI
AGAIN:
CMP BL, [SI]
JB L1
MOV BL, [SI]
L1:
INC SI
LOOP AGAIN ; loop unit count (CL Register) is zero
MOV max, BL ; store the minimum element in location max
MOV AH, 4CH ; program termination
INT 21H
CODE ENDS
END START
Akhilraj V. Gadagkar, Asst. Prof. Dept. of CSE, NMAMIT, Nitte Page 3
CS 3005-1 Microprocessor and Embedded Systems
4. Write an ALP to determine the parity of a given number
DATA SEGMENT
NUM DB 10101010b ; 8-bit number for which parity needs to be calculated
; Suffix b indicates the number is binary
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
MOV AX, DATA ; Initialize DS register (Data Segment)
MOV DS, AX
; Initialize parity flag to 0
XOR AH, AH
; Calculate parity
MOV AL, NUM
PARITY_LOOP:
TEST AL, 1 ; Test the least significant bit
JNZ SET_PARITY ; If it's set, set the parity flag
SHR AL, 1 ; Right shift AL
JMP PARITY_CHECK ; Jump to the parity check
SET_PARITY:
INC AH ; Increment the parity flag
SHR AL, 1 ; Right shift AL
PARITY_CHECK:
TEST AL, AL ; Check if AL is zero
JNZ PARITY_LOOP ; If not zero, continue the loop
; AH now contains the parity flag (1 for odd parity, 0 for even parity)
; Display the result
MOV AH, 4CH ; Function to terminate program
INT 21H
CODE ENDS
END START
Akhilraj V. Gadagkar, Asst. Prof. Dept. of CSE, NMAMIT, Nitte Page 4