Assembly language programming — Unit 3 Notes (Microprocessors and Microcontrollers)

BCS605 · Unit 3

Assembly language programming notes — Unit 3

Free unit-wise study notes on assembly language programming for Microprocessors and Microcontrollers, Semester 6 of B.Tech — Computer Science & Engineering — key concepts, examples, important questions and a revision checklist for semester exams.

Assembly language programming

Notebook — 14 pages

Page 1

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

1. Introduction to Assembly Language

Microprocessors only understand Machine Language (binary 0s and 1s). Writing programs directly in binary is incredibly error-prone and tedious for humans. Assembly Language is a low-level programming language that uses alphanumeric mnemonics (like ADD, SUB, MOV) to represent machine-level instructions.

1.1 The Assembler

The processor cannot directly execute assembly language. A software program called an 'Assembler' translates the human-readable assembly code into the binary machine code that the processor can execute.

Next — Structure of a Program

1 of 14

Page 2

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

2. Structure of an Assembly Program

An assembly language program consists of a sequence of statements. Each statement has a specific format, typically divided into four fields.

2.1 The Four Fields

  • Label: An optional string that marks a specific memory address. Used as a target for JUMP and CALL instructions (e.g., `START:`, `LOOP:`).
  • Mnemonic (Opcode): The actual instruction command (e.g., `MOV`, `ADD`).
  • Operand: The data or registers the instruction operates on (e.g., `A, B`, `2000H`).
  • Comment: Optional text meant for humans, ignored by the assembler. Usually preceded by a semicolon (`;`).

Next — Assembler Directives

2 of 14

Page 3

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

3. Assembler Directives (Pseudo-Instructions)

Directives are instructions given to the Assembler itself, not to the microprocessor. They do not generate any machine code.

3.1 Common Directives

  • ORG (Origin): Tells the assembler where to put the next block of code in memory. (e.g., `ORG 2000H` means the program starts at address 2000H).
  • EQU (Equate): Defines a constant. (e.g., `PORT1 EQU 80H`). Whenever the assembler sees PORT1, it replaces it with 80H. Makes code readable.
  • DB (Define Byte): Reserves 8-bit memory locations and initializes them with data (e.g., for arrays).
  • END: Tells the assembler this is the physical end of the source code file.

Next — Programming: Addition

3 of 14

Page 4

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

4. Program: 8-bit Addition (8085)

Problem: Add two 8-bit numbers stored at memory locations 2000H and 2001H. Store the result at 2002H.

```asm
LXI H, 2000H ; Point HL register pair to 2000H
MOV A, M ; Move data from 2000H into Accumulator
INX H ; Increment HL to point to 2001H
ADD M ; Add data at 2001H to Accumulator
INX H ; Increment HL to point to 2002H
MOV M, A ; Store the result in Accumulator to 2002H
HLT ; Stop execution
```

Next — Handling Carries

4 of 14

Page 5

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

5. Program: Addition with Carry (8085)

If we add FFH + 02H, the result is 101H. The accumulator is only 8 bits, so it will hold 01H, and the Carry Flag will be set. The previous program would lose the '1'. We must account for the carry.

```asm
LXI H, 2000H ; Point to data
MVI C, 00H ; Clear Register C to hold the carry
MOV A, M ; Get first number
INX H
ADD M ; Add second number
JNC SKIP ; If no carry, jump to SKIP
INR C ; If there IS a carry, increment C to 01
SKIP: INX H
MOV M, A ; Store the 8-bit result
INX H
MOV M, C ; Store the carry on the next memory location
HLT
```

Next — Programming: Block Transfer

5 of 14

Page 6

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

6. Program: Block Transfer (8085)

Problem: Move 10 bytes of data starting from 2000H to a new location starting at 3000H.

```asm
LXI H, 2000H ; Source pointer
LXI D, 3000H ; Destination pointer
MVI C, 0AH ; Counter = 10 (0A in hex)

LOOP: MOV A, M ; Get byte from source
STAX D ; Store byte at destination (using DE pair)
INX H ; Increment source pointer
INX D ; Increment destination pointer
DCR C ; Decrement counter
JNZ LOOP ; If counter is not zero, go back to LOOP
HLT
```

Next — Time Delays

6 of 14

Page 7

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

7. Generating Time Delays

Microprocessors execute instructions extremely fast. If you want to blink an LED once per second, you cannot just turn it on and off sequentially. You must force the processor to wait. This is done by writing a 'Delay Loop' that wastes clock cycles.

7.1 Calculation

If a processor runs at 2 MHz, one clock state (T-State) takes `1 / 2,000,000 = 0.5` microseconds. If a loop takes 14 T-States to execute, and you run the loop 255 times, the total delay is `14 255 0.5 = 1785` microseconds (1.78 ms).

