30
University of Southern Queensland 1 1 /30 /30 ELE1301 Computer Engineering Faculty of Engineering and Surveying © The University of Southern Queensland, 2009 All Rights Reserved ELE1301 Computer Engineering Module 11c Assembly & Machine Language Programming ELE1301 Computer Engineering Module 11c Assembly & Machine Language Programming Semester 1 2009 Semester 1 2009 Mr Glenn Harris and Dr Wei Xiang Mr Glenn Harris and Dr Wei Xiang Faculty of Engineering & Surveying Faculty of Engineering & Surveying University of Southern Queensland University of Southern Queensland

micro controller

Embed Size (px)

Citation preview

Page 1: micro controller

University of Southern Queensland

11/30 /30

ELE1301 Computer Engineering

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved

ELE1301 Computer Engineering Module 11c

Assembly & Machine Language Programming

ELE1301 Computer Engineering Module 11c

Assembly & Machine Language Programming

Semester 1 2009 Semester 1 2009 Mr Glenn Harris and Dr Wei XiangMr Glenn Harris and Dr Wei Xiang

Faculty of Engineering & SurveyingFaculty of Engineering & SurveyingUniversity of Southern QueenslandUniversity of Southern Queensland

Page 2: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 22/30 /30

University of Southern QueenslandELE1301 Computer Engineering

ObjectivesObjectives

At the completion of this module you will be able to:

Describe the function of an unknown program and code it into machine code.

Demonstrate the use of the index register in assembly and machine program development.

Page 3: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 33/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 1Problem 1

Describe the function of the program and code it into machine code with the program starting at $0010

Label Field Mnemonic Field Operand Field Comment Field

ORG $0000

num3 FCB 03

num4 FCB 04

num5 FCB 05

ORG $0010

num1 FCB 01

num2 FCB 02

START LDX #$0004 Set the Xreg = 4

CLRA Clear Acc A

LOOP ADDA $00 ,X Add (M) to acc A, where M = (Xreg) + 0

DEX Decrement the Xreg for next M

BPL LOOP For M > 0000 go back to Loop

END JMP END Stop (endless loop)

Page 4: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 44/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 1 - SolutionProblem 1 - Solution

The flowchartLoad data

pointer

Start

End

Clear accumulator

Add data to accumulator

decrement data pointer

False

TruePoint > 0

Loop

Page 5: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 55/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 1 - SolutionProblem 1 - Solution

Simplified memory map

0000 01

0001 02

0002 03

0003 04

0004 05

0010 START

|

xxxx END

Data Section

Program Section

Page 6: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 66/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 1 - SolutionProblem 1 - Solution

Machine Code Assembly Language

0000 01 num1 FCB 01

0001 02 num2 FCB 02

0002 03 num3 FCB 03

0003 04 num4 FCB 04

0004 05 num5 FCB 05

CLRA Clear Acc A

LOOP ADDA $00 ,X Add (M) to acc A, where M = (Xreg) + 0

DEX Decrement the Xreg for next M

BPL LOOP For M > 0000 go back to Loop

END JMP END Stop (endless loop)

Memory Address

Machine Code

Label Field

Mnemonic Field

Operand Field

Comment Field

0010 START LDX #$0004 Set the Xreg = 4

Table formatted

Page 7: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 77/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 1 - SolutionProblem 1 - Solution

Machine Code Assembly Language

0000 01 num1 FCB 01

0001 02 num2 FCB 02

0002 03 num3 FCB 03

0003 04 num4 FCB 04

0004 05 num5 FCB 05

0013 4F CLRA Clear Acc A

0014 AB 00 LOOP ADDA $00 ,X Add (M) to acc A

0016 09 DEX Decrement the Xreg for next M

0017 2A FD BPL LOOP For M > 0000 go back to Loop

0019 7E 00 19 END JMP END Stop (endless loop)

Memory Address

Machine Code

Label Field

Mnemonic Field

Operand Field

Comment Field

0010 CE 00 04 START LDX #$0004 Set the Xreg = 4

Machine code added

Page 8: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 88/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - UsesIndex register - Uses

Lookup tables

Lists

Loops – counting and timing

