Está en la página 1de 104

Assembly Language

Assembly Language

Addressing Modes Types of Instruction


1. 2. 3. 4. 5.

Arithmetic Logical Data Transfer Boolean Variables Program Branching

Addressing Modes

1. Immediate Addressing
Value to be stored in memory immediately follows the operation code in memory Or The instruction itself dictates the value to be stored in memory Example MOV A, #20h Accumulator will be loaded with the value that immediately follows; in this case 20h

1. Immediate Addressing

It is very fast Value to be loaded is included in instruction The value to be loaded is fixed at compile time its not very flexible

1. Immediate Addressing
Examples

MOV A,#25H MOV R4,#62H MOV B,#40H

COUNT EQU 30 MOV A,#COUNT MOV P1,#34


8

Important

MOV A, #FH

; gives error

MOV A, #0FH ; OKAY

2 . Direct Addressing

Value to be stored in memory is obtained by directly retrieving it from another memory location
Example MOV A, 30H

The above instruction will read data from RAM address 30H and put that data in A.

10

2 . Direct Addressing

Generally fast, although the value to be stored is not included in the instruction
Its quickly accessible since value is stored in 8051s Internal RAM Much more flexible than immediate addressing. Value to be loaded is whatever it is found at that location which may be variable Entire 128 bytes of RAM (0 to FF) can be accessed
11

2 . Direct Addressing
Examples

MOV R0, 40H MOV 56H, A

MOV R4, 7FH

12

3. Indirect Addressing

Also called indirect register addressing Very powerful and flexible addressing

Example
MOV A, @R0

This instruction analyzes the value of R0. A will be loaded with value from RAM whose address is found in R0 For example if R0 holds value 40H and RAM address 40h holds value of 67H. Then A will have value of 67H
13

3. Indirect Addressing

Only R0 and R1 can be used for this purpose


MOV A,@R0 MOV @R1, B Note: @ sign is compulsory in order to use Indirect addressing

14

3. Indirect Addressing

This mode makes data access dynamic instead of static (as in case of direct addressing)
Only R0 and R1 can be used Since these registers are 8 bit, use is restricted to internal RAM only

15

4. Register Addressing

In register addressing mode, we directly use the value stored in register 8051 has 8 working registers (of the currently active register bank) R0 to R7 Examples MOV A, R7 MOV A, R5 Contents of R7 and R5 will be moved in above instructions
16

4. Register Addressing

Easy to use register name instead of address


Source and destination registers should match in size Move between A and registers is allowed MOV A, R4 ; OKAY Move between registers is not allowed MOV R7, R4 ; gives error

17

5. Relative Addressing mode

Used with certain jump instructions


8 bit signed offset is used, range for jumping is -128 to 127 location SJMP [label]

18

6. Absolute Addressing

Used only with ACALL and AJMP instruction It allows branching within 2K limit (11 bit)

19

7. Long Addressing

Used only with LCALL and LJMP instruction Allows branching within 64K limit (16 bit)

20

8. Index Addressing

Used with JMP and MOVC instruction Use base register (either PC or DPTR) and an offset (the accumulator) in forming the effective address
MOVC A, @A + DPTR JMP @A + DPTR It is used for look up table

Previously we studied how its stored Well cover its usage later on

21

Instruction Types

22

Types of Instructions
1. 2. 3. 4. 5.

Arithmetic Logical Data Transfer Boolean Variable Manipulation Program Branching

23

MOV Summary

MOV [Destination],[Source]

Examples

MOV A,#55H ; A will be loaded with 55H MOV R2, A ; Load value of A in R2

24

1. Arithmetic Instructions

Addition Subtraction Multiplication Division Increment Decrement

25

1. Arithmetic Instructions - Addition


ADD Addition

Add A, source Add A, #data

; Add source to A ; Add data to A

Destination operand is always register A

Example

26

mov a,#5 mov b,#10 Add a,b

; Set a = 5 ; Set b = 10 ; what will be contents of a?

1. Arithmetic Instructions - Addition


