CMSC 216 Arduino (AVR)技术难点分析

贾蘅
• 阅读 147

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
1
Fortran). 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
2
5.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.
  3. Grading Criteria
    Your assignment grade will be determined with the following weights:
    Results of public tests 100%
    3
  4. 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.
    WX:codehelp
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
待兔 待兔
1年前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Easter79 Easter79
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Easter79 Easter79
3年前
SpringMvc接受特殊符号参数被转义
WEB开发时,在前端通过get/post方法传递参数的时候 如果实参附带特殊符号,后端接收到的值中特殊符号就会被转义例如该请求: http://localhost:10001/demo/index.do?name张三(1)注:中文()不会出现此种情况后台就收到的实际name值为:  张三&40;1&41;&40;其实为h
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
mysql数据库的查询
1、“查”——之单表查询INSERTINTOstudent2(name,grade,gender)VALUES('songjiang',40,'男'),('wuyong',100,'男'),('qinming',90,'男'),('husanniang',88,'女'),('sunerniang',66,'女'),('wus
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这