Page 9: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 99/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register – Lookup tablesIndex register – Lookup tables

Lookup tables are useful in the generation or translation of data that cannot be calculated due to timing constraints.

Indexed addressing is well suited to keeping track of and accessing tables.

Using the indexed addressing mode an effective address is formed by adding an offset to the contents of the index register.

Page 10: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1010/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register – Lookup tablesIndex register – Lookup tables

In a lookup table sets of data are stored in such a way that the required data is selected by the offset.

Eg. Convert °C to °F (the offset represents °C)ie. LDAA $16 ,X will return 72°F for 22°C,

where IX = $1000

Memory Address

Table data (° F)

$

Offset $

1014 44 14

1015 46 15

1016 48 16

1017 50 17

Page 11: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1111/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register – Lookup tablesIndex register – Lookup tables

The advantage of this method is that no calculation of °C to °F is required.

The disadvantage is that fractional values are not available and more memory space being taken.

The program’s application will dictate whether or not a lookup table should be used rather than performing the calculation.

Page 12: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1212/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - ListsIndex register - ListsA list may be considered a one-dimensional array or table.

There are many ways to manipulate data in a list;

– Move the list to another location– Search the list– Sort the list– Delete items in the list

The index register can be used to track or find data within the list.

Page 13: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1313/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - LoopsIndex register - Loops

Counting – a loop counter is used to keep track of the number of times a loop is executed.

The loop counter can be a register or a memory address. The only requirement is that the we must be able to load, increment and decrement the counter.

For counting loops up to 25610 an 8-bit accumulator can be used, but for counts up to 6553510 the 16-bit index register is used.

Page 14: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1414/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - LoopsIndex register - Loops

A loop counter can either count up or down using the increment/decrement instructions.

It is more efficient to count down as the 68HC11 provides a ‘Z’ flag in the status register (CC) to indicate when an increment/decrement instruction produces a ‘zero’ count.

The alternative is to use a ‘compare’ instruction and then check the status register.

Page 15: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1515/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - LoopsIndex register - Loops

Timing – Looping is a good way to create a time delay in a program.

Time delays may be used to make a display blink or to space data transmissions.

From the 68HC11 instruction set it can be seen that each instruction in each addressing mode takes a precise number of machine cycles to execute.

Page 16: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1616/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - LoopsIndex register - LoopsUsing this information, a timing loop can be designed to produce the required time interval.Eg.If 1 cycle = 0.5us, calculate the time interval for the following code segment

LDAA #$05 2 cyclesCOUNT DECA 2 cycles

NOP 2 cyclesNOP 2 cyclesBNE COUNT 4 cycles

The Load instruction is executed once, ie 1usec. The loop time is 0.5us x 10 cycles = 5usec for each pass, therefore executing theloop 5 times produces a time interval of 25usec.Add to this the initialisation time of 1usec, the total time is 26usec.

Page 17: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1717/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - LoopsIndex register - LoopsThe previous example used an 8-bit accumulator as the loop counter, so the maximum count is limited to 25610.

Using the index register as the loop counter, counts up to or down from 6553510 are possible. Try the following example, using 0.5us/cycle. (ans ≈ 0.3sec)

LDX #65535 3 cyclesCOUNT DEX 3 cycles

NOP 2 cyclesBNE COUNT 4 cycles

Note: The load instruction becomes less significant as the time interval increases.

Page 18: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1818/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Index register - LoopsIndex register - LoopsNested timing loops – Nesting the 0.3 second timing loop inside a counting loop of 5 will produce a time interval of about 1.5 seconds.

The following code segments shows how this is done.

LDAA #5LOOP1 LDX #65535LOOP2 DEX

BNE LOOP2DECABNE LOOP1

Page 19: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 1919/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 2Problem 2

Develop a program (starting at $1000) that will provide a 0.3 second time delay, given:

– Operating frequency, f = 1MHz

– Since machine cycle time (T) is 1/f, then T = 1usec

Page 20: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2020/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 2 - SolutionProblem 2 - Solution

Flowchart

Counter - 1

Start

Set Counter

Counter = 0

End

True

False

Loop

Page 21: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2121/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 2 - SolutionProblem 2 - Solution

