Assembly Language programming (3)

Similar documents
Control Transfer Instructions Jump, Loop, and Call. ECE473/573 Microprocessor System Design, Dr. Shiue

CHAPTER 3 JUMP, LOOP, AND CALL INSTRUCTIONS


JUMP, LOOP AND CALL INSTRUCTIONS

Microcontroller Intel [Instruction Set]

ELEG3924 Microprocessor

Assembly Language programming (2)

8051 Microcontroller Assembly Programming

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

ELEG3923 Microprocessor Ch.3 Jump, Loop, and Call

SN8F5000 Family Instruction Set

8051 Microcontroller

Chapter Family Microcontrollers Instruction Set

ET2640 Microprocessors

Q. Classify the instruction set of 8051 and list out the instructions in each type.

Programming of 8085 microprocessor and 8051 micro controller Study material

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

Module Contents of the Module Hours COs

8051 Microcontrollers

Architecture & Instruction set of 8085 Microprocessor and 8051 Micro Controller

8051 Overview and Instruction Set

UNIT THE 8051 INSTRUCTION SET AND PROGRAMMING

Dodatak. Skup instrukcija

DR bit RISC Microcontroller. Instructions set details ver 3.10

Programming Book Microcontroller Kit. Rev 3.0 January, Wichit Sirichote

Instruction Set Of 8051

Assembly Language programming (1)

UNIT-III ASSEMBLY LANGUAGE PROGRAMMING. The CPU can access data in various ways, which are called addressing modes

Digital Blocks Semiconductor IP

Digital Blocks Semiconductor IP

Digital Blocks Semiconductor IP

Microcontroller. Instruction set of 8051

Embedded Controller Programming

TUTORIAL Assembly Language programming (2)

C51 Family. Architectural Overview of the C51 Family. Summary

Memory organization Programming model - Program status word - register banks - Addressing modes - instruction set Programming examples.

Lecture 5. EEE3410 Microcontroller Applications Department of Electrical Engineering Assembly Language Programming (1)

EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 4 The 8051 Architecture

Dragonchip. Instruction Set Manual

CPEG300 Embedded System Design. Lecture 6 Interrupt System

INSTRUCCIONES ARITMETICAS ERROR! MARCADOR NO DEFINIDO.

Principle and Interface Techniques of Microcontroller

What Registers are available? Programming in Assembler. Assembler Programming - like early Basic. Assembler Data Movement Instructions

MICROPROCESSOR LABORATORY MANUAL

2. Write an 8051 program to generate a square wave of 25 khz at pin P2.3 using XTAL = 12 MHz. Solution:

Assembly Language Programming of 8085

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

Introduction to uc & Embedded Systems

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

~: Simple Programs in 8051 assembly language :~

Highlights. FP51 (FPGA based 1T 8051 core)

Introduction To MCS-51

Assembly Language Programming of 8085

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

The 8051 Microcontroller and Embedded Systems

Application Brief D-005

MCS -51 Programmer s Guide and Instruction Set

TUTORIAL. Donal Heffernan University of Limerick May Tutorial D.Heffernan 2000,

NAME as31 - An Intel 8031/8051 assembler. SYNOPSIS as31 [-h] [-l] [-s] [-v] [-Aarg] [-Ffmt] [-Ofile] infile.asm


ELEG3924 Microprocessor

Principle and Interface Techniques of Microcontroller

8051 Single Board Monitor Programming. Minmon - Yeralan & Ahluwalia. PaulMon1 & PaulMon2 - Paul Stoffregen

Subroutines & Software Delay Routines

Chapter 3 BRANCH, CALL, AND TIME DELAY LOOP

Contents. Join the Technical Community Today!

8051 Core Specification

80C51 family programmer s guide and instruction set. 80C51 Family. PROGRAMMER S GUIDE AND INSTRUCTION SET Memory Organization. Philips Semiconductors

Chapter 3. Bit Addressable Area. By DeccanRobots

Introduction to Assembly Language Programming (Instruction Set) 1/18/2011 1

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

ELEG3923 Microprocessor Ch.9 Timer Programming

UNIT 1 REFERENCE 1 PREPARED BY S.RAVINDRAKUMAR, LECT/ECE, CHETTINAD COLLEGE OF ENGG AND TECH, KARUR


Arithmetic and Logic

ORG ; TWO. Assembly Language Programming