ADDC Addition with carry Used in case of adding 16 bit numbers

High byte and Low Byte are added separately using 2 instructions If there is a Carry out from addition of low bytes, it has to be added in sum of high bytes

27

1. Arithmetic Instructions - Addition


ADDC Addition with carry Example
Write a program to add two 16 bit numbers 2CE7H and 558DH

28

1. Arithmetic Instructions - Addition


ADDC Write a program to add two 16 bit numbers 2CE7H and 558DH CLR C ;clear carry flag. CY = 0 ; Load lower byte of 1st number ; Add lower byte of 2nd number ; now A = 74H and CY = 1; ; Move lower byte of sum in R0 ; Load higher byte of 1st number ; Add with carry ; 2C + 55 + 1 (carry) = 82 H ; Move higher byte of sum in R1

MOV A, #0E7H ADD A, #8DH


MOV R0,A

MOV A, #2CH ADDC A,#55H


MOV R1, A
29

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow Subtraction with borrow Uses carry flag to read if there is any borrow or not Format SUBB A, Source ; A = A source CY

Before the subtraction, Carry flag means Borrow


If carry = 0, there is no carry If carry = 1, there is a borrow

Note: There is no subtraction instruction without borrow in 8051 (many microprocessors have it)
30

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow

After the operation


Carry = 0 if result is POSITIVE Carry = 1 if result is NEGATIVE

Negative result is stored in format of 2s complement

31

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow Example
clr C mov a,#25h mov r1,#10h subb a,r1

What will be value of A?


32

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow Example

clr C mov a,#25h mov r1,#10h subb a,r1

; set carry flag = 0 ; A = 25h ; r1 = 10h ; a = a r1 = 25 10 = 15

What will be value of A?

15h Carry = 0

33

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow Example
setb C mov a,#25h mov r1,#10h subb a,r1

What will be value of A?


34

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow Example

setb C mov a,#25h mov r1,#10h subb a,r1

; set carry flag = 1 ; A = 25h ; r1 = 10h ; a = a r1 1 = 25 10 1 = 14

What will be value of A?

14h Carry = 0

35

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow Example
clr C mov a,#10h mov r1,#20h subb a,r1

What will be value of A?


36

1. Arithmetic Instructions - Subtraction


SUBB SUBtract with Borrow Example

clr C mov a,#10h mov r1,#20h subb a,r1

; set carry flag = 0 ; A = 10h ; r1 = 20h ;a = F0h

What will be value of A?


F0h = 1111 0000. Taking 2s complement (by inverting and adding 1) = 0000 1111 + 1 = 0001 0000 = 10h Carry = 1. Showing it is a negative number

37

1. Arithmetic Instructions - Multiplication


MUL Multiplication Format
MUL AB

Multiplies A and B Operands must be in A and B Result


A has Low byte B has high byte

38

1. Arithmetic Instructions - Multiplication


MUL Multiplication Example
mov a,#16 mov b,#4 mul ab What will be value of A and B?

39

1. Arithmetic Instructions - Multiplication


MUL Multiplication Example
mov a,#16 mov b,#4 mul ab What will be value of A and B?

A = 40h = 64 B=0

40

1. Arithmetic Instructions - Division


DIV Division Format
DIV AB

Divide A by B Operands must be in A and B Result


A has Quotient B has Remainder

41

1. Arithmetic Instructions - Division


DIV Example
mov a,#16 mov b,#4 div ab What will be value of A and B?

42

1. Arithmetic Instructions - Division


DIV Example
mov a,#16 mov b,#4 div ab What will be value of A and B?

A = 4h B=0

43

1. Arithmetic Instructions - Increment


INC Increment Format INC source

Adds 1 to source Example

mov r1,#55h inc r1 R1 = 56h

44

1. Arithmetic Instructions - Decrement


DEC Decrement Format

DEC source Subtracts 1 from source


Example

mov r1,#55h dec r1 R1 = 54h

45

