Introduction-to-Assembly-Language-Programming-8085 - M2
Introduction-to-Assembly-Language-Programming-8085 - M2
Language Programming
(8085)
Welcome to the world of assembly language programming for the 8085
microprocessor. This presentation will guide you through the fundamentals
of assembly language, from basic concepts to practical implementation of
various operations including data transfer, arithmetic, logic, branching, and
I/O operations.
What is Assembly Language?
Assembly language is a low-level programming language that
provides a human-readable representation of machine code
instructions. Each assembly instruction has a direct one-to-one
correspondence with specific processor instructions, making it
the closest programming language to the hardware itself.
Optimization Skills
Assembly programming develops critical thinking about resource usage, performance
optimization, and efficient algorithm implementation—skills that translate to any
programming domain.
Historical Significance
The 8085 represents an important milestone in computing history, and understanding it
provides valuable context for the evolution of processor design and computer architecture.
The 8085 Microprocessor Overview
The 8085 processes data in 8-bit The instruction set includes 74 basic
chunks, with an 8-bit data bus for instructions that can perform various
transferring information between operations from simple data moves
components. to complex arithmetic.
Example:
Assemble
Convert assembly code to object code (.obj) using an assembler
Link
Combine object files and resolve references to create executable
Modern development typically uses simulators like GNUSim8085 or 8085sim that combine these steps into a streamlined process, allowing for easier
debugging and execution.
Addressing Modes in 8085
Immediate Addressing
Data is part of the instruction itself
Register Addressing
Operand is a register
Direct Addressing
Memory address is specified in the instruction
Indirect Addressing
Address is held in a register pair
The choice of addressing mode affects instruction size, execution speed, and program flexibility. Efficient programs typically use a strategic
mix of addressing modes based on the task requirements.
Flowchart of Assembly Programming
Flowcharts provide a visual representation of program logic, making it
easier to plan and understand assembly language programs before
coding.
Key flowchart elements for assembly programming:
Algorithm
1. Load the first number (2H) into register D
2. Load the second number (3H) into register E
3. Move the content of register D to accumulator A
4. Add the content of register E to accumulator A
5. Store the result from accumulator A to register C
6. Halt the processor
Between Registers
Transfer data between the processor's internal registers for
immediate processing
To/From Memory
Store results or retrieve data from RAM for longer-term storage
Data transfer instructions typically don't modify the data itself, but simply
move it to where it needs to be for processing or storage.
Types of Data Transfer Instructions
Register-to-Register
The MOV instruction transfers data between registers without altering it.
Register-to-Memory
Store register contents to memory location pointed by HL register pair.
Immediate Data
Load constants directly into registers.
Example: MVI D, 5FH loads the hexadecimal value 5FH into register D.
Memory-to-Register
Retrieve data from specified memory addresses.
Example: LDA 2050H loads accumulator with contents at memory address 2050H.
MOV Instruction
Register-to-Register Transfer
The MOV instruction is the most fundamental data transfer
operation in 8085 assembly. It copies data between registers
without modifying it.
Format:
Format:
Examples:
MVI provides the fastest way to initialize registers
MVI A, 5AH ; Load hex value 5AH into accumulator AMVI B, 00H with known values at the beginning of a program
; Clear register B by loading 0MVI M, FFH ; Store FFH at or subroutine. For memory operations, the M
memory location pointed by HL destination refers to the memory location whose
address is in the HL register pair.
MVI instructions require 2 bytes of memory: one for the opcode and one for
the immediate data. They typically execute in two machine cycles.
LDA and STA Instructions
LDA Instruction
Load Accumulator Direct
STA Instruction
Store Accumulator Direct
These instructions provide direct access to any memory location within the 64K
address space without needing to set up register pairs for addressing. They're
commonly used for accessing variables and I/O mapped devices.
LXI and LDAX/STAX Instructions
LXI (Load Register Pair Immediate)
LDAX B ; Load A from address in BC These instructions are particularly useful for:
STAX D ; Store A to address in DE
• Setting up memory pointers
These instructions use register pairs (only B-C or D-E) as • Initializing the stack
pointers to memory locations for indirect addressing • Working with arrays and data tables
operations. • Performing indexed addressing
Data Transfer with I/O Ports
IN 07H ; Read from port 07H into A OUT 01H ; Send contents of A to port 01H
Used for receiving data from peripherals like keyboards, sensors, and input switches. Used for transmitting data to peripherals like displays, printers, and control systems.
The 8085 supports 256 possible I/O ports (00H to FFH), providing a standardized way to interact with external devices. These instructions implement the I/O-mapped
architecture, where I/O devices have their own address space separate from memory.
Practical Example: Input/Output Transfer
Reading from Input Port to Memory
ADD B ; A = A + B
ADD M ; A = A + (HL)
ADI 05H ; A = A + 05H (immediate)
Subtraction Instructions
SUB C ;A = A – C
SUB M ; A = A - (HL) Flag Effects:
SUI 0AH ; A = A - 0AH (immediate)
• Carry (CY): Set if result exceeds 8 bits
• Zero (Z): Set if result is zero
Subtraction instructions subtract the content of a register, memory
• Sign (S): Set if result is negative (bit 7 is 1)
location, or immediate value from the accumulator and store the
result in the accumulator. • Parity (P): Set if result has even parity
• Auxiliary Carry (AC): Set if carry from bit 3 to bit 4
ADC, SBB Instructions
ADC (Add with Carry)
ADC B ; A = A + B + CY
ADC M ; A = A + (HL) + CY
ACI 05H ; A = A + 05H + CY
ADC instructions add the source operand and the carry flag to the accumulator.
They're essential for multi-byte addition when performing calculations with
numbers larger than 8 bits.
SBB C ; A = A - C – CY
SBB M ; A = A - (HL) – CY
SBI 0AH ; A = A - 0AH - CY
Multi-byte Addition Example
SBB instructions subtract the source operand and the carry flag (acting as
borrow) from the accumulator. They're used for multi-byte subtraction. ; Add 16-bit numbers in BC and DE, result in HLMOV A, C ; Get
low byte of first numberADD E ; Add low byte of second
numberMOV L, A ; Store low byte of resultMOV A, B ; Get high
byte of first numberADC D ; Add high byte + carryMOV H, A ;
Store high byte of result
INX, DCX, INR, DCR Instructions
INR DCR INX DCX
(Increment (Decrement (Increment (Decrement
Register/Mem Register/Mem Register Pair) Register Pair)
Increases the Decreases the
ory) ory)
Increases the Decreases the content of a content of a
content of a content of a register pair by 1 register pair by 1
register or register or
memory location memory location INX H ; HL DCX D ; DE
by 1 by 1 = HL + 1INX = DE - 1DCX
INR B ; B DCR C ; C B ; BC = SP ; SP =
= B + 1INR = C - 1DCR BC + 1 SP - 1
M ; (HL) = M ; (HL) =
(HL) + 1 (HL) - 1 Does not affect Does not affect
any flags any flags
Affects all flags Affects all flags
except CY except CY
These instructions are commonly used for loop counters, memory pointers, and array indexing in assembly programs.
DAA: Decimal Adjust Accumulator
Binary Coded Decimal (BCD) Operations
The DAA instruction adjusts the accumulator content after a BCD
addition to ensure the result remains in valid BCD format (each
nibble representing a decimal digit from 0-9).
Example:
For larger numbers (24-bit, 32-bit, etc.), the same pattern can be
extended with additional bytes and carry handling.
Logic Operations Overview
Logic operations in the 8085 perform bit-wise manipulation of data, enabling programs to test,
set, clear, or modify individual bits in a byte. These operations are fundamental for tasks like
masking, bit testing, flag checking, and implementing Boolean logic.
Logical Operations
• AND (ANA) - Bitwise AND
• OR (ORA) - Bitwise OR
• XOR (XRA) - Bitwise Exclusive OR
Comparison
• CMP - Compare register/memory with A
• CPI - Compare immediate with A
Rotate Operations
• RLC/RRC - Rotate A left/right
• RAL/RAR - Rotate A through carry
Complement
• CMA - Complement (invert) accumulator
• CMC - Complement carry flag
ANA, XRA, ORA Instructions
Bitwise Logical Operations
ANA (AND with Accumulator)
Performs bit-by-bit XOR operation. Useful for toggling bits and quick zero check. Common Applications:
ORA (OR with Accumulator) • AND: Isolate specific bits (e.g., ANI 0FH masks high nibble)
• OR: Set specific bits (e.g., ORI 80H sets the most significant bit)
ORA D ; A = A OR DORA M ; A = A OR (HL)ORI 80H ; A = A OR • XOR: Toggle bits or check for equality (XRA A clears accumulator)
80H (immediate)
All these operations clear the Carry and Auxiliary Carry flags, and affect Zero, Sign,
and Parity flags based on the result.
Compares the content of the accumulator with the specified register or memory location by performing a
subtraction (A - operand) but without storing the result.
Compares the accumulator with an immediate value using the same mechanism.
Flag Effects:
; Check if A equals BCMP BJZ EQUAL ; Jump if equal; Check if A less than CCMP CJC
LESS ; Jump if carry (A < C); Check if A greater than DCMP DJNZ NOT_EQUAL ; Must be
not equalJNC GREATER ; Jump if no carry (A ≥ D) ; If we get here, A = D
RAL, RAR, RLC, RRC (Rotate)
RLC (Rotate Left Circular) RRC (Rotate Right Circular) RAL (Rotate Left through Carry)
RAR (Rotate Right through Carry)
Rotates all bits in the Rotates all bits in the Rotates all bits in the Rotates all bits in the
accumulator one position to the accumulator one position to the accumulator one position to the accumulator one position to the
left. Bit 7 moves to bit 0 right. Bit 0 moves to bit 7 left through the Carry flag. Bit 7 right through the Carry flag. Bit
position, and also to the Carry position, and also to the Carry goes to Carry, and previous 0 goes to Carry, and previous
flag. flag. Carry goes to bit 0. Carry goes to bit 7.
RLC ; Rotate A left circular RRC ; Rotate A right circularRAL ; Rotate A left RAR ; Rotate A right
through carry through carry
Rotation operations are useful for serial data processing, multiplication/division by powers of 2, and bit manipulation tasks. Only the
Carry flag is affected; other flags remain unchanged.
Practical Logic Example
Example 1: Masking Lower Nibble
; Preserve only the lower nibble (bits 0-3) of AANI 0FH ; A = A AND 00001111b; Result: Upper nibble cleared,
lower nibble unchanged
; Set bits 0 and 7 in accumulatorORI 81H ; A = A OR 10000001b; Result: Bits 0 and 7 are set to 1
; Test if value in A is odd or evenANI 01H ; Isolate bit 0 (1 for odd, 0 for even)JZ EVEN ; Jump if zero
(bit 0 was 0 = even)JMP ODD ; Otherwise it's odd
; Swap upper and lower nibbles in A; Example: Convert 12H (00010010b) to 21H (00100001b)MOV B, A ; Save a
copyRLC ; Rotate left 4 timesRLCRLCRLC ; Now upper nibble is in lower positionANI 0FH ; Mask to keep only the
moved nibbleMOV C, A ; Save in CMOV A, B ; Restore original valueANI 0FH ; Mask to keep only lower nibbleRLC ;
Rotate left 4 times to move to upper positionRLCRLCRLCORA C ; Combine with saved nibble; Result: Nibbles are
swapped
Branching Operations Overview
Branching operations alter the normal sequential flow of program execution,
allowing for decision-making, loops, and subroutine calls. These instructions
manipulate the Program Counter (PC) to direct execution to different parts
of the program.
Restart
• RST 0 through RST 7
• Single-byte CALL to fixed addresses
JZ LABEL ; Jump if Zero flag is set (Z=1)JNZ LABEL ; Jump if Zero flag is not set
(Z=0)JC LABEL ; Jump if Carry flag is set (CY=1)JNC LABEL ; Jump if Carry flag is
not set (CY=0)JP LABEL ; Jump if Sign flag is not set (S=0, positive)JM LABEL ;
Jump if Sign flag is set (S=1, negative)JPE LABEL ; Jump if Parity flag is set (P=1,
even parity)JPO LABEL ; Jump if Parity flag is not set (P=0, odd parity)
; If-Then structure CPI 10H ; Compare A with 10H JNZ NOT_EQUAL ; Jump if not equal ;
Code for "then" part JMP ENDIF ; Skip "else" partNOT_EQUAL: ; Code for "else"
partENDIF: ; Continue program; While loopLOOP: ; Loop body DCR B ; Decrement counter
JNZ LOOP ; Repeat if not zero
CALL, RET Instructions
Subroutine Operations
Subroutines are reusable blocks of code that can be called from different parts of a program. They help in
modular programming and code reuse.
CALL Instruction
CALL pushes the return address (PC) onto the stack and jumps to the subroutine address.
RET Instruction
Subroutine Example:
RET pops the return address from the stack and jumps to that address.
; Main program MVI B, 05H ; Set parameter CALL DOUBLE ; Call subroutine MOV C,
A ; Save result HLT ; Halt; Subroutine to double a valueDOUBLE: MOV A,
B ; Get parameter ADD A ; Double it (A = A + A) RET ; Return
to caller
The stack is a LIFO (Last-In-First-Out) data structure in memory that keeps track of return addresses for
Practical Branching Example
16-bit Addition with Overflow Detection
The JNC instruction creates an efficient branch that only executes the
overflow handling code when necessary, optimizing program execution
for the common case (no overflow).
Looping and Iterative Constructs
Loop Implementation in Assembly
Loops in assembly language are created using a combination of:
1. Counter initialization
2. Loop body instructions
3. Counter modification (increment/decrement)
4. Conditional branch back to loop start
RST 6 0030H
RST 7 0038H
RST instructions are efficient single-byte calls to predefined memory locations, often used for
common routines or interrupt handling.
I/O and Machine Control
Operations Overview
I/O and machine control operations allow the 8085 to interact with external devices and
manage the processor's internal state. These instructions provide mechanisms for data
exchange with peripherals and control over the processor's execution mode.
I/O Operations
IN and OUT instructions facilitate data exchange with external devices through
dedicated I/O ports, allowing the processor to read sensors, control displays, and
communicate with other hardware components.
Processor Control
Instructions like HLT (Halt), NOP (No Operation), and DI/EI (Disable/Enable
Interrupts) manage the processor's execution state, providing control over
program flow and interrupt handling.
Interrupt Management
SIM (Set Interrupt Mask) and RIM (Read Interrupt Mask) instructions provide
software control over the 8085's interrupt system, allowing programs to
selectively enable or disable specific interrupt sources.
IN, OUT Instructions for I/O
Input/Output Port Operations
The 8085 uses a separate address space for I/O devices, allowing for up to 256 input
ports and 256 output ports (addresses 00H to FFH).
IN Instruction
The IN instruction reads a byte from the specified port and places it in the accumulator.
This is used to get data from external devices like switches, keypads, ADCs, or
communication interfaces.
OUT Instruction
The 8085's architecture supports both approaches, with I/O-mapped I/O being more
The OUT instruction sends the content of the accumulator to the specified port, allowing efficient for simple devices and Memory-mapped I/O providing more flexibility for
control of external devices like LEDs, displays, motors, or communication interfaces. complex peripherals.
SIM, RIM Instructions
Interrupt Mask Control
The 8085 provides special instructions to manage its interrupt system:
; Disable RST 7.5, enable RST 6.5 and 5.5MVI A, 08H ; Set bit 3, clear bits 0-2SIM
; Set interrupt mask
These instructions provide essential control over processor operation, interrupt handling, and bit manipulation. They're often used in critical sections of code,
power management, timing sequences, and logical operations.
Machine Control Example
Input Processing with Controlled Shutdown
; Read input from port 80H until a specific value (FFH) is received; Process each valid
input and output result to port 40H; Gracefully shut down the system when doneSTART: EI
; Enable interrupts for external events WAIT_LOOP: IN 80H ; Read from
input port CPI FFH ; Is it the termination value? JZ SHUTDOWN ; If yes, prepare
to shut down ; Process the input value CPI 80H ; Check if value > 80H JNC
LARGE ; Jump if value is large ; Process small values ADD A ; Double the
value JMP OUTPUT ; Go to output section LARGE: ANI 0FH ; Mask to get lower
nibble only OUTPUT: OUT 40H ; Output the processed value JMP WAIT_LOOP ;
Continue waiting for inputSHUTDOWN: DI ; Disable interrupts before halting
MVI A, 00H ; Clear accumulator OUT 40H ; Turn off all outputs HLT ;
Halt the processor
This pattern is common in embedded control systems where the processor must manage external devices and
respond to termination conditions.
Debugging and Common Errors
Assembler Errors
• Invalid mnemonics or operands
• Incorrect addressing modes
• Undefined labels or symbols
• Improper syntax in directives
Logic Errors
• Incorrect algorithm implementation
• Improper loop termination conditions
• Off-by-one errors in counting
• Stack overflow/underflow
Data Errors
• Improper initialization of registers/memory
• Overwriting critical data accidentally
• Using incorrect constants or immediate values
• Confusing hexadecimal and decimal values
Debugging assembly programs typically involves using simulators with features like register viewing, memory inspection, breakpoints, and step-by-
step execution to trace program flow and identify issues. Tools like GNUSim8085 provide these capabilities for 8085 assembly debugging.
Conclusion & Further Resources
Key Takeaways Recommended Resources
Throughout this presentation, we've explored the fundamental aspects of 8085 To continue your learning journey with 8085 assembly:
assembly language programming:
• Books: "Microprocessor Architecture, Programming and Applications with the 8085"
• Data Transfer Operations: MOV, MVI, LDA, STA, LDAX, STAX, IN, OUT by Ramesh Gaonkar
• Arithmetic Operations: ADD, SUB, INR, DCR, ADC, SBB, DAA • Simulators: GNUSim8085, 8085sim, Virtual 8085
• Logic Operations: ANA, ORA, XRA, CMP, RLC, RRC, RAL, RAR • Practice Projects:
• Branching Operations: JMP, conditional jumps, CALL, RET, RST • LED display controllers
• I/O and Machine Control: IN, OUT, HLT, NOP, DI, EI, SIM, RIM • Simple calculators
• Digital clock implementation
Understanding these operations provides a solid foundation for embedded systems
programming, hardware interfacing, and low-level software development. • Data sorting and searching algorithms