9
Chapter 1 Compiling the First Program A compiler is a program that processes statements written in a particular programming language (C, Assembler, etc.) and turns them into machine code executed directly by a computer's Central Processing Unit (CPU). Typically, a programmer writes language statements one line at a time using an editor. Below, Keil 8051 Development Tools (C51)© is used for defining the Accumulator Register.

Assembler Programming

Embed Size (px)

Citation preview

Page 1: Assembler Programming

Chapter 1

Compiling the First Program

A compiler is a program that processes statements written in a particular programming language

(C, Assembler, etc.) and turns them into machine code executed directly by a computer's Central

Processing Unit (CPU). Typically, a programmer writes language statements one line at a time using

an editor. Below, Keil 8051 Development Tools (C51)© is used for defining the Accumulator

Register.

Page 2: Assembler Programming

Assembler Program and Machine Code………………….…….………. 1

Machine Code in Program Memory………………..…….……………. 1

Assembler’s Output Files.……………………………………….……. 2

Components of Assembly Language line.…………………………...…. 3

Instruction Set.………………………………………………………. 3

Assembler Directives.…………………………………………..……. 3

Bit, Byte, Word, Binary, HEX…….………………………………….... 4

Style Guidelines.……………………..……………………………... 4

Clock.……………………..……………………………..………... 5

Example Program (Delay).……..…………………………..………... 5

Tips.……..…………………………..…………………….……... 7

Page 3: Assembler Programming

Assembler Instruction

Machine Code

Operation Code

Flash/EE Program Memory

Program Counter

Assembler Instruction:

Accumulator Register = FEH

Assembler Instruction:

No Operation

Statement: End

Control Statement:

Program Start Address (0000H)

Assembler Instruction:

Jump to Absolute Address

(0100H)

Control Statement:

Next Code defined at

100H of Program Memory

Label: Represents the program

memory address of an instruction

BEGIN_PROG: = 100H

Machine Code is the closest to the hardware

Mnemonic Description Byte Oscillator Periods

MOV A, #data Move Immediate Data to Accumulator

2 12

Machine Code in Program Memory

General format Description

MOV <dest-byte>, <src-byte> The byte variable indicated by the second operand is copied into the location specified by the first operand. The source byte is not affected. No other register or flags are affected

0 1 1 1 0 1 0 0

The programmer writes a set of assembler instructions

(Assembler Program or Assembly Code) in an editor,

the Assembler translates assembly code to machine

code (sequence of 0's and 1's that constitute the object

program). The output file with the machine code and

other symbols is used to program the Microcontroller.

After Reset, the Microcontroller initializes the Program

Counter (PC, holds the memory address of the next

instruction that would be executed) with the First

Address of Program Memory (0000H) in order to fetch

the first Operation Code (OP) that has the information

of what task the microcontroller must execute.

Assembler Program and Machine Code

Assembly language is a low-level programming language for a programmable device specific to a computer

architecture. The instruction set, used for creating an assembler program, combine mnemonics and syntax that is

translated into their numerical equivalents. This representation typically includes an operation code as well as

other control bits and data. The follow assembler instruction uses 2 bytes in memory and delay 12 clock periods

1

2

7

3

4

5

5

1

6

1

1

2

3

4

1 2

3 4 5

6 7

4

#data

3 5

0x0101 2

0x0100

High Level Language Assembly Language

Machine Code

Hardware

Page 4: Assembler Programming

Assembler’s Output Files

Assembler Instruction Address Program Memory Machine Code

LJMP Address LJMP 0100H

0000H

0001H

0002H

0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 0

Operation Code (02H)

Program Address (15-8): 01H

Program Address (7 - 0): 00H

MOV A, #data MOV A, #0FEH

0100H

0101H

0 1 1 1 0 1 0 0

1 1 1 1 1 1 1 0

Operation Code (74H)

#data: 0FEH

NOP 0102H 0 0 0 0 0 0 0 0

Operation Code (00H)

The Assembler generates several output files during compilation. By default, each output file shares the same filename as the source file but has a different file extension. The most important files are: .HEX: Output file in Intel HEX file format. Every line follows a specific structure explained below

Number of Data bytes in the Record Address Field Data bytes

: 0 3 0 0 0 0 0 0 0 2 0 1 0 0 F A

Colon Record type 00 Checksum .LST: The Assembler Program is listed along with the generated instruction opcodes or machine code. Listing file may optionally contain cross reference. .OBJ: Object file that must be linked with other object files to produce a complete executable.

Assembler Program and Machine Code

Table 1.1.- Assembler Instructions and equivalent Machine Code stored in Program Memory

Listing (LST) File

1

Assembler Program

Address of Program Memory

2 Machine Code

3

4 Symbols Table

5 Register Banks

1 2 3

4

5

2

Page 5: Assembler Programming

Label: Assigns a symbolic representation to the address in program memory corresponding to line

Opcode: Reserved symbols that correspond to the operation to be performed by the microcontroller Operands: Entities operated upon by the instruction Comment: Used to document/understand programs

Components of Assembly Language line

Instruction Set

Assembler Directives

Assembler Program and Machine Code

Each line of assembly language source code has this general form:

{Label} {Opcode} {Operands} {;Comments}

1

2

3

4

1 1

2 2

3 3 4

The Instruction Set of a Microprocessor executes at least the following general operations: Arithmetic and Logical, Data Transfer, Boolean Variable Manipulation and Program Branching, some examples are shown in the following table

Arithmetic Operations Data Transfer Logical Operation Program Branching Boolean Variable

ADDC A,source MOV dest,#data ORL direct,A JNE @Ri,#data,rel SETB C

SUBB A,#data XCHD A,@Ri CPL A DJNZ Rn,rel CLR bit