1. Arithmetic Instructions BCD Numbers


BCD Numbers Binary Coded Decimal Unpacked BCD

Lower 4 Bits represent decimal number (0 to 9) Remaining bits are zero Example: 9 will be represented as 0000 1001 1 byte can represent 1 number only

Packed BCD

46

Each 4 bits represent a decimal number (0 to 9) 1 byte can store more 2 digits Example: 79 will be represented as 0111 1001 Utilizes space more efficiently than unpacked BCD

1. Arithmetic Instructions BCD Numbers


BCD Numbers Binary Coded Decimal Addition of BCD numbers Consider addition of following BCD numbers mov A,#59h add A,#1h

Result is 5Ah Is it a BCD? NO The answer should have been 60 Solution?


47

1. Arithmetic Instructions BCD Numbers


DAA Decimal Adjust mov A,#59h add A,#1h DAA A Result is 60h Is it a BCD? Yes Auxiliary Carry (from 4th to 5th bit) is used to check this condition
48

1. Arithmetic Instructions
Important

Play with arithmetic instructions and see the results Good to revise Binary Arithmetic first

49

2. Logical Instructions

AND OR Exclusive OR Clear Complement Rotate


Left Right

SWAP
50

2. Logical Instructions - AND


AND Format
ANL destination, source

Result destination = destination AND source

The destination is normally Accumulator

51

2. Logical Instructions - AND


AND Example
mov a,#74h anl a,#0Fh

What will be value of A?

52

2. Logical Instructions - AND


AND Example
mov a,#74h anl a,#0Fh

What will be value of A? A = 04h

53

2. Logical Instructions - OR
OR Format
ORL destination, source

Result destination = destination OR source

The destination is normally Accumulator

54

2. Logical Instructions - OR
OR Example
mov a,#74h orl a,#0Fh

What will be value of A?

55

2. Logical Instructions - OR
OR Example
mov a,#74h orl a,#0Fh

What will be value of A? A = 7Fh

56

2. Logical Instructions - XOR


XOR (Exclusive OR) Format XRL destination, source Result
destination = destination XOR source

The destination is normally Accumulator

57

2. Logical Instructions - XOR


XOR Example
mov a,#74h xrl a,#0Fh

What will be value of A?

58

2. Logical Instructions - XOR


XOR Example
mov a,#74h xrl a,#0Fh

What will be value of A? A = 7bh

59

2. Logical Instructions - CLEAR


CLEAR Format
CLR operand

Result operand = 0

The operand may be Accumulator, carry flag (C), any IO Port Bit etc

60

2. Logical Instructions - CLEAR


CLEAR Example
mov a,#74h clr a

What will be value of A? A=0

61

2. Logical Instructions - COMPLEMENT


CPL Format
CPL operand

Result All bits of operand inverted

The operand may be Accumulator, carry flag (C), any IO Port Bit etc

62

2. Logical Instructions - COMPLEMENT


CPL Example
mov a,#74h cpl a

What will be value of A?

63

2. Logical Instructions - COMPLEMENT


CPL Example
mov a,#74h cpl a

What will be value of A? A = 8Bh

64

2. Logical Instructions Rotate Left


RL (Rotate Left) Format
RL A

Result RL: All bits of A shifted to left

LSB is replaced by MSB


MSB LSB

65

2. Logical Instructions Rotate Left


RLC (Rotate Left through Carry) Format RLC A Result RL: All bits of A shifted to left

Carry bit is included in rotation


CY MSB LSB

66

2. Logical Instructions Rotate Right


RR (Rotate Right) Format
RR A

Result All bits of A shifted to right

MSB is replaced by LSB


MSB LSB

67

2. Logical Instructions Rotate Right


RRC (Rotate Right through Carry) Format RRC A Result RL: All bits of A shifted to left

Carry flag is included in rotation


MSB LSB CY

68

2. Logical Instructions Rotate


Use of Rotate Used in bit manipulation

Rotate Left used as Multiplication Rotate Right used as Division

Less memory required Much faster