Memory map

0000 Reserved for

| I/O registers

0FFF

1000 START

|

|

|

1008 END

Reserved Section

Program Section

Page 22: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2222/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 2 - SolutionProblem 2 - SolutionAssembly language program. Counter value will need to be calculated once the number of machine cycle in the loop are known.

LDX = 3 cyclesDEX = 3 cyclesBNE = 3 cycles

Machine Code Assembly Language

LOOP DEX Decrement the counter

BNE LOOP For X ≠ 0000 go back to Loop

END JMP END Stop (endless loop)

Memory Address

Machine Code

Label Field

Mnemonic Field

Operand Field

Comment Field

1000 START LDX #$? Counter

Page 23: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2323/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 2 - SolutionProblem 2 - Solution

Loop time = number of cycles in the loop x cycle time

ie. 0.3 = (6y + 3) x 1usecy = 49999.510 (round off to 50000)

= C35016

since the LDX instruction is only performed once it contributes very little to the delay and can be ignored.

Page 24: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2424/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 2 - SolutionProblem 2 - Solution

Machine code program

Machine Code Assembly Language

1003 09 LOOP DEX Decrement the counter

1004 26 FD BNE LOOP For X ≠ 0000 go back to Loop

1006 7E 10 06 END JMP END Stop (endless loop)

Memory Address

Machine Code

Label Field

Mnemonic Field

Operand Field

Comment Field

1000 CE C3 50 START LDX #$C350 Counter

Page 25: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2525/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 3Problem 3

Develop a program that will provide a 3 second time delay, given:

– Operating frequency, f = 1MHz

– Since machine cycle time (T) is 1/f, then T = 1usec

Page 26: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2626/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 3 - SolutionProblem 3 - Solution

Flowchart

Counter2 - 1

Start

Set Counter2

Counter2 = 0

End

True

False

Loop2

Set Counter1Loop1

Counter1-1

Counter1 = 0

False

True

Loop 1 = 10 times

Loop 2 = 0.3 sec

Page 27: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2727/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 3 - SolutionProblem 3 - Solution

Memory map

0000 Reserved for

| I/O registers

0FFF

1000 START

|

|

|

100D END

Reserved Section

Program Section

Page 28: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2828/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 3 - SolutionProblem 3 - SolutionAssembly language program. Counter value will need to be calculated once the number of machine cycle in both loops are known.

Machine Code Assembly Language

1000 START LDAA #$0A Set counter1 to 10

DECA

BNE LOOP1 For A ≠ 0000 go back to Loop1

LOOP2 DEX Decrement the counter2

BNE LOOP2 For X ≠ 0000 go back to Loop2

END JMP END Stop (endless loop)

Memory Address

Machine Code

Label Field

Mnemonic Field

Operand Field

Comment Field

LOOP1 LDX #$? Set counter2

Page 29: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 2929/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 3 - SolutionProblem 3 - SolutionMachine cycles– Loop1: LDAA = 2, DECA = 2, BNE = 3– Loop2 : LDX = 3, DEX = 3, BNE = 3

Loop time = cycle time x number of cycles inloops 1 and 2

ie. 3 = [(6y + 7) x 1us] 10y = 4998610

= C34216

The value for y is a little less than the previous example due to additional instructions in the outer loop.

Page 30: micro controller

Faculty of Engineering and Surveying© The University of Southern Queensland, 2009 All Rights Reserved 3030/30 /30

University of Southern QueenslandELE1301 Computer Engineering

Problem 3 - SolutionProblem 3 - Solution

Machine code program

Machine Code Assembly Language

1000 86 0A START LDDA #$0A Set counter1 to 10

1008 4A DECA

1009 26 F7 BNE LOOP1 For A ≠ 0000 go back to Loop1

1005 09 LOOP2 DEX Decrement the counter2

1006 26 FD BNE LOOP2 For X ≠ 0000 go back to Loop2

100B 7E 10 0B END JMP END Stop (endless loop)

Memory Address

Machine Code

Label Field

Mnemonic Field

Operand Field

Comment Field

1002 CE C3 42 LOOP1 LDX #$C342 Set counter2