You are on page 1of 17

Assembly Language Programming

Lets Learn How to do It in 8051 !!

What is Assembly Language?


An Assembly Language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture.

This representation is usually defined by the hardware manufacturer, and is based on mnemonics [MOV, ADD, SUB, INC, CPL] that symbolize processing steps (instructions), processor registers, memory locations, and other language features.

Widely Used Register


A (Accumulator) Other Registers B, R0, R1, R2, R3, R4, R5, R6, R7 DPTR (data pointer), and PC (program counter)

8-Bit register/Memory/Port
The 8 bits of a register are shown from MSB D7 to the LSB D0

Basic Commands
Command ACALL Description Absolute Call

ADD
ANL CJNE CLR

Add Accumulator
Bitwise AND Compare and Jump if Not Equal Clear Register

CPL
DEC DJNZ INC JB JBC JC JMP

Complement Register
Decrement Register Decrement Register and Jump if Not Increment Register Jump if Bit Set Jump if Bit Set and Clear Bit Jump if Carry Set Jump to Address

Basic Commands | Contd..


Command JNB JNC Description Jump if Bit Not Set Jump if Carry Not Set

JNZ
JZ LCALL LJMP

Jump if Accumulator Not Zero


Jump if Accumulator Zero Long Call Long Jump

MOV
NOP ORL RET SETB SJMP SUBB

Move Memory
No Operation Bitwise OR Return From Subroutine Set Bit Short Jump Subtract From Accumulator With Borrow

Data Transfer Instruction


MOV A,#55H MOV R0,A MOV R1,A MOV R2,A MOV R3,#95H MOV A,R3 ;load value 55H into reg A ;copy contents of A into R0 (A=R0=55H) ;copy contents of A into R1 (A=R0=R1=55H) ;copy contents of A into R2 (A=R0=R1=R2=55H) ;load value 95H into R3 (R3=95H) ;copy contents of R3 into A (A=R3=95H)

DIY [Do it Yourself]: ORG 0H MOV A,#55H MOV R1,A; END

Program Status Word [PSW]

Arithmetic Instruction | Addition


ADD A, source ; ADD the source operand to the accumulator DIY [Do it Yourself] ORG 0H MOV A,#25H ;load 25H into A MOV R2,#34H ;load 34H into R2 ADD A,R2 ;add R2 to accumulator END Executing the program above results in A = 59H

Arithmetic Instruction | Addition | Contd..


MOV A, #38H ADD A, #2FH ;after the addition A=67H, CY=0

Solution:
38 +2F 67 00111000 00101111 01100111

CY = 0 since there is no carry beyond the D7 bit AC = 1 since there is a carry from the D3 to the D4 bi P = 1 since the accumulator has an odd number of 1s (it has five 1s)

Arithmetic Instruction | Subtraction


SUBB A, source ;Subtract the source operand to the accumulator DIY [Do it Yourself] ORG 0H CLR C MOV A,#3FH ;load 25H into A SUBB A,#23H ;add R2 to accumulator END Executing the program above results in A = 1CH

Arithmetic Instruction | Subtraction

Loop
Write a program to add value 3 to the Accumulator ten times using Loop DIY [Do it Yourself]
ORG 0H MOV A,#00H MOV R2,#10H AGAIN: ADD A,#03H DJNZ R2,AGAIN MOV R5,A END ; A=0, clear ACC ;load counter R2=10 ;add 03 to ACC ;repeat until R2=0,10 times ;save A in R5

NOTE: A loop can be repeated a maximum of 255 times, if R2 is FFH

Delay in assembly & I/O operation


Write a program to toggle Port 1 using delay DIY [Do it Yourself]
AGAIN: ORG 0H MOV A,#55H MOV P1,A ACALL DELAY CPL A SJMP AGAIN MOV R3,#200 DJNZ R3,HERE RET END

DELAY: HERE:

;---time delay-------

AND operation
Write a program to do AND operation between Accumulator & R1 DIY [Do it Yourself]
ORG 0H MOV A,#0FH MOV R1,#0F1H ANL A,R1 END

0FH 0F1H
01H AND Operation

00001111 11110001
00000001

OR operation
Write a program to do OR operation between Accumulator & R1 DIY [Do it Yourself]
ORG 0H MOV A,#0FH MOV R1,#0F1H ORL A,R1 END

OFH 0F1H
FFH OR Operation

00001111 11110001
11111111

NOT operation
Write a program to do NOT operation DIY [Do it Yourself]
ORG 0H MOV A,#0FH CPL A END

OFH F0H

00001111 11110000

NOT Operation

You might also like