69

2. Logical Instructions SWAP


SWAP Format
SWAP A

Result Swaps lower and higher nibbles

Works only with accumulator

70

2. Logical Instructions SWAP


SWAP Example
mov a,#74h swap a

What will be value of A?

71

2. Logical Instructions SWAP


SWAP Example
mov a,#74h swap a

What will be value of A? A = 47h

72

3. Data Transfer

MOV

Addressing Modes

(Already Covered)

MOVC XCH PUSH POP

73

3. Data Transfer MOVC


MOVC Used to read data (look-up tables etc) stored in ROM

How to save data in ROM?

Dbit, DB, DW etc

Format MOVC destination, source Destination is mostly A Source is in ROM


74

3. Data Transfer MOVC


MOVC Example
Write a program which reads a number from port1 and sends its square to p2 continuously

75

3. Data Transfer MOVC


MOVC Write a program which reads a number from port1 and sends its square to p2 continuously ORG 00h mov dptr,#300h Label: mov a,p1 movc a, @a + dptr mov p2,a jmp Label

; Index Addressing Mode

ORG 300h sq: db 0,1,4,9,16,25,36,49,64,81,100 end


76

3. Data Transfer XCH


XCH Exchanges the contents of operands Format XCH A, source
Example Mov a,#15h Mov r1,#0FEh XCH a,r1

; A = 15h ; R1 = FEh ; swaps data of A and R1 ; A = FEh and R1 = 15h

77

3. Data Transfer PUSH


PUSH Used to Push or store data onto stack

Stack Pointer (SP) stores the address of current stack location. At power up, SP = 07

First number will be stored at 08 and so on

When data is stored (using PUSH) command, SP is incremented automatically


78

3. Data Transfer PUSH


PUSH

RAM locations 08h to 1Fh can be used for stack RAM locations 20h to 2Fh are bit-addressable locations More space if SP is set to point to 30h to 7Fh i.e. general purpose RAM
79

3. Data Transfer PUSH


PUSH Format
PUSH expression

expression must be

Number of a register

1 for R1 2 for R2 and so on

Address Symbol referring to an address

Note: Accumulator cannot be used with PUSH statement


80

3. Data Transfer PUSH


PUSH Format
PUSH expression

Example ; Push contents of R3 ; Push contents of R1 ; Push value stored at memory location 55h

Push 3 Push 1 Push 55h

81

3. Data Transfer POP


POP Format POP expression

expression must be

Number of a register

1 for R1 2 for R2 and so on

Address Symbol referring to an address

Retrieves the value the last value stored by PUSH Automatically decrements the SP
82

3. Data Transfer POP


POP Example mov r7,#85h mov 80h,#5h

PUSH 7 PUSH 80h


POP 1 POP 75h mov a,75h What is this program doing?
83

3. Data Transfer POP


POP Example mov r7,#85h mov 80h,#5h ; R7 = 85h ; Store 5h at memory location 80h

PUSH 7 PUSH 80h


POP 1 POP 75h

; Store R7 using PUSH ; Store contents of location 80h


; Retrieve last pushed value in R1. R1 = 5h ; Retrieve the current value in stack and save ; it in memory location 75h ; A = contents of location 75h. A = 85h

mov a,75h
84

4. Boolean Variable Manipulation

CLR

CLR bit Clears the bit i.e. makes the bit = 0 Examples

CLR C CLR P0^3

SETB

SETB bit Sets the bit i.e. makes the bit = 1 Examples

SETB C SETB P2^0

85

4. Boolean Variable Manipulation

ANL

ANL bit1,bit2 Takes logical AND of both bits Example

anl c,P1^7

ORL

ORL bit1,bit2 Takes logical OR of both bits Example

orl c,P3^5

CPL

CPL bit Inverts the value of bit Example

CPL P0^0

86

5. Program Branching

CALL and RET Unconditional


AJMP SJMP LJMP

Conditional

87

CJNE DJNZ JZ JNZ JB JNB JC JNC