DIV AB MOV DPTR, #data16 ANL A,#data LCALL addr16 JNB bit,rel

The operands involved in the instructions are described below

Rn Register addressing using R0-R7

direct 8 bit internal address (00H-FFH)

@Ri Indirect addressing using R0 or R1

source Any of [Rn,direct,@Ri]

dest Any of [Rn,direct,@Ri]

#data 8bit constant included in instruction

#data16 16bit constant included in instruction

bit 8bit direct address of bit

rel Signed 8bit offset

addr16 16bit address

Directives are commands that are part of the assembler syntax but are not related to microcontroller instruction set. Assembler Directives are used as instruction to the assembler and are no translated to machine code during the assembly process. Some Instructions to Assembler: Address Control, Conditional Assembly, Memory Initialization and Reservation, Procedure Declaration, Program Linkage, Symbol Definition, etc. The following table explains the actions of certain directives

3

Page 6: Assembler Programming

Bit, Byte, Word, Binary, HEX…

Style Guidelines

EQU Define symbol DW Store word values in program memory

DATA Define internal memory symbol ORG Set segment location counter

IDATA Define indirect addressing symbol END End of assembly source file

XDATA Define external memory symbol CSEG Select program memory space

BIT Define internal bit memory symbol XSEG Select external data memory space

CODE Define program memory symbol DSEG Select internal data memory space

DS Reserve bytes of data memory ISEG Select indirectly addressed internal

DBIT Reserve bits of bit memory data memory space

DB Store byte values in program memory BSEG Select bit addressable memory space

0 1 0 0 1 1 0 1

1 0 0 1 1 1 1 0

Assembler Program and Machine Code

You can use the following style guidelines to improve the readability and understandability of your programs:

✓ Provide a Program Header: o Purpose of program o Register usage o Memory usage o Interrupts o Additional Hardware o Input and Output parameters o Example o Author

✓ Each major component or module of program should begin with a comment containing its purpose ✓ Each line must fit on the page ✓ Don’t explain obvious actions of an assembly language instruction: “complement bit” ✓ Start labels, opcode, operands, and comments in same column for each line ✓ Use meaningful symbolic names:

o Mixed upper and lower case for readability: Hex16bits_to_Ascii5bytes, Wait_Keypress… ✓ Subroutine comments:

o Name o Statement of what it does o List of its parameters and their roles o List of its arguments and return values

✓ Do not invent the wheel, use libraries: 32-Bit Math Routines for the 8051, INTEL

bit byte

word

4

Page 7: Assembler Programming

Clock

Delay of MOV A,#data. ADuC832 = 12 * System Clock. ADuC842 = System Clock

Example Program

In this example, a delay program of 0.01 sec will be implemented based in the formula: td = tf + tl + ta

where: td: Desired time tf: Fixed time tl: Loop time ta: Adjustment time using the reference program that is shows below

Assembler Program and Machine Code

The Microconverters from Analog Devices (based on 8051 core) have incorporated a PLL that converts 32.768 kHz watch crystal onto a multiple (512) of this to provide a stable 16,777,216 Hz clock for the system (ADuC842). One machine cycle is equivalent to one clock cycle (5.9605e-08 sec). The ADuC832 has a microcontroller core that is 8051 instructions set compatible. One machine cycle is equivalent in this case to 12 clock cycles (12 * 5.9605e-08 sec = 7.1526e-07).

Mnemonic Description Bytes Clock Periods Machine cycles Single-cycle core

MOV A, #data Move Immediate Data to Accumulator

2 12 (ADuC 832)

1

1 (ADuC 842)

The first step to develop a program is define the microcontroller core and output files, as it’s shown below

5

Page 8: Assembler Programming

Fixed Time

Adjust Time

Loop Time

Output in GPIO pin P0.7

// ----- Delay equation: td = (((3*NN + 2) + 3)*MM + 6 + 4 (LCALL))*tclock

DELAY_LOOP: MOV R1, #255 ; MOV R1,#MM (2 Cycles) LOOP_1: MOV R0, #217 ; MOV R0,#NN (2 Cycles) LOOP_0: DJNZ R0, LOOP_0 ; 3*NN Cycles DJNZ R1, LOOP_1 ; 3*MM Cycles ; Adjust time ;##################################################### MOV R1,#160 ; (2 Cycles) LOOP_2: DJNZ R1, LOOP_2 ; 3*160 = 480 Cycles ;##################################################### RET ; 4 Cycles

1

1

2 2

3

3

1

Assembler Program and Machine Code

2

3

% System frequency fclk = 16777216; % 16.78MHz % Desired time (10 msec) tdes = 0.01; % Clock cycle tcycle = 1/fclk; % Based on (Delay 0.01 sec) ==> ((3*NN + 2) + 3)*MM + 6 + 4 % Defining ==> MM = 255 NN = floor(((((10e-3/tcycle) - 10)/255)-5)/3) % Real Delay treal = (((3*NN + 2) + 3)*255 + 10) * tcycle; % Adjust time tadjust = tdes - treal; % Cycles to adjust Cycles_adjust = floor(tadjust / tcycle)

A program to compute the coefficients of loop time and adjust time is shows below

Being the result

1

NN = 217 Cycles_adjust = 482

6

Page 9: Assembler Programming

Assembler Program

Machine Code Tips

https://www.facebook.com/profile.php?id=100017837860698

Complement bit (Exclusive OR with 1)

01010000 XOR 01000101 00010101

Set bit (OR with 1)

01010000 OR 01000101 01010101

Reset bit (AND with 0)

01010000 AND 01000101 01000101

Divide by two (Rotate to the right)

00001000 (08H) 00000100 (04H)

Multiply by two (Rotate to the left)

01001000 (48H) 10010000 (90H)

Assembler Program and Machine Code

7