CS计算机代考程序代写 assembly algorithm assembler DT131B: Embedded Systems Programming

DT131B: Embedded Systems Programming
Lab Tutorial: Understanding Computer Organization and Architecture with AVR Microcontrollers.
OBJECTIVES: You shall
1. GetabetterunderstandingoftheorganizationoftheCPUandmemoryofthemicrocontroller 2. Getbetterunderstandingofthemachinelanguage
3. LearnhowtowritesimpleassemblylanguageprogramsfortheAtmelfamilymicrocontrollers 4. Learnhowtowriteprogramsinassemblylanguagetosolvesimpletasks
5. LearnhowtousegeneralpurposeI/O(input/output)ports.
Equipment and ICs:
• Lap top computer (students’ own) with Atmel Studio 6/7 or similar IDE that supports assembly language development for AVR/Atmel processors
Preparation
1. You must develop familiarity with the micro controller programming environment such as: a. UsingtheIDE(creatingprojects,usingtheeditor,etc.)
b. Compilinganddebuggingcodeinasimulator
c. Transferring and running code on target device.
2. You should have a general understanding of the architecture and register set of the ATmega328 and similar microcontrollers from Atmel.
3. Remember the main issues discussed during the computer organization and architecture lectures in the class.
Before coming to the lab, you should:
• Understand the basic concepts discussed in the lectures!!
• Download and install the IDE and familiarize yourself with it!!
• Read the sample codes given below
The website http://www.avr­asm­tutorial.net contains useful resources to learn assembly programming. There is also a good tutorial titled “Beginners Introduction to the Assembly Language of ATMEL­AVR­Microprocessors” by Gerhard Schmidt on this web site.
Theory:
Assembler codes translate one­to­one to executed machine codes. The processor needs only to execute what you want it to do and what is necessary to perform the task. No extra loops and unnecessary features bloat the generated code. If your program storage is limited and you have to optimize your program to fit into memory, assembly programming is your natural choice. Shorter programs are easier to understand and debug.
Because only necessary code steps are executed, assembly programs are faster than machine codes generated from high level languages. Since the duration of every instruction is known, the total execution time of a code section can be precisely calculated for tasks written in an assembler language. Time critical applications that require precise time measurements but have no hardware timer should be written in assembler.

The main disadvantage of assembly languages is that there are no mathematical functions (such as trigonometric, exponential, logarithmic, etc.) or APIs available at machine code level. Advanced arithmetic operations involving large integers and floating point numbers cannot be handled as easily as in high level languages. The effort for realizing such functions in assembly is unaffordably high.
Another limitation is that because assembly language is so closely tied to the underlying machine, one needs to have a good understanding of the architecture of the particular processor such as ALU architecture, register set details, data size, addressing modes and memory map and organization, etc.
Furthermore, assembly languages are hardware dependent. An assembly program written for one processor family does not work on another processor. However, in some cases, there may be a possibility to use cross­assemblers that translate a code written in one assembly language to another.
Procedure (for tutorial1)
1. Create a GCC­Assembly executable project in your IDE. Make sure that you specify the correct processor type for your project.
2. Write the sample codes given at the end of this document in your editor. Compile and run the codes in your IDE simulator.
3. Startthesimulatoranddebugyourcodeinit.
4. Studythememorymapofyourcompiledcodeandfindtherelationship/correspondencebetween
your assembly code and the content of the memory.
5. Debugthecodeinstepmodeandobservethecontentsofthememories,registersandIOportsas
you execute each instruction.
Tasks: Writing your own program
1. Explore the memory view and the I/O view for your microcontroller.
2. Examine the digital IO ports by reading/writing data to the port registers :
a. Write a program such that it:
i. Sets the value of register R19 to 55.
ii. Checks the content of R19.
iii. Displays 8 if the content of R19 > 50 or displays 0 otherwise.
b. Check the above code by changing the content of R19 to 45.
c. Write a short program to multiply two integers. Check the result on the I/O view of your IDE debugger if it is what you expected.
d. Modify your program to add two 16­bit integers from memory and store the result back to memory.
e. Repeat the above for 16­bit subtraction.
Challenge: Can you write a program to perform integer division? (since there is no division operation in your microcontroller’s ALU.

3.
4.
One of the important aspects of real­time applications is accurate timing of tasks. Write a code that produces a delay of 1msec. Explain how you estimated the duration of the loop. Repeat the above for delays of 10ms, 100ms, 1s, 10sec.
Suppose you have an array of integer data (say 10 numbers) in a memory
a. Write a program to find the largest of these numbers and show the location on the lab deck display
b. Repeat the above to find the sum of the same numbers.
Analysis:
In each of the above cases, what is the size of your program in bytes (or KB)? You should answer this for both the source code and the machine code.

Example Programs
You can test the sample codes below on your PC without having the target device. You can see the execution of the code on the simulator (you must install the Atmel Studio IDE!)
Example (with comments)
;This program stores integers in registers and adds the contents of two registers. It
;then stores the result into memory.
;The process is repeated for different register pairs: (R1, R2), (R17, R18), (R19, R20)
.include “m168pdef.inc” ;this file must correspond to the processor to be used
.list
tester:
ldi R17, 16
ldi R18, 32
mov R1, R17
mov R2, R18
add R1, R2
clr R27
ldi R26, 80
st x+, R19
sts 128, R1
ldi R17, 127
ldi R18, 126
add R17, R18
inc R26
st x+, R17
ldi R19, 254
ldi R20, 2
add R19, R20
inc R26
st x+, R19
;First R1+R2
;set R17=16 (0x10)
;set R18=32 (0x20)
;set R1 = R17
;set R2 = R18
;R1 = R1+r2
;clear higher byte of memory index register (=00)
;set lower index register to destination memory address
;store the sum in memory pointed by pair R27:R26
;store the sum (R1) at a pre-specified address (128)
;(Refer to the comments above)
;increment the index so that it points to the next memory location
;store the sum
;(Refer to the comments above)
;increment the index so that it points to the next memory location
;store the sum

Examples (without comments)
What does the following codes do? Draw the flow chart of the algorithm used in this example.
Sample 1:
.include “m168pdef.inc” .list
ldi R16, $5
ldi R18, 0
ldi R19, 5 looper:
add R18, R19 dec R16
brne looper
Sample 2:
.include “m168pdef.inc” .list
ldi R26,$10
ldi R19, 0
clearmem:
st x,R19
dec R26
cp R26,R19
brne clearmem
Sample 3:
.include “m168pdef.inc” used
.list
ldi R17, $ff
out DDRD, R17
ldi R18, $81
out PORTD, R18
;this file must correspond to the processor to be used
;this file must correspond to the processor to be used
;this file must correspond to the processor to be