C51 Family. C51 Family Programmer s Guide and Instruction Set. Summary

Microcontroller and Applications

2003 LXI H, 42F2H ; this instruction store 42F2 in to the HL pair POP H ; store data from top of the stack to HL pair

Question Bank Microprocessor and Microcontroller

Practical Course File For

CPEG300 Embedded System Design. Lecture 8 Timer

8051 Programming: Arithmetic and Logic

MODULE-1. Short Answer Questions


Interfacing ahantronix 128x64 Graphic Module to an 8-bitMicrocontroller

ELEG3923 Microprocessor Ch.4 I/O Ports

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 9 Simple I/O Interfacing

(2) Explain the addressing mode of OR What do you mean by addressing mode? Explain diff. addressing mode for 8085 with examples.

UNIT MICROCONTROLLER AND ITS PROGRAMMING

AL8051S 8-BIT MICROCONTROLLER Application Notes

CEIBO FE-5131A Development System

Counters & Time Delays. Microprocessors & Interfacing 1

Instruction set of 8085

CPEG300 Embedded System Design. Lecture 3 Memory

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

D: arc SRC KUT51 KUT51LCD.LST KUT51LCD PAGE 1

BHARATHIDASAN ENGINEERING COLLEGE. III Year / V Semester / EEE MICROPROCESSORS AND MICROCONTROLLERS (R-2013)

Transcription:

EEE3410 Microcontroller Applications LABORATORY Experiment 3 Assembly Language programming (3) Name Class Date Class No. Marks Conditional Program Branching and Subroutine Call in 8051 Objectives To learn and practice the following conditional jump and subroutine call instruction in 8051: - Branch Instructions - Looping - Call and Return Instructions To practice the use of 8051 simulation and debugging software in program design Equipment/Components Required Personal computer with Windows XP operating system installed MedWin 8051 simulation software Ver Author(s) Date Remark 1.0 KK SIN 9/2006 Laboratory Experiment 3 1

A. Background Teaching lecturer should go through the related topic on Branching and Subroutine Call with students before carrying out the following lab. B. Programming Exercise B.1 Jump Instruction When JUMP instruction is executed, it directs the program flow to the immediate destination address which follows the JUMP instruction. There are two kinds of Jump instructions. They are classified into two groups; CONDITIONAL JUMP and UNCONDITIONAL JUMP. CONDITIONAL JUMP is a jump to the target location in which the control of the jump is valid only if a condition is met, e.g. the status of Carry Flag, result of comparison, etc. UNCONDITIONAL JUMP is a jump to the target location where no condition is needed to control the jump. B.1.1 Conditional Jump Instruction All conditional jumps are short jumps and the range of jump to the target address must be within -128 to +127 bytes. Conditional jump instructions for 8051 are summarized as below: Instruction Action JZ Jump if A equal 0 JNZ Jump if A not equal 0 DJNZ Decrement and jump, if A not equal 0 CJNE A, byte Jump, if A not equal to byte CJNE reg, #data Jump, if byte not equal to #data JC Jump, if CY =1 JNC Jump, if CY =0 JB Jump, if bit equal to 1 JNB Jump, if bit equal to 0 JBC Jump, if bit equal to 1 and clear bit B.1.1.1 Use 8051 simulation software, create and carry out simulation of the assembly program in the Table B1.1.1. Open suitable windows to observe the change of values in the registers and internal RAM during program execution. 2 Experiment 3 Laboratory

Table B1.1.1 line Mnemonics Operands 1. ORG 00H 2. MOV R3, #42 3. MOV A, #55H 4. JZ NEXT 5. INC R3 6. AGAIN: INC A 7. INC A 8. NEXT: ADD A, #7EH 9. JNC OVER 10. CLR A 11. MOV R0, A 12. MOV R1, A 13. MOV R2, A 14. NOP 15. MOV R4, A 16. OVER: ADD A, R3 17. JNC AGAIN 18. HERE: SJMP HERE 19. END (1) Fill in the Op Codes in the following blank boxes. EEE3410 Microcontroller Applications line Code Address Op Codes Mnemonics Operands 4. 0004 JZ NEXT : : 6. 0007 AGAIN: INC A : : 8. 0009 NEXT: ADD A, #7EH 9. 000B JNC OVER : : : : 16. 0013 OVER: ADD A, R3 17. 0014 JNC AGAIN (2) Verify by calculation on how to find out the forward addresses in the Op code of the three conditional jumps. Laboratory Experiment 3 3