5. Program Branching CALL and RET


CALL and RET Used to Call Subroutines (SR)

Format CALL name ; Calling Subroutine Return from SR RET ;

If SR is Interrupt Subroutine (ISR), return is:

RETI
88

; Return from interrupt SR

5. Program Branching CALL and RET


CALL and RET Example ORG 00h Call fn1 Call fn2 Jmp $ fn1: . . RET RET

; Calling Subroutine fn1 ; Calling Subroutine fn2 ; Infinite Loop

fn2:

What if infinite loop is omitted?

89

5. Program Branching CALL and RET


CALL and RET Important

When SR is being executed, PSW can be changed

Carry Flag Auxiliary Carry Overflow etc

All required registers are stored before calling SR and retrieved after the SR has finished

90

PSW A B R0 R7

5. Program Branching CALL and RET


CALL and RET Important Saving Required registers
1. 2.

PUSH them in stack before calling SR POP them from stack after returning from SR

91

5. Program Branching Unconditional Jumps


SJMP Can jump 128 to 127 (8 bit signed) from current address SJMP REL; REL is 8 bit offset AJMP Can jump to any address represented by 11 bits AJMP Addr11 LJMP Can jump to any address represented by 16 bits AJMP Addr16 JMP We can write JMP and assembler will convert it to appropriate jump
92

5. Program Branching Unconditional Jumps


SJMP, AJMP, LJMP Example
Org 00h Main: mov a,#23h mov a,#25h mov a, #27h jmp main ; Automatically decides the appropriate ; Jmp end
93

5. Program Branching Conditional Jumps


CJNE Compare and Jump if NOT Equal CJNE A, expression, Label DJNZ Decrement and Jump if NOT Zero DJNZ, register, Label JZ Jump if Accumulator is Zero JZ Label JNZ Jump is Accumulator is NOT Zero JNZ Label
94

5. Program Branching Conditional Jumps


JB Jump if Bit is set JB bit, Label JNB Jump if Bit is Not set
JNB bit, label JC Jump if Carry is set JC Label JNC Jump if Carry NOT set JNC Label
95

5. Program Branching Conditional Jumps


CJNE Compare and Jump if NOT Equal CJNE expression1, expression2, Label expression1 may be

A Register (R0 R7)

expression2 may be

Number Address

96

5. Program Branching Conditional Jumps


CJNE Example

mov a,#01h

main:
Jmp $

INC a CJNE A, #09h, main

What is this program doing?


97

5. Program Branching Conditional Jumps


DJNZ Decrement and Jump if Not Zero DJNZ register, Label Register may be any register of the active bank

R0 to R7

First it decrements the value of register and then checks if the value is zero or not

98

5. Program Branching Conditional Jumps


DJNZ Example
mov r7,#05h main:

DJNZ r7, main


jmp $ What is this program doing?
99

5. Program Branching Conditional Jumps


JZ

Jump if accumulator is Zero Format JZ expression expression may be

Label 8 bit signed offset address

100

5. Program Branching Conditional Jumps


JZ

Example

mov a,#1h main: jmp $ DEC a JZ main

What is this program doing?


101

5. Program Branching Conditional Jumps


JNZ Jump if accumulator is Not Zero JNZ expression

Expression may be

Label 8 bit signed offset address

102

5. Program Branching Conditional Jumps


JNZ Example mov a,#07h
main: DEC a JNZ main

jmp $

What is this program doing?


103

5. Program Branching Conditional Jumps


JB Jump if Bit is set
JB bit, Label

Example

Mov a,#0Fh Label: DEC A mov p0, a JB P0^3, Label What is this program doing?
104

5. Program Branching Conditional Jumps


JNB Jump if Bit is NOT set
JNB bit, Label

Example

Mov a,#01h Label: INC A mov P0,a JNB P0^3, Label What is this program doing?
105

5. Program Branching Conditional Jumps


JC Jump if Carry is set
JC Label JNC Jump if Carry Not set

JNC Label

106

También podría gustarte