0% found this document useful (0 votes)
9 views19 pages

Assembly Language

The document contains various examples of 8085 assembly language programs demonstrating different operations such as comparison, division, finding the greatest number, multiplication, logical operations, and array manipulations. Each example includes comments explaining the purpose and functionality of the code. Additionally, it presents application questions that illustrate practical uses of assembly programming for tasks like passcode verification and temperature monitoring.

Uploaded by

fridahkawira021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Assembly Language

The document contains various examples of 8085 assembly language programs demonstrating different operations such as comparison, division, finding the greatest number, multiplication, logical operations, and array manipulations. Each example includes comments explaining the purpose and functionality of the code. Additionally, it presents application questions that illustrate practical uses of assembly programming for tasks like passcode verification and temperature monitoring.

Uploaded by

fridahkawira021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

;

Assume the manager code is in memory at 2000H and cashier code at 2001H

LDA 2000H ; Load manager code to accumulator A


MOV B, A ; Copy A to B
LDA 2001H ; Load cashier code to accumulator A
CMP B ; Compare cashier code (A) with manager code (B)
JZ MATCH ; If equal, jump to MATCH
JMP NOMATCH ; Otherwise, jump to NOMATCH

MATCH:
MVI C, 01H ; Example: move 1 to register C to indicate success
OUT 01H ; Output to port (opens lock)
HLT

NOMATCH:
MVI C, 00H ; Example: move 0 to register C to indicate failure
OUT 01H ; Output to port (keeps lock closed)
HLT
EXAMPLE 3

8085 Assembly Program (Using Repeated Subtraction) 25 divided by 6


assembly
Load dividend (numerator) into A
LDA 2000H ; A <- [2000H]
MOV B, A ; B = Dividend (numerator)

; Load divisor into C


LDA 2001H ; A <- [2001H]
MOV C, A ; C = Divisor (denominator)

MVI D, 00H ; D will store Quotient


LOOP: CMP C ; Compare A with C
JC END ; If A < C, jump to END (done dividing)
SUB C ; A = A - C (remainder gets smaller)
INR D ; D = D + 1 (count quotient)
JMP LOOP ; Repeat

END:
MOV E, A ; E = Remainder (leftover in A)
MOV A, D ; Move quotient to A
STA 2002H ; Store quotient at 2002H
MOV A, E
STA 2003H ; Store remainder at 2003H
HLT ; Halt

EXAMPLE 4

8085 Assembly Program to Find the Greatest Number


Assembly

LXI H, 2000H ; HL points to 2000H


MOV C, M ; C = counter (number of elements)
INX H ; Move to first number at 2001H
MOV A, M ; Assume first number is the greatest
DCR C ; Decrease counter (already used one number)

NEXT:
INX H ; Point to next number
CMP M ; Compare with current value in A
JC SKIP ; If A < M, skip replacing
MOV A, M ; Else, update A with new greatest

SKIP:
DCR C ; Decrease counter
JNZ NEXT ; Repeat if more numbers left

STA 3000H ; Store greatest number in 3000H


HLT
EXAMPLE 5

8085 Assembly Program (Multiplication Using Repeated Addition) 5*4


assembly
LDA 2000H ; Load multiplicand into A
MOV B, A ; Store it in B

LDA 2001H ; Load multiplier into A


MOV C, A ; Store it in C (counter)

MVI A, 00H ; Clear A to accumulate result


MVI D, 00H ; D = 0 (will be used to accumulate)

LOOP: ADD B ; A = A + B (add multiplicand)


DCR C ; Decrease counter
JNZ LOOP ; Repeat until C = 0

STA 2002H ; Store result at 2002H


HLT ; End program

Examples of Logical Operations

1. AND Operation (ANA)


assembly
MVI A, 0F0H ; A = 11110000
MVI B, 0F5H ; B = 11110101
ANA B ; A = A AND B → 11110000 AND 11110101 = 11110000
2. OR Operation (ORA)
assembly
MVI A, 0F0H ; A = 11110000
MVI C, 0F5H ; C = 11110101
ORA C ; A = A OR C → 11110000 OR 11110101 = 11110101

3. XOR Operation (XRA)


assembly

MVI A, 0F0H ; A = 11110000


MVI D, 0F5H ; D = 11110101
XRA D ; A = A XOR D → 11110000 XOR 11110101 = 00000101

4. Complement Accumulator (CMA)


assembly
MVI A, 55H ; A = 01010101
CMA ; A = ~A → 10101010

