首页 > > 详细

辅导CMSC 216、AVR留学生讲解、辅导C++/Java、讲解C++/Java编程语言 辅导Web开发|讲解R语言编程

CMSC 216 Arduino (AVR) Assembly Basics Exercise Spring 2019
Assembly Basics Exercise Due: Fri, Apr 12, 11:30 PM
1 Objectives
The goal of this exercise is to start to prepare you to write your own functions using assembly. Assembly
has a different vocabulary from C and Java (instructions on registers instead of expressions on variables), and
a different structure (branches and labels instead of conditionals and blocks). This exercise is mostly about
practicing the vocabulary of loads, compares, and branches, and about following the calling convention.
It can be tricky to put together a working assembly function without knowing the useful instructions first. We
will provide the high-level structure of some simple routines; all you have to do is fill in the actual instructions
that accomplish the goal.
In this exercise, we will fill in a template, one (or maybe two) instruction(s) at a time. It will be up to you to
use the documentation and our in-class examples to find the appropriate assembly instructions and operands
to perform the task described in the template.
2 Context: Use of Assembly in Practice
Assembly language is used in two key situations. First, when a machine first starts up, assembly language
routines read in the operating system. You might call this the “boot loader”. Second, when software needs to
interact with hardware, it uses special instructions (in, out) or special registers that aren’t exposed to C code.
Here, we’re working in the context of a C program, where these routines will be called from C, but we won’t
do anything the compiler couldn’t.
You’ll also see “hand tuned” assembly called from C when a routine needs to be optimized for size, speed,
or to ensure specific timing (see neopixel_blit.c). Only extremely important or frequently used code gets
this treatment, but on small microcontrollers without much memory for instructions, optimization for size is
common.
3 Template Style
You will fill in the code in a template that uses the following conventions.
Comments led by three semicolons are descriptive background. They represent context, or show the prototype
of the function as it will be called by C code. Comments led by two semicolons represent an instruction you’re
meant to fill in. Leave all template comments in your solution. You may add comments after the line of code
using a single semicolon.
Labels are already included. Labels represent the beginnings of functions and branch targets. Numeric labels
are branch targets and need not be unique because the assembler will find the nearest label having that
number. For example, brge 1f will search “forward” for the next label “1”.
The “.global” declarations tell the assembler that the label should be exported, which allows the linker to
connect a function call in the C driver file to this function implementation. You can think of it like “extern” in
C, but we’ve provided it for you in the template. We can use directives like these to declare global variables
as well.
In some cases, the comment describing what to do will require two related instructions (e.g., add/adc, cp/brge,
clr/clr) to complete. It is permitted, but not recommended, to deviate somewhat from these instructions, or
even to find one instruction that accomplishes two-comments-worth of the exercise.
Assembly is generally formatted so that labels are flush-left and instructions are indented by a “tab”. Indentation
makes the labels more visible. The assembler, unlike make, does not need a literal tab character;
indentation can be accomplished with spaces and indentation is optional to the language (unlike Python or
1Fortran). Emacs “assembler-mode” will try to help with this formatting; other editors may do the same.
4 Documentation
The key reference documentation for the AVR instruction set we use is the instruction set manual. The summary
datasheet contains a quick table “instruction set summary” of instructions that will likely be sufficient
most of the time.
? Overview of documentation on Microchip.com
? Summary Datasheet - Keep pages 13-15 in a window while you work.
? Instruction Set Manual - Search for a complete description of each instruction you might want to use.
We aren’t using microchip’s assembler, so examples on that site are likely to use different assembler directives
and aren’t likely to assemble with avr-gcc. An advantage of using the GNU assembler is that you may be able
to reuse the syntax (though not the instructions) when writing assembly for other processors.
? AVR GCC Wiki Documentation - The ABI section is descriptive of conventions for register use. Ignore
the “reduced tiny” processor; we are ’mega’ or ’enhanced’.
? AVR GCC Wiki Documentation on Calling Convention
? GNU Assembler Manual
? GNU Assembler on the .global directive
? GNU Assembler on lo8 and hi8
? AVR-libc annotated example
5 Functions
5.1 Five
This function will return the value “5” as a 16-bit integer. You will need to figure out where the return value
belongs and how to load an immediate (constant) into it.
1 ;;; Five
2 .global Five
3 Five:
4 ;;; uint16_t Five(): return 5 as a uint16_t
5 ;; load immediate
6 ;; load immediate or clear
7 ;; return
Note that until you fill in the return instruction, the processor will just keep going, kind of like a switch/case
without a break in C.
5.2 Max
This implementation of Max relies on the first parameter having the same location as the return value.
1 ;;; Max
2 .global Max
3 Max:
4 ;;; uint8_t max(uint8_t x, uint8_t y): return the greater of the arguments
5 ;; compare x to y
6 ;; if x >= y, branch 1f
7 ;; copy y into return value / first param slot.
8 1:
9 ;; return
25.3 Strlen
Note that the return value is a 16-bit integer and a pointer is a 16-bit value. It will sometimes take two
instructions to alter values.
1 ;;; Strlen
2 .global Strlen
3 Strlen:
4 ;;; uint16_t Strlen(char *arg)
5 ;; copy argument to X (r27:26) pointer
6 ;; initialize return value to zero
7 2:
8 ;; load X with post-increment
9 ;; if loaded value was zero, branch 1f (label 1, forward)
10 ;; increment return value
11 ;; jump 2b (label 2, backward)
12 1:
13 ;; return
6 Using the simulator and debugger
The makefile provides useful rules for executing the simulator and debugger.
To run the exercise in the simulator:
% make exercise.run
This should be equivalent to running “simavr exercise”, but includes some command line arguments that
should be redundant on grace.
To run the exercise in the simulator, but configure it to wait for avr-gdb (gdb compiled to debug AVR code) to
connect so that you can single-step, use:
% make exercise.gdb
You’ll then want to use typical gdb commands, like “break Five”, “break Strlen”, “continue”, “step”, etc.
Particularly useful to us is “info registers”.
Note that gdb doesn’t expect the Harvard architecture used by the AVR, so addresses above 0x800000 are
really just memory addresses in the normal 16-bit range. This is just to separate the data memory address
space from the instruction memory address space. This is why, when running “info registers” you’ll see the
stack pointer (SP) has a value like “0x800aef”. It is really just “0xaef” but in data memory, not in instruction
memory.
7 Submit Server Time
This project is due at 11:30 pm. You will notice that in the submit server the due time is set to 11:31 pm. This
is because a submission that is done exactly at 11:30 pm is considered late by the submit server.
8 Submitting your assignment
1. Use the submit command as if you were submitting a class project.
2. Your assignment must be electronically submitted by the date and time above to avoid losing credit. See
the course syllabus for details.
9 Grading Criteria
Your assignment grade will be determined with the following weights:
Results of public tests 100%
310 Academic integrity statement
Please carefully read the academic integrity section of the course syllabus. Any evidence of impermissible
cooperation on assignments, use of disallowed materials or resources, or unauthorized use of computer
accounts, will be submitted to the Student Honor Council, which could result in an XF for the course, or
suspension or expulsion from the University. Be sure you understand what you are and what you are not
permitted to do with regard to academic integrity when it comes to assignments. These policies apply to all
students, and the Student Honor Council does not consider lack of knowledge of the policies to be a defense
for violating them. Full information is found in the course syllabus – please review it at this time.
4

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!