(3) Execute the program step-by-step until the last instruction and write down the contents of the following registers. Register Content Register Content A R0 R1 R2 R3 CY flag B.1.1.2 JB instruction enables the program jump to the specified destination if the testing Bit is set. Use 8051 simulation software, create and carry out simulation run of the assembly program in the Table B1.1.2. During simulation run, open the Ports window and use mouse to set the value of P1 equal to 80H. What happen when Port 1 is set to value 80H? Table B.1.1.2 line Mnemonics Operands 1. ORG 00H 2. MOV P1, #0FFH 3. LOOP: MOV A, P1 4. NOP 5. CJNE A, #80H, LOOP 6. HERE: SJMP HERE 7. END If JB instruction is used to replace the instruction CJNE to monitor the set of Bit 7 of Port 1, re-write the program lines 3 to 5 in Table B.1.1.2 to give the same execution result. line Mnemonics Operands 1. ORG 00H 2. MOV P1, #0FFH 3. 4. 5. 6. HERE: SJMP HERE 7. END 4 Experiment 3 Laboratory

EEE3410 Microcontroller Applications B.1.1.3 Looping is widely used in the 8051 to repeat a sequence of instruction at a number of times you want. To perform the looping action, instructions DJNZ and CJNE are commonly used as a counting machine. The program in Table B.1.1.3 demonstrates how to use R2 to control looping. Table B.1.1.3 Line Label Mnemonics Operands 1. ORG 00H 2. MOV A, #0H 3. MOV R2, #10 4. AGAIN: ADD A,#03 5. DJNZ R2,AGAIN 6. END (a) How many of times that the loop AGAIN is repeated? (b) What is the maximum number of times that the loop AGAIN can be repeated? B.1.1.4. Nested loop means there are loops inside a loop. For a 2-layer nested loop, 2 registers are used as counting machines. Table B.1.1.4 demonstrates a nested loop program using 2 counting registers. One register (R2) holds the number of repetitions of inner loop and the other register (R3) holds the number of repetitions of the outer loop. Inner loop count is decremented by 1 for each execution and jumped to the memory location AGAIN to repeat the instructions inside the loop again until R2 equal to zero. Then go to the outer loop. It works the same as inner loop; decremented by 1, then jumps to the memory location NEXT and repeats inner loop. The process will continue until R3 equal to 0. Table B.1.1.4 Line Label Mnemonics Operands 1. ORG 0H 2. MOV A, #0H 3. MOV R3,#3 4. NEXT: MOV R2,#10 5. AGAIN: ADD A, #2 6. DJNZ R2,AGAIN 7. DJNZ R3,NEXT 8. END Laboratory Experiment 3 5

(a) How many times is the instruction ADD A, #2 at repeated? (b) What is the maximum number of times that line 5 can be repeated by the nested loop? (c) Modify the program in Table B.1.1.4 so that execution of line 5 can be repeated one million times, i.e. 1,000,000 times. Write down your program in the following table. Line Label Mnemonics Operands 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 6 Experiment 3 Laboratory

EEE3410 Microcontroller Applications B.1.2 Unconditional Jump Instruction Unconditional Jump instruction causes the program jump to a target location without a condition. There are 3 kinds of unconditional jumps: LJMP (long jump), SJMP (short jump) and AJMP(absolute jump). LJMP allows the program counter jump to any address location within the 64K code space (16-bit address from 0000h to FFFFh). However, SJMP allows the program counter jump either forward or backward within a range of -128 and 127 bytes relative to the original memory address. AJMP allows the program jump to the target address within a 2K bytes program memory page unconditionally. Table B.1.2(a) Line Code Address Op Codes Label Mnemonics Operands 1. ORG 0H 2. HERE: MOV R1, #0F0H 3. MOV R2, #0FH 4. AJMP THERE 5. ORG 070H 6. THERE: CPL A 7. MOV R1, #0FH 8. MOV R2, #0F0H 9. LJMP HERE 10. END (a) Assembly the source programs in the Table B1.2(a). Fill in the code addresses and Opcodes in Table B1.2(a). Repeat the same steps for Table B1.2(b). Table B.1.2(b) Line Code Address Op Codes Label Mnemonics Operands 1. ORG 0H 2. HERE: MOV R1, #0F0H 3. MOV R2, #0FH 4. AJMP THERE 5. ORG 070H 6. THERE: CPL A 7. MOV R1, #0FH 8. MOV R2, #0F0H 9. SJMP HERE 10. END Laboratory Experiment 3 7