5. Compare (CMP)
assembly
MVI A, 30H ; A = 48
MVI E, 40H ; E = 64
CMP E ; Compare A and E. Sets flags:
; - Zero flag if A == E
; - Carry if A < E

6. Rotate Left (RLC)


assembly
MVI A, 85H ; A = 10000101
RLC ; A = 00001011 (bit7 goes to bit0 and carry)

7. Set Carry (STC) and Complement Carry (CMC)


assembly
STC ; Set CY = 1
CMC ; Complement CY (if CY=1 → CY=0)

OTHER EXAMPLES

I. Find the Smallest Number in an Array

Compare elements and track the smallest.

Program:

LXI H, 2000H ; HL = start address


MOV C, M ; C = count of numbers
INX H ; HL = points to first number
MOV A, M ; A = first number (assumed smallest)
DCR C

NEXT:
INX H
CMP M
JC SKIP ; If A < M, skip
MOV A, M ; Else, update A

SKIP:
DCR C
JNZ NEXT

STA 3000H ; Store smallest in 3000H


HLT
2. Count Even and Odd Numbers from an Array

Use masking (AND 01H) to check LSB.

Program:

LXI H, 2000H
MOV C, M ; Counter
INX H

MVI D, 00H ; D = even count


MVI E, 00H ; E = odd count

LOOP:
MOV A, M
ANI 01H ; Check LSB
JZ EVEN
INR E ; Increment odd count
JMP CONTINUE

EVEN:
INR D ; Increment even count

CONTINUE:
INX H
DCR C
JNZ LOOP
MOV A, D
STA 3001H ; Even count
MOV A, E
STA 3002H ; Odd count
HLT
3. Sort an Array in Ascending Order (Bubble Sort)

Nested loop, repeated swapping using temp register.


LXI H, 2000H
MOV B, M ; B = number of elements
DCR B ; (n - 1) outer passes

OUTER:
LXI H, 2001H ; HL = start of data
MOV C, B ; Inner loop count

INNER:
MOV A, M
INX H
CMP M
JC SKIP
MOV D, M
MOV M, A
DCX H
MOV M, D
INX H

SKIP:
DCR C
JNZ INNER

DCR B
JNZ OUTER
HLT
4. Find Factorial of a Number

Multiply descending numbers down to 1 using repeated addition or loop multiplication.

Example: Factorial of number in 2000H

LDA 2000H ; Load number (n)


MOV B, A ; Counter = n
MVI A, 01H ; A = 1 (start factorial)

LOOP:
MOV C, B
DCR C
JZ DONE

CALL MULTIPLY ; Multiply A = A * B


DCR B
JNZ LOOP

DONE:
STA 3000H ; Store result
HLT

; Multiply subroutine (A * C result in A)


MULTIPLY:
PUSH B
PUSH D
MOV D, A
MVI A, 00H

MLOOP:
ADD D
DCR C
JNZ MLOOP

POP D
POP B
RET

5. Reverse an Array

Swap elements from both ends moving inward.

LXI H, 2000H
MOV C, M ; Count of elements
MOV B, C
DCR B ; For half swaps

LXI D, 2001H ; Start pointer


LXI H, 2001H
MOV A, C
ADD L
MOV L, A
DAA
DCX H ; HL = end pointer

REVERSE:
MOV A, M ; A = M[end]
MOV E, A
MOV A, M ; A = M[start]
MOV M, E
XCHG ; Swap HL and DE
INX D
DCX H
DCR B
JNZ REVERSE

HLT
Application questions
1. Design a system that checks if a passcode (e.g., 55H) is entered. If correct, turn on an LED (simulate by storing 01H in memory).
Otherwise, store 00H.
LDA 2000H ; Load user input
CPI 55H ; Compare with password
JZ CORRECT
MVI A, 00H
STA 3000H ; Lock remains closed
HLT

CORRECT:
MVI A, 01H
STA 3000H ; Unlock (simulate LED ON)
HLT
2. If the temperature (from a sensor, stored at 2000H) is over 30°C (e.g., 1EH), turn ON a fan (store 01H), otherwise OFF (00H).
LDA 2000H ; Read temperature
CPI 1EH ; Compare with 30°C
JC LOW_TEMP
MVI A, 01H
STA 3001H ; Fan ON
HLT

LOW_TEMP:
MVI A, 00H
STA 3001H ; Fan OFF
HLT

You might also like