Next — Writing a Delay Subroutine

7 of 14

Page 8

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

8. Writing a Delay Subroutine

To get a large delay (like 1 second), a single 8-bit register looping 255 times is not enough. We must use a 16-bit register pair (loops up to 65,535 times) or nested loops.

```asm
DELAY: LXI B, FFFFH ; Load 16-bit counter
LOOP: DCX B ; Decrement pair BC (6 T-states)
MOV A, B ; (4 T-states)
ORA C ; Logical OR checks if both B and C are zero (4 T-states)
JNZ LOOP ; Jump if not zero (10 T-states)
RET ; Return from subroutine
```

The total T-states for the loop = `6 + 4 + 4 + 10 = 24`. 24 65535 0.5µs = ~0.78 seconds of delay.

Next — 8086 Programming Basics

8 of 14

Page 9

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

9. 8086 Programming: Segment Directives

Because the 8086 uses memory segmentation, 8086 assembly programs are structured differently. You must explicitly define your Data Segment, Code Segment, and Stack Segment using directives.

```asm
DATA SEGMENT
NUM1 DB 25H ; Define byte 25H
NUM2 DB 10H
RESULT DB ? ; Reserve uninitialized byte
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA ; Initialize the Data Segment Register
MOV DS, AX
; ... program logic ...
CODE ENDS
END START
```

Next — 16-bit Addition 8086

9 of 14

Page 10

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

10. Program: 16-bit Addition (8086)

Unlike the 8085 which requires two 8-bit additions to add 16-bit numbers, the 8086 can do it in a single instruction.

```asm
MOV AX, 1234H ; Load 16-bit data directly into AX
MOV BX, 5678H ; Load 16-bit data into BX
ADD AX, BX ; Add BX to AX. Result is in AX (68ACH)
```

10.1 32-bit Addition (8086)

To add 32-bit numbers, we use the `ADC` (Add with Carry) instruction.

```asm
ADD AX, CX ; Add lower 16 bits
ADC BX, DX ; Add upper 16 bits AND the carry from the lower addition
```

Next — String Instructions

10 of 14

Page 11

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

11. 8086 String Operations

The 8086 has dedicated hardware instructions for moving or comparing entire blocks of memory (strings), making the block transfer program incredibly short.

11.1 Key Registers

  • SI (Source Index): Always points to the source data in the Data Segment.
  • DI (Destination Index): Always points to the destination in the Extra Segment.
  • CX (Count): Holds the number of bytes to transfer.

Next — String Block Transfer

11 of 14

Page 12

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

12. Program: 8086 Block Transfer

Transfer 100 bytes from `SOURCE` to `DESTINATION`.

```asm
LEA SI, SOURCE ; Load Effective Address of source into SI
LEA DI, DESTINATION ; Load destination address into DI
MOV CX, 100 ; Set counter to 100
CLD ; Clear Direction Flag (Auto-increment SI/DI)

REP MOVSB ; REPeat MOVe String Byte
```

The single `REP MOVSB` instruction tells the processor to move the byte from `[SI]` to `[DI]`, increment SI and DI, decrement CX, and repeat this loop entirely in hardware until CX reaches 0.

Next — Multiplication and Division

12 of 14

Page 13

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

13. 8086 Multiplication and Division

13.1 MUL Instruction

When multiplying two 16-bit numbers, the result can be 32 bits. The 8086 automatically handles this by splitting the result across two registers.

`MUL BX` means: Multiply the contents of `AX` by `BX`. Store the upper 16 bits of the result in `DX`, and the lower 16 bits in `AX`.

13.2 DIV Instruction

When dividing a 32-bit number (stored across DX and AX) by a 16-bit register (like BX), `DIV BX` performs the division. The 16-bit Quotient is stored in `AX`, and the 16-bit Remainder is stored in `DX`.

Next — Subroutines and the Stack

13 of 14

Page 14

Wink Notes

B.Tech CSE — 6th Semester

Microprocessors and Microcontrollers

Unit - 3

14. 8086 Stack Operations

The Stack is a Last-In, First-Out (LIFO) memory structure crucial for subroutines and interrupts.

14.1 PUSH and POP

`PUSH AX` decrements the Stack Pointer (SP) by 2, and stores the 16-bit contents of AX at that memory location.

`POP BX` retrieves the 16-bit data from the top of the stack into BX, and increments the SP by 2.

14.2 Passing Parameters

High-level languages (like C++) use the stack to pass arguments to functions. The main program PUSHes the arguments onto the stack, calls the subroutine, and the subroutine uses the Base Pointer (BP) to read the arguments off the stack without POPping them.

14 of 14

Continue in this subject