(b) Observe and state the difference between the operands of the two JUMP instructions, LJMP and SJMP. B2. Subroutine Call Instruction The main different between Subroutine Call and JUMP instruction is that after the completion of a subroutine call, the program will return to the memory address of next instruction follow the subroutine call. The program will resume execution at there. 2 kinds of instructions for subroutine call: ACALL (Absolute Call) and LCALL (Long Call). The ACALL instruction is a 2 byte instruction. It calls subroutines with a target address within 2K bytes from the current program counter (PC). It apparently gives a 16-bit memory address to PC where the called subroutine starts. The LCALL instruction is a 3-byte instruction, the first byte is the opcode and the second and third byte is the operand, which denotes the start of memory address of the subroutine call. LCALL can be used to call subroutines located anywhere within the 64kbyte memory address. Both instructions require stack to store up the 16-bit memory address of next instruction temporarily for the return of subroutine. The 16-bit memory address is pushed onto the stack, upper-order byte first and lower-order byte next. Time delay function is commonly implemented by Subroutine Call instruction. Table B.2.1 is an example of a Time Delay Subroutine. Line Label Mnemonics Operands Machine Total Machine Cycles Cycles 1. ORG 0H 2. ACALL DELAY 2 3. BACK: SJMP BACK 1 4. ; Time Delay Subroutine 5. DELAY: MOV R2, #200 1 250 loops 6. HERE1: MOV R3,#250 1 200 loops 7. HERE2: DJNZ R3, HERE2 2 = machine 8. DJNZ R2, HERE1 2 cycles? 9. RET 1 10. END 8 Experiment 3 Laboratory

EEE3410 Microcontroller Applications (a) What is/are the value(s) stored in the stack when the DELAY subroutine is executed? (b) The first loop count repeats 250 times, after that, the second loop count repeats 200 times. What is the no. of machines cycles required to complete the execution of the DELAY subroutine? Assume the 8051 uses a 12MHz clock, then the time for 1 machine cycle (corresponds to 12 clock oscillation periods) is: 1 6 1 machine cycle = 12T = 12 s = 1 10 s, i. e.1µ s. 6 12 10 (c) What is the total time delay generated by the DELAY subroutine? (d) Modify the Time Delay subroutine in Table B.2.1 so that it has a 3 seconds time delay. Write down your subroutine program in the table below. Line Label Mnemonics Operands 4. ; Time Delay Subroutine 5. DELAY: 6. 7. 8. 9. 10. 11. 12. 13. Laboratory Experiment 3 9

(e) Use the 8051 simulation software to test the delay time generated by the subroutine written by you. C. Exercise Analyze the stack for the first and the second LCALL instructions in the following program. Line Code Address Op Codes Label Mnemonics Operands 1. 0000 ORG 0H 2. 0000 74 55 BACK: MOV A, #55H 3. 0002 F5 90 MOV P1, A 4. 0004 7B A4 MOV R3, #A4H 5. 0006 7D 78 MOV R5, #78H 6. 0008 51 00 ACALL DELAY 7. 000A 74 AA MOV A, #0AAH 8. 000C F5 90 MOV P1, A 9. 000E 12 02 00 LCALL DELAY 10. 0011 80 ED SJMP BACK 11. 0013 ; This is the Delay subroutine 12. 0200 ORG 200H 13. 0200 C0 03 DELAY: PUSH 3 14. 0202 C0 05 PUSH 5 15. 0204 7B FF MOV R3, #0FFH 16. 0206 7D FF NEXT: MOV R5, #0FFH 17. 0208 DD FE AGAIN: DJNZ R5, AGAIN 18. 020A DB FA DJNZ R3, NEXT 19. 020C D0 05 POP 5 20. 020E D0 03 POP 3 21. 0210 22 RET 22. 0200 END Fill in the values in the stack after the execution of the instructions. STACK After the ACALL After the first After the first PUSH 3 PUSH 5 After the LCALL 0C 0C 0C 0C 0B 0B 0B 0B 0A 0A 0A 0A 09 09 09 09 08 08 08 08 - End - 10 Experiment 3 Laboratory