Assembly Language programming (1)

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

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

Assembly Language programming (2)

ELEG3924 Microprocessor

Assembly Language programming (3)

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

The 8051 Microcontroller and Embedded Systems

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

Chapter Addressing Modes

Principle and Interface Techniques of Microcontroller

Microcontroller Intel [Instruction Set]

8051 Microcontrollers

8051 Microcontroller Assembly Programming

8051 Programming using Assembly

8051 Microcontrollers

ORG ; TWO. Assembly Language Programming

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

8051 Microcontroller

ENE 334 Microprocessors

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

Module Contents of the Module Hours COs

TUTORIAL Assembly Language programming (2)

8051 Overview and Instruction Set

NEW CEIBO DEBUGGER. Menus and Commands

CodeWarrior. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Programming of 8085 microprocessor and 8051 micro controller Study material

SN8F5000 Family Instruction Set

Instruction : A command to the microprocessor to perform a given task on specified data. Each instruction has two parts

MICROCONTROLLER UNIT 1

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

Figure Programming model

CPEG300 Embedded System Design. Lecture 3 Memory

Question Bank Microprocessor and Microcontroller

Laboratory: Introduction to Mechatronics. Instructor TA: Edgar Martinez Soberanes Lab 2. PIC and Programming


8051 Addressing Modes

Microcontroller. Instruction set of 8051

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

Embedded Controller Programming

Mark II Aiken Relay Calculator

Dodatak. Skup instrukcija

Programming Book Microcontroller Kit. Rev 3.0 January, Wichit Sirichote

Principle and Interface Techniques of Microcontroller

ME4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 7

Architecture & Instruction set of 8085 Microprocessor and 8051 Micro Controller

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

EE6502- MICROPROCESSOR AND MICROCONTROLLER

8051 Instruction Set


Digital Blocks Semiconductor IP

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

JUMP, LOOP AND CALL INSTRUCTIONS

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad


MODEL ANSWER WINTER 17 EXAMINATION Subject Title: Microcontroller and applications

Assembly Language Programming

ET2640 Microprocessors

CPEG300 Embedded System Design. Lecture 6 Interrupt System

Digital Blocks Semiconductor IP

Contents. Join the Technical Community Today!

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

Cross Assembly and Program Development

8051 Microcontroller

CC411: Introduction To Microprocessors

Digital Blocks Semiconductor IP

ECE 331: PC Lab 3 Stack and Subroutines

Assembly Language Fundamentals

Computer Organization and Assembly Language. Lab Session 01

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

C51 Family. Architectural Overview of the C51 Family. Summary


UNIT THE 8051 INSTRUCTION SET AND PROGRAMMING

sarm User Guide Note that a space must appear before the operation field because any word beginning in the first column is a label

DR bit RISC Microcontroller. Instructions set details ver 3.10

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

Subroutines & Software Delay Routines

MODEL ANSWER SUMMER 17 EXAMINATION Subject Title: Microcontroller and Applications Subject Code:

Professor E. Ambikairajah UNSW Sydney

1. Write A Program to move a block of data within the internal RAM

AS-2883 B.Sc.(Hon s)(fifth Semester) Examination,2013 Computer Science (PCSC-503) (System Software) [Time Allowed: Three Hours] [Maximum Marks : 30]

Introduction To MCS-51

PIC Discussion. By Eng. Tamar Jomaa

INSTITUTE OF ENGINEERING AND MANAGEMENT, KOLKATA Microprocessor

CPU. IBM PC and compatible Memory Structure

Getting Started with the HCS12 IDE

MICROPROCESSOR LABORATORY MANUAL

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

8051 Core Specification

MODULE-1. Short Answer Questions

8086 ALP TOOLS (CH 2) CHAPTER 2

ET355 Microprocessors Friday 6:00 pm 10:20 pm

Sencer Yeralan and Helen Emery Gainesville, Florida January 2000

LAB FILE of EE1531: MICROCONTROLLER & EMBEDDED SYSTEMS LAB

EEM336 Microprocessors I. Data Movement Instructions

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

ET2640 Microprocessors

Chapter 3. Bit Addressable Area. By DeccanRobots

Instruction Set Of 8051


Transcription:

EEE3410 Microcontroller Applications LABORATORY Experiment 1 Assembly Language programming (1) Name Class Date Class No. Marks Familiarisation and use of 8051 Simulation software Objectives To learn how to use PC to write simple 8051 assembly instructions of different addressing modes To learn and 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 1 1

