+ All Categories
Home > Documents > Lect5 Assembler

Lect5 Assembler

Date post: 09-Apr-2018
Category:
Upload: bharani-rajkumar
View: 236 times
Download: 0 times
Share this document with a friend

of 56

Transcript
  • 8/8/2019 Lect5 Assembler

    1/56

    Assembler language 8051

    Mairtin O Conghaile

  • 8/8/2019 Lect5 Assembler

    2/56

    2

    8051 Microcontroller

    Complete, highly-integrated microcomputer CPU, RAM, ROM, IO

    Port 0

    8-bit bidirectional I/O port OR multiplexed low-order address and data bus bytes

    Port 1 8-bit bidirectional I/O port

    Port 2

    8-bit bidirectional I/O port OR high-order address byte Port 3

    8-bit bidirectional I/O port OR various special-function signals

  • 8/8/2019 Lect5 Assembler

    3/56

    3

    8051 Microcontroller

    Interface Signals (1)P O / A D [ 7 - 0 ]

    P 1

    P 2 / A [ 1 5 - 8 ]

    P 3 . 2 / I N T 0 *P 3 . 3 / I N T 1 *

    P 3 . 4 / T 0P 3 . 5 / T 1

    P 3 . 6 / W R *P 3 . 7 / R D *

    W r i t eR e a d

    T i m e r 1T i m e r 0

    I n t e r r u p t 1I n t e r r u p t 0

    P o r t 0 o r L o A d d r / D a t a

    P o r t 1

    P o r t 2 o r H i A d d r

    P o r t 3 / S p e c i a l F u n c t i o n s :

    8

    8

    8

    P 3 . 0 / R X DP 3 . 1 / T X D

    S e r i a l I n p u t P o r tS e r i a l O u t p u t P o r t

    P S E NA L E / P R O G *A d d r L a t c h E n a / P r o g r a m

    P r o g r a m S t o r e E n a b l e

    [ B u s T i m i n g ]

    [ I n t ]

    [ I / O ]

  • 8/8/2019 Lect5 Assembler

    4/56

    4

    8051 Microcontroller

    Interface Signals (2)E A * / V P P

    X T A L 2 *

    X T A L 1 *

    E x t e r n a l A c c e s s E n a b l e /

    P r o g r a m m i n g V o l t a g e

    C r y s t a l / o s c i l l a t o r

    C r y s t a l

    R e s e t R S T

    C l o c k / R e s e t

    G e n e r a t o r

    ( E x t e r n a l )

  • 8/8/2019 Lect5 Assembler

    5/56

    Introduction to the 8051

    microcontroller Type of memory

    Code memory (64k max)

    Internal RAM (128 bytes) External RAM (64k max) Special Function Registers (SFR) Bit memory

  • 8/8/2019 Lect5 Assembler

    6/56

    Internal RAM

  • 8/8/2019 Lect5 Assembler

    7/56

    Basic Registers Accumulator R registers B register Data Pointer (DPTR) 16-bit register Program Counter (PC) 16-bit register

    Stack Pointer (SP)

  • 8/8/2019 Lect5 Assembler

    8/56

    MOV instruction Simply stated, the MOV instruction copies

    data from one location to another. It has the

    following formatMOV destination, source This instruction tells the CPU to move (copy) the source

    operand to the destination operand, without changingthe content of the source operand.

    Examples: MOV A,#55h ; load 55h into register A MOV R0,A ; copy contents of A into R0 MOV R3, #95h ; load value 95h into R3 MOV A,R3 ;copy content of R3 into A

  • 8/8/2019 Lect5 Assembler

    9/56

    Some useful pointers Values can be loaded directly into any of the registers

    A,B or R0-R7. However, to indicate that it is animmediate value it must be preceeded with a # sign. MOV A,#23H;load 23H into A

    MOV R6,#12 ;load 12d into R6 MOV R5,#0f9h

    If value is small, rest of bits are assumed to be allzeros. E.g. mov a 4-bit value into an 8-bit register

    Moving a value that is too large into a register will

    cause an error. To load a value into a register it must be preceeded

    with a # sign. Otherwise it means to load from amemory location. MOV A,#17h MOV A,17h

  • 8/8/2019 Lect5 Assembler

    10/56

    ADD instruction The ADD instruction has the following

    format:

    ADD A, source ;Add the source operandto A

    This tells the CPU to add the source

    byte to reg A and put the result inreg A

  • 8/8/2019 Lect5 Assembler

    11/56

    Example Calculate the content of the

    accumulator after the program is

    executed on the 8051. MOV R5,#25h MOV R7,#34H

    MOV A,#0 ADD A,R5 ADD A,R7

  • 8/8/2019 Lect5 Assembler

    12/56

    Addressing Modes An "addressing mode" refers to how you are

    addressing a given memory location. In summary, the addressing modes are as

    follows, with an example of each: Immediate Addressing MOV A,#20h Direct Addressing MOV A,30h Indirect Addressing MOV A,@R0

    External Direct MOVX A,@DPTR Code Indirect MOVC A,@A+DPTR

    Each of these addressing modes providesimportant flexibility.

  • 8/8/2019 Lect5 Assembler

    13/56

    Immediate Addressing Immediate addressing is so-named because the value to be

    stored in memory immediately follows the operation code inmemory. That is to say, the instruction itself dictates whatvalue will be stored in memory.

    MOV A,#20h This instruction uses Immediate Addressing because the

    Accumulator will be loaded with the value that immediatelyfollows; in this case 20 (hexidecimal).

    Immediate addressing is very fast since the value to be loadedis included in the instruction. However, since the value to be

    loaded is fixed at compile-time it is not very flexible.

  • 8/8/2019 Lect5 Assembler

    14/56

    Direct Addressing Direct addressing is so-named because the value to be stored in

    memory is obtained by directly retrieving it from another memorylocation. For example:MOV A,30h

    This instruction will read the data out of Internal RAM address 30(hexidecimal) and store it in the Accumulator.

    Direct addressing is generally fast since, although the value to beloaded isnt included in the instruction, it is quickly accessablesince it is stored in the 8051s Internal RAM.

    It is also much more flexible than Immediate Addressing since thevalue to be loaded is whatever is found at the given address--which may be variable.

    Also, it is important to note that when using direct addressing anyinstruction which refers to an address between 00h and 7Fh isreferring to Internal Memory. Any instruction which refers to anaddress between 80h and FFh is referring to the SFR controlregisters that control the 8051 microcontroller itself.

  • 8/8/2019 Lect5 Assembler

    15/56

    Indirect Addressing Indirect addressing is a very powerful addressing mode which

    in many cases provides an exceptional level of flexibility.MOV A,@R0

    This instruction causes the 8051 to analyze the value of the

    R0 register. The 8051 will then load the accumulator with thevalue from Internal RAM which is found at the addressindicated by R0.

    For example, lets say R0 holds the value 40h and InternalRAM address 40h holds the value 67h. When the aboveinstruction is executed the 8051 will check the value of R0.Since R0 holds 40h the 8051 will get the value out of Internal

    RAM address 40h (which holds 67h) and store it in theAccumulator. Thus, the Accumulator ends up holding 67h.

  • 8/8/2019 Lect5 Assembler

    16/56

    Indirect

    Addressing(contd) Indirect addressing always refers to Internal RAM; it never

    refers to an SFR. Thus, in a prior example we mentionedthat SFR 99h can be used to write a value to the serial port.Thus one may think that the following would be a validsolution to write the value 1 to the serial port:

    MOV R0,#99h ;Load the address of the serial portMOV @R0,#01h ;Send 01 to the serial port -WRONG!!

    On an 8051 these two instructions would produce anundefined result since the 8051 only has 128 bytes ofInternal RAM.

  • 8/8/2019 Lect5 Assembler

    17/56

    External Direct

    Addressing External Memory is accessed using "External Direct"

    addressing. There are only two commands that use External Direct

    addressing mode:

    MOVX A,@DPTRMOVX @DPTR,A

    As you can see, both commands utilize DPTR. In these instructions, DPTR must first be loaded with the

    address of external memory that you wish to read orwrite. Once DPTR holds the correct external memory

    address, the first command will move the contents ofthat external memory address into the Accumulator.

    The second command will do the opposite: it will allowyou to write the value of the Accumulator to theexternal memory address pointed to by DPTR.

  • 8/8/2019 Lect5 Assembler

    18/56

    External Indirect

    Addressing External memory can also be accessed using a

    form of indirect addressing. This form of addressing is usually only used in

    relatively small projects that have a very smallamount of external RAM. An example of thisaddressing mode is:MOVX @R0,A

    Once again, the value of R0 is first read and

    the value of the Accumulator is written to thataddress in External RAM. Since the value of@R0 can only be 00h through FFh the projectwould effectively be limited to 256 bytes ofExternal RAM.

  • 8/8/2019 Lect5 Assembler

    19/56

    Question 1 Write a short program for the 8051

    microcontroller that carries out the followinginstructions:

    Loads the accumulator with the value 40h Loads R7 with 12d Copies the content of R7 to address 30h directly Loads the register R0 with 30h Indirectly moves the contents of 30h to reg B

    Indicate clearly the contents, at each stage,of all memory locations and registersinvolved.

  • 8/8/2019 Lect5 Assembler

    20/56

    Structure of Assembly

    language Consists of a series of assembly

    language instructions. Instruction consists of four fields:[label1:] mnemonic [operands] [;comment] Label field allows the program to refer to a

    line by name. Comment field must begin with a

    semicolon

  • 8/8/2019 Lect5 Assembler

    21/56

    Example of Assembly

    ProgramORG 0H ;start at mem loc 0MOV R5,#25h ;load 25h in r5MOV R7,#34H ;load 34h into r7

    MOV A,#0 ;clear AADD A,R5 ;A=A+R5ADD A,R7 ;A=A+R7ADD A,#12h ;A=A+12

    HERE: SJMP HERE ;stay in this loopEND ;end of asm source ;file

  • 8/8/2019 Lect5 Assembler

    22/56

    8051 Data Types and

    Directives Only one type of data type 8-bit

    Its the job of the programmer to

    break down data larger than 8-bits.

  • 8/8/2019 Lect5 Assembler

    23/56

    DB The DB directive is the most widely used data directive in

    assembler. Used to define the 8-bit data. When DB is used, the numbers can be decimal, binary,

    hex or ASCII formats. The only directive that can be used to define ASCII strings

    larger than two characters.

    ORG 500HDATA1: DB 39HDATA2: DB 2591 ;ASCII NUMBERS

    ORG 518HDATA3: DB Computer Engineering

  • 8/8/2019 Lect5 Assembler

    24/56

    Assembler Directives ORG: Used to indicate the

    beginning of the address. EQU: Used to define a constant

    without occupying amemory location.

    e.g. Count EQU 25

    END: Indicates the end of thesource file.

  • 8/8/2019 Lect5 Assembler

    25/56

    PSW (Program StatusWord)

    Addresses D0h, Bit-Addressable The Program Status Word is used to store a

    number of important bits that are set and clearedby 8051 instructions. The PSW SFR contains the carry flag, the auxiliary

    carry flag, the overflow flag, and the parity flag. Additionally, the PSW register contains the

    register bank select flags which are used to selectwhich of the "R" register banks are currently

    selected.

  • 8/8/2019 Lect5 Assembler

    26/56

    Example

    State the contents of the RAMlocations after the following program:

    SETB PSW.4MOV R0,#99H

    MOV R1,#85H

    MOV R2,#99H

    MOV R7,#85H

    MOV R5,#99H

  • 8/8/2019 Lect5 Assembler

    27/56

    Solution

    By default PSW.3 = 0 and PSW.4 =0; thereforeline 1 sets RS1=1 and RS0 = 0, therebyselecting register bank 2.

    Register Bank 2 uses RAM locations 10H 17H.After execution of this program we have thefollowing: RAM location 10H has value 99H RAM location 11H has value 85H RAM location 12H has value 3FH RAM location 17H has value 63H RAM location 15H has value 12H

  • 8/8/2019 Lect5 Assembler

    28/56

    Stack in the 8051

    Section of RAM used by the CPU tostore information temporarily.

    Information can be data or anaddress.

    The CPU needs this storage area

    since there are only a limitednumber of registers.

  • 8/8/2019 Lect5 Assembler

    29/56

    How stacks are accessedin the 8051

    The register used to access the stack is called theSP (stack pointer) and is 8-bits wide (00h-ffh).

    When the 8051 is powered up the SP contains thevalue 07. This means that RAM location 08 is the first location

    used for the stack. Final location is 1F (20h -> used for bit- addressable

    memory) Storing of a CPU register in the stack is called a

    PUSH. Loading the contents of the stack back into a CPUregister is called a POP.

  • 8/8/2019 Lect5 Assembler

    30/56

    Pushing onto the stack

    The SP points to the last used location of thestack.

    As we push data onto the stack, the stackpointer is incremented by one.

    When you pop a value off the stack, the8051 returns the value from the memory

    location indicated by SP, and thendecrements the value of SP.

  • 8/8/2019 Lect5 Assembler

    31/56

    Example

    Show the stack and stack pointer for the following:

    MOV R6,#25H

    MOV R1,#12H

    MOV R4,#0F3HPUSH 6 ;push onto stack from R6

    PUSH 1 ;push onto stack from R1

    PUSH 4 ;push onto stack from R4

    POP 3 ;pop stack into R3POP 5 ;pop stack into R5

    POP 2 ;pop stack into R2

  • 8/8/2019 Lect5 Assembler

    32/56

    Upper Limit Ram locations 08 1F used for the

    stack. If more than 24bytes of stack required,

    then the SP must be changed to pointto RAM locations 30h-7Fh using theinstruction

    MOV SP,#xx Also may need to shift SP if a given

    progam needs register bank1,2 or 3.

  • 8/8/2019 Lect5 Assembler

    33/56

    Example

    Show the stack and stack pointer for thefollowing instructions:MOV SP,#5FH

    MOV R2,#25HMOV R1,#12HMOV R4,#0F3HPUSH 2PUSH 1PUSH 4

  • 8/8/2019 Lect5 Assembler

    34/56

    Questions Write a simple program in which the value 55h is added five times.

    Show the stack and the stack pointer for each line of the following:Org 0MOV SP,#70HMOV R5,#66HMOV R2,#7FH

    MOV R7,#5DHPUSH 5PUSH 2PUSH 7CLR AMOV R2,AMOV R7,APOP 7POP 2POP 5

  • 8/8/2019 Lect5 Assembler

    35/56

    Program Flow

    When an 8051 is first initialized, it resets thePC to 0000h.

    The 8051 then begins to execute instructions

    sequentially in memory unless a programinstruction causes the PC to be otherwisealtered.

    There are various instructions that can modifythe value of the PC; specifically, conditionalbranching instructions, direct jumps and calls,and "returns" from subroutines.

    Additionally, interrupts, when enabled, cancause the program flow to deviate from itsotherwise sequential scheme.

  • 8/8/2019 Lect5 Assembler

    36/56

    Loop and JumpInstructions

    Repeating a sequence of instructions a certainnumber of times is called a loop.

    The loop action is performed by the instruction

    DJNZ reg,label In this instruction, the register is decremented;

    if it is not zero, it jumps to the target addressreferred to by the label.

    Prior to the start of the loop the register isloaded with the counter for the number ofrepetitions.

  • 8/8/2019 Lect5 Assembler

    37/56

    Example

    Write a program to clear the Acc, thenadd 3 to the accumulator ten times.

    MOV A,#0

    MOV R2,#10

    AGAIN: ADD A,#03DJNZ R2,AGAIN

    MOV R5,A

  • 8/8/2019 Lect5 Assembler

    38/56

    Question

    Write a program to load theaccumulator with the value 10h

    and then complement the Acc700times. (Hint: Try two separateloops to achieve the overall of 700)

  • 8/8/2019 Lect5 Assembler

    39/56

    Solution

    MOV A,#10H

    MOV R3,#10

    NEXT: MOV R2,#70

    AGAIN: CPL A

    DJNZ R2,AGAINDJNZ R3,NEXT

  • 8/8/2019 Lect5 Assembler

    40/56

    Other Conditional Jumps

    Instruction Action

    JZ Jump if A = 0

    JNZ Jump if A 0

    DJNZ Decrement and jump if A 0

    CJNE A,byte Jump if A byte

    CJNE reg,#data Jump if byte #data

    JC Jump if CY = 1

    JNC Jump if CY = 0

    JB Jump if bit = 1

    JNB Jump if bit = 0

  • 8/8/2019 Lect5 Assembler

    41/56

    Example

    Find the sum of the values 79H,F5H and E2H. Put the sum of the

    registers in R0 (low byte) and R5(high byte)

  • 8/8/2019 Lect5 Assembler

    42/56

    Solution

    MOV A,#0 ;clear A

    MOV R5,A ;clear R5

    ADD A,#79H ;A=A+79h

    JNCN_1 ;if no carry, add nextINCR5 ;if CY=1, increment R5

    N_1: ADD A,#0F5H ;A=79H+F5H=6EH and CY1=1

    JNCN_2 ;jump if CY=0

    INCR5 ;if CY=1, increment R5

    N_2: ADD A,#0E2H ;A=6E+E2=50H and CY=1JNCOVER ;jump if CY=0

    INCR5 ;if CY=1, increment R5

    OVER: MOV R0,A ;now R0=50H and R5=02

  • 8/8/2019 Lect5 Assembler

    43/56

    Unconditional JumpInstructions

    All conditional jumps are short jumps,meaning that the address of the target

    must be within -128 and +127 bytes ofthe contents of the program counter(PC).

    Unconditional jump instructions are: LJMP (Long jump) 3 byte instruction SJMP (Short jump) 2 byte instruction

  • 8/8/2019 Lect5 Assembler

    44/56

    CALL instructions

    CALL instruction is used to call a subroutine

    LCALL (long call) 3 byte instruction

    ACALL (absolute call) 2 byte instruction When a subroutine is called, control is

    transferred to that subroutine. After finishing execution of the subroutine, the

    instruction RET (return) transfers control backto the caller.

  • 8/8/2019 Lect5 Assembler

    45/56

    Time Delay Generationand Calculation

    For the CPU to execute an instruction takes acertain number of clock cycles.

    In the 8051 family, these clock cycles are

    referred to as machine cycles. We can calculate a time delay using the

    available list of instructions and theirmachine cycles.

    In the 8051, the length of the machine cycledepends on the frequency of the crystaloscillator connected to the 8051 system.

  • 8/8/2019 Lect5 Assembler

    46/56

    Time Delay Generationand Calculation (contd)

    The frequency of the crystal connectedto the 8051 family can vary from 4MHz

    to 30MHz. In the 8051, one machine cycle lasts 12

    oscillator periods.

    Therefore, to calculate the machine

    cycle, we take 1/12 of the crystalfrequency and then take the inverse.

  • 8/8/2019 Lect5 Assembler

    47/56

    Example

    The following shows crystalfrequency for three different 8051-

    based systems. Find the period ofthe machine cycle in each case.

    (a)11.0592MHz

    (b) 16MHz(c)20MHz

  • 8/8/2019 Lect5 Assembler

    48/56

    Solution

    1/11.0592MHz = period per oscillation

    Machine cycle = 12x

    = 1.085s 1/16MHz = period per oscillation

    Machine cycle = 12x

    = 0.75s 1/20MHz = period per oscillation

    Machine cycle = 12x

    = 0.6s

  • 8/8/2019 Lect5 Assembler

    49/56

  • 8/8/2019 Lect5 Assembler

    50/56

  • 8/8/2019 Lect5 Assembler

    51/56

    Delay Calculation

    A delay subroutine consists of two parts:

    (a) setting a counter

    (b) a loop Most of the time delay is performed by the body of the loop. Very often we calculate the time delay based on the

    instructions inside the loop and ignore the clock cyclesassociated with the instructions outside the loop.

    Largest value a register can hold is 255; therefore, one wayto increase the delay is to use the NOP command.

    NOP, which stands for No Operation simply wastes time.

  • 8/8/2019 Lect5 Assembler

    52/56

    Example

    Find the size of the delay in the following program, ifthe crystal frequency is 12MHz.

    MOV A,#55HAGAIN: MOV P1,A

    ACALL DELAY

    CPLA

    SJMP AGAIN

    DELAY: MOV R3,#200HERE: DJNZ R3,HERE

    RET What does the above program do?

  • 8/8/2019 Lect5 Assembler

    53/56

    Solution

    Crystal Cycle

    DELAY: MOV R3,#200 12

    HERE: DJNZ R3,HERE 24

    RET 12

    Therefore, we have a delay of[(200X24)+12+24]x0.083s = 402s

  • 8/8/2019 Lect5 Assembler

    54/56

    Loop inside a loop delay

    Another way to get a large delay is to use a loop insidea loop, which is also called a nested loop. E.g.

    Crystal Cycle

    DELAY: MOV R3,#250 12HERE: NOP 12

    NOP 12

    NOP 12

    NOP 12

    DJNZ R3,HERE 24RET 24

    Time Delay=[250.(12+12+12+12+24)]x0.083s +(12+24)x0.083s = s

  • 8/8/2019 Lect5 Assembler

    55/56

    Question

    For a machine cycle of 1s, find the time delay ofthe following subroutine.

    DELAY:MOV R2,#200

    AGAIN: MOV R3,#250

    HERE: NOP

    NOPDJNZ R3,HERE

    DJNZ R2,AGAIN

    RET

  • 8/8/2019 Lect5 Assembler

    56/56

    Solution

    HERE Loop: (4x250)x1s = 1000 s AGAIN Loop: Repeats the HERE loop 200

    times i.e. 200x1000s = 200ms The instructions MOV R3,#250 and DJNZ

    R2,AGAIN at the beginning at end of theAGAIN loop will add (3x200x1s) = 600s tothe delay time.

    Total execution time 200.6ms (anapproximation since we have ignored thefirst and last instructions in the subroutine)


Recommended