A. Familiarisation of 8051 assembly programming A.1 Introduction to 8051 assembly programming CPU can work only in binary. A program consists of 0s and 1s is called machine language. Although the hexadecimal system was used as a more efficient way to represent binary numbers, the process of programming in machine code was still cumbersome for humans. Assembly languages were developed that provided mnemonics for the machine code instruction that made programming faster and less prone to error. The term mnemonics refer to codes and abbreviations that are relatively easy to remember. Assembly language programs must be translated into machine codes by a program called an assembler. Assembly language is referred to as low-level language because it deals directly with the internal structure of the CPU. To program in Assembly language, the programmer must know all the registers of the CPU and the size of them, as well as other details. A.2 Structure of Assembly language An Assembly language instruction basically consists of four fields: [label:] mnemonic [operands] ;comment Brackets indicate that the field is optional, and not all lines have them. When the field has content, brackets should not be typed in. Table A.2: Example of an assembly language program [label:] mnemonic [operands] ;comment COUNT EQU 25H ;COUNT = 25H ORG 0H ;start (origin) at location 0 MOV R5, #COUNT ;load 25H into R5 MOV R7,#34H ;load 34H into R7 MOV A,#0 ;load 0 into A ADD A,R5 ;add contents of R5 to A, now A = A + R5 ADD A,R7 ;add contents of R7 to A, now A = A + R7 ADD A,#12H ;add to A value 12H, now A = A + 12H NOP ;no operation HERE: SJMP HERE ;stay in this loop ORG 10H DATA1: DB 39H ; DATA2: DB America ; END ;end of assembly source file 2 Experiment 1 Laboratory

EEE3410 Microcontroller Applications Note: 1. The label field allows the program to refer to a line of code by name. Any label referring to an instruction must be followed by a colon symbol, :. 2. An assembly language instruction consists of a mnemonic, optionally followed by one, two or no operands. The operands are the data items being manipulated, and the mnemonics are the commands to the CPU. The mnemonic (instruction) and operand(s) fields together perform the real work of the program and accomplish the tasks for which the program was written. 3. Instead of mnemonics and an operand, these two fields could contain assembler pseudo-instructions, or directives. e,g, ORG (origin) indicates the beginning of the address of the instructions. The number that comes after ORG can be either hex or decimal. END indicates to the assembler the end of the source assembly instructions. EQU (equate) used to define a constant without occupying a memory location. It does not set aside storage for a data item but associates a constant value with a data label so that when the label appears in the program. Its constant value will be substituted for the label. DB (define byte) used to define 8-bit data and store them in assigned memory locations. Define data can be in decimal, binary, hex, or ASCII formats. 4. The comment field begins with a semicolon comment indicator ;. Comments may be at the end of a line or on a line by themselves. They used to describe the program and make it easier for reading and understanding. The assembler ignores the comments, but they are essential to programmers. A.3 Assembling and running of an 8051 program The steps to create executable machine codes from assembly language program are outlined as follows. Use an editor program to create an assembly source program, e.g. myfile.asm The asm source file is then converted by an 8051 assembler to machine codes. Two files, an object file and a list file, are produced. A linker program takes the object file and produces an absolute object file with extension abs and fed it into a object to hex conversion program (OBJ-HEX) to create a hex program that is ready to be run by 8051. Myfile.lst EDITOR PROGRAM ASSEMBLER PROGRAM LINKER PROGRAM Myfile.abs OBJ-HEX PROGRAM Myfile.asm Myfile.asm Myfile.obj Laboratory Experiment 1 3

Content of the List file of the assembly language program in Table A.2 LOC OBJ LINE SOURCE Original content of the source file 0025 1 COUNT EQU 25H ;COUNT = 25H 0000 2 ORG 0H ;start (origin) at location 0 0000 7D25 3 MOV R5, #COUNT ;load 25H into R5 0002 7F34 4 MOV R7, #34H ;load 34H into R7 0004 7400 5 MOV A, #0 ;load 0 into A 0006 2D 6 ADD A, R5 ;add contents of R5 to A, now A = A + R5 0007 2F 7 ADD A, R7 ;add contents of R7 to A, now A = A + R7 0008 2412 8 ADD A, #12H ;add to A value 12H, now A = A + 12H 000A 00 9 NOP ;no operation 000B 80FE 10 HERE: SJMP HERE ;stay in this loop 0010 11 ORG 10H 0010 39 12 DATA1: DB 39H ; 0011 416D6572 13 DATA2: DB "America" ; 0015 696361 14 END ;end of assembly source file SYMBOL TABLE LISTING ------ ----- ------- N A M E T Y P E V A L U E ATTRIBUTES COUNT N NUMB 0025H A DATA1 C ADDR 0010H A DATA2 C ADDR 0011H A HERE C ADDR 000BH A REGISTER BANK(S) USED: 0 ASSEMBLY COMPLETE. 0 WARNING(S), 0 ERROR(S) Corresponding machine codes for the mnemonics instructions Indicates the memory locations that the machine codes being stored. The warnings/errors messages indicate the result of the assembly process. Zero means that the program is successfully converted and ready to use, otherwise, correction on the source file is needed. Content of the Hex file of the assembly language program in Table A.2 :0D0000007D257F3474002D2F24120080FEEA :0800200039416D6572696361ED :00000001FF Separating the fields we get :CC AAAA TT Data SS :0D 0000 00 7D257F3474002D2F24120080FE EA :08 0020 00 39416D6572696361 ED :00 0000 01 FF CC the count byte. It tells the loader how many bytes are in the line. AAAA It is a 16-bit address which tells the loader where the first byte of data to be placed. TT Either 00 or 01. 00 means there are more lines to follow after this line. 01 means this is the last line. Data the real information and the length is 16 bytes in maximum. SS checksum byte which used for error checking. 4 Experiment 1 Laboratory

EEE3410 Microcontroller Applications B. Familiarisation of 8051 simulation software B.1 Open the 8051 simulation software Execute the program MedWin inside Folder Manley Click the button Simulation when dialogue box Set LPT Port is displayed on the screen. (Note: the software can only be running at simulation mode. The emulation mode is not worked as there is no emulator connected to the PC port) The program window of MedWin is then displayed and the software is ready to use. B.2 Editing/Create an assembly program Inside the program window of MedWin, use New command in the File pull-down menu to create a new source file. Go into the folder ASM files. Then type the file name myfile.asm and click open. An editor window will turn up. Display on the desktop screen should be observed as follow. Figure B2 The editor window will allow you to type in the assembly source program. Use Save command to save up your typed program. Laboratory Experiment 1 5

B.3 Assemble the source program With the source program in the editor window. Use Make&Load command in the Project pull-down menu to assembly the source program into machine executable instructions. This single comment will produce all necessary files, included the List file and Hex file and others are used by MedWin. A window is then popped up at the bottom of the screen to show out the assembly result. If error or warning message was shown in the window, there should be incorrect or undefined statements being found in the source program. e.g. syntax errors or undefined variables. The assembly process could not be completed. You should correct the errors and repeat the assembly procedures again until no such message appears. B.4 Monitoring functions of the simulation software The simulation software has monitoring functions which are activated for use after the source program is being assembly. Commands inside the View pull-down menu: Registers - watch the immediate contents of all registers of 8051. SFR - watch the immediate contents of Special Function Registers of 8051. Disassembly code - display the memory locations where the 8051 machine codes placed. Watches groups 1/2/3/4 allow to set watches groups on target expressions. Variable allow setting watches on target variables. Memory IData/Data/Code/XData/Bit/PData - display the contents of memory page of IData/Data/Code/XData/Bit/PData Commands inside the Peripherals pull-down menu: Interrupts Display the status of Seven Interrupts of 8051. Ports - Display the status of the Four I/O Ports of 8051. Timer/Counter 0 - Display the status of Timer/Counter 0 of 8051. Timer/Counter 1 - Display the status of Timer/Counter 1 of 8051. Timer/Counter 2 - Display the status of Timer/Counter 2 of 8051. Serial Ports - Display the status of Serial Ports 0 of 8051. The display of the desktop screen showed on the next page when monitoring functions commands of Registers, SFR, Disassembly code and Memory Code were being carried out. 6 Experiment 1 Laboratory

EEE3410 Microcontroller Applications Source program window Location where Program Counter pointed to Content of Program Counter Disassembly code window Machine codes of assembly instruction Trace button Step button Animate button Stop button Reset button B.5 Animation runs Code memory page window Registers window Figure B4 Special Function Registers window Press Animate button will start to simulate the running of the 8051 program. A dialogue box appears and asks for the speed of the simulation run. Press OK will start the simulation. Press Stop button will stop the simulation run. Press Trace button will only simulate the running of the 8051 instruction pointed by the program counter and stops. Program counter will point to the next instruction. The immediate result will show on the SFR and Registers windows. Press Step button, the result is similar to Trace button. The difference is that when Laboratory Experiment 1 7

the Step command meets a subroutine call instruction, it will execute the whole subroutine as one single step. But Trace command will not and it will go into the subroutine and the instructions inside one by one. The immediate results will show on the SFR and Registers windows. Press Reset button will restore all 8051 registers to its original values. C. Programming exercise C.1 Familiarisation (1) Open the 8051 simulation software in Part B. Create a new file with filename 8051Lab02_asm in the editor window. (2) Type in the assembly program in table A.2 into the editor window. (3) Assemble the source program until no compilation error or warning message occurs. (4) Issue suitable commands to show the following windows on screen 4.1 Disassembly Code window 4.2 The Register window 4.3 The Special Function Registers (5) Complete the following table by writing down the addresses and opcodes corresponding to the instructions inside the program. Table C1 Code Address Op Codes Mnemonics Operands ORG 0H 0000 7D 25 MOV R5, #25H 0002 7F 34 MOV R7,#34H 0004 74 00 MOV A,#0 0006 2D ADD A,R5 0007 2F ADD A,R7 0008 24 12 ADD A,#12H 000A 00 NOP 000B 80 FE HERE: SJMP HERE ORG 10H 0010 39 DATA1: DB 39H 0011 41 6D DATA2: DB America 0013 65 72 END 0015 69 0011 63 61 8 Experiment 1 Laboratory

EEE3410 Microcontroller Applications (6) Execute the program steps by steps until end. Note the change of values in the registers (Use the reset command to restart the program execution at the beginning). (7) Record down the values in accumulator A, registers R5 & R7 at the start of the execution. Value of the Accumulator A = 00H R5 = FFH R7 = FFH (8) Record down all change values in Accumulator A when program is executed. Change of values in the Accumulator A = 00H 25H 59H 6B (9) Print out the List file and Hex file of the program. C.2 Data Movement C.2.1 Immediate Addressing The source operand is a constant in immediate addressing mode. The immediate data must be preceded by the pound sign #. This addressing mode is used to load data into any of the registers. (1) Type in the following assembly program into the editor window. Table C.2.1 line Mnemonics Operands 1. ORG 0h 2. MOV A, #25h 3. MOV B, #18h 4. MOV R1, #0CDh 5. MOV R2, #0A1h 6. MOV R3, #36h 7. MOV DPTR, #4521h 8. ADD A, #5Bh 9. ADD A, #28h 10. END (2) Assemble the source program and observe the values in the registers during program execution. (3) Record the value of the Accumulator A at the end of execution. A8H C.2.2 Register Addressing Register addressing mode involves the move of data between registers and use them to hold the data to be manipulated. (1) Replace line 10 in the assembly program in table C.2.1 by the following 5 instructions. Table C.2.2 line Mnemonics Operands Laboratory Experiment 1 9

10. MOV A, R2 11. MOV R3, A 12. ADD A, R1 13. MOV A, R3 14. MOV R2, A 15. END (2) Assemble the source program and observe the change of values in the registers during program execution. (3) Record the value of the Accumulator A at the end of execution. A1H C.2.3 Direct Addressing Direct addressing mode is used to transfer data to and from internal memory locations directly between the registers of the 8051. (1) Replace instruction lines 10 to 14 of the assembly program in part C.2.2 by the following 5 instructions. Table C.2.3 line Mnemonics Operands 10. MOV 20h, R1 11. ADD A, 20h 12. MOV 21h, A 13. MOV R3, 20h 14 ADD A, R3 (2) Assemble the source program and observe the change of values in the registers during program execution. (3) Write down the contents of the following at the end of execution Accumulator A = 42H R1 = CDH R3 = CDH Memory locations 20h = CDH 21h = 75H C.2.4 Register Indirect Addressing In the indirect addressing mode, a register is used as a pointer to the data. The content of the register stored the address of the memory location to be accessed. C.2.4.1 Transfer from internal RAM (8-bit memory address) A register (8-bit length) can work as a pointer to store the RAM address to be accessed. (1) Use the 8051 simulation software, create and carry out simulated execution of the assembly program in table C.2.4.1 10 Experiment 1 Laboratory

EEE3410 Microcontroller Applications Table C.2.4.1 line Mnemonics Operands 1. ORG 0H 2. MOV 20H, #38H 3. MOV 21H, #30H 4. MOV 22H, #35H 5. MOV 23H, #31H 6. MOV R0, #20H 7. MOV R1,#30H 8. MOV R2,#40H 9. MOV A,@R0 10. MOV @R1, A 11. INC R0 12. INC R1 13. MOV A,@R0 14. MOV @R1, A 15. INC R0 16. INC R1 17. MOV A,@R0 18. MOV @R1, A 19. INC R0 20. INC R1 21. XCH A, @R0 22. MOV A, @R0 23. MOV @R1, A 24. END (2) Open suitable windows to observe the change of values in the registers and internal RAM during program execution. (3) Write down the contents of the following registers at the end of execution. Accumulator A = 35H R0 = 23H R1 = 33H R2= 40H Internal Memory location Content Internal Memory location Content 20H 38H 30H 38H 21H 30H 31H 30H 22H 35H 32H 35H C.2.4.2 Transfer from code memory (16-bit memory address) A 16-bit Special Function Register (SFR), DPTR register, which is used as a 16-bit pointer to external connected RAM. (1) Use the 8051 simulation software, create and carry out simulated execution Laboratory Experiment 1 11

of the assembly program in table C.2.4.2 Table C.2.4.2 line Mnemonics Operands 1. ORG 200H 2. DB USA 3. ; 4. ORG 0H 5. MOV DPTR, #200H 6. CLR A 7. MOVC A,@A+DPTR 8. MOV R3, A 9. MOV R0, A 10. PUSH 0 11. INC DPTR 12. CLR A 13. MOVC A,@A+DPTR 14. MOV R1, A 15. INC DPTR 16. CLR A 17. MOVC A,@A+DPTR 18. XCH A, R3 19. POP 3 20. END (2) Observe the change of values in the registers during program execution. (3) Write down the contents of the following at the end of execution Register Content Memory location Content R0 55H 200H 55H R1 53H 201H 53H R2 FFH 202H 41H R3 SP DPTR 55H 07H 0202H PART D: Exercise and Discussion 1. What is the size in bytes of the program in Table C.2.1 you assembled? 18 bytes 2. How many machine cycles are needed to execute the program in Table C.2.1? 10 machine cycles 12 Experiment 1 Laboratory

EEE3410 Microcontroller Applications 3. Most of registers in 8051 are 8 bits wide. 4. Registers R0 - R7 are all 8 bits wide. 5. Registers A (ACC) and B are all 8 bits wide. 6. What is the largest hex value that can be moved into an 8-bit register? What is the decimal equivalent of the hex value? Largest hex value = FFH Decimal equivalent = 255 7. Determine the following is (are) legal or illegal? (a) MOV R3, #500 legal / illegal (b) MOV R1, #50 legal / illegal (c) MOV R7, #00 legal / illegal (d) MOV A, #255H legal / illegal (e) MOV A, #50H legal / illegal (f) MOV A, #F5H legal / illegal (g) MOV R9, #50H legal / illegal 8. Distinguish the different between the following two 8051 instructions. a) MOV A, #20H and MOV A, 20H Instruction (a) is to assign hex value 20H to register A, but instruction (b) is to copy the content in RAM address 20H to register A. b) MOV R0, A and MOV @R0, A Instruction (a) is to copy the content in register A to register R0, but instruction (b) is to copy the content in register A to the RAM address specified in register R0. 9. Use the table on next page to write a 8051 assembly program starting at code address 00h to assign registers R1 to R5 with values from 11h to 15h correspondingly. Then transfer the contents of R1 to R5 to internal RAM space starting at 30H. Also gives the corresponding code addresses and Op-codes for your program. Laboratory Experiment 1 13

Program Listing of question (9): Line Code Address Op Codes Mnemonics Operands Remarks 1. ORG 0H 2. 0000 A9 11 MOV R1, 11h 3. 0002 AA 12 MOV R2, 12h 4. 0004 AB 13 MOV R3, 13h 5. 0006 AC 14 MOV R4, 14h 6. 0008 AD 15 MOV R5, 15h 7. 000A 89 30 MOV 30h, R1 8. 000C 8A 31 MOV 31h, R2 9. 000E 8B 32 MOV 32h, R3 10. 0010 8C 33 MOV 33h, R4 11. 0012 8D 34 MOV 34h, R5 12. END - End - 14 Experiment 1 Laboratory