AVR and MeggyJr Simple

Similar documents
Plan. Regression testing: Demo of how to use the regress.sh script.

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I)

Course Administration

CS453 CLASSES, VARIABLES, ASSIGNMENTS

By: Dr. Hamed Saghaei

COMP2121: Microprocessors and Interfacing

CS453 Arrays in Java in general

Data Transfer Instructions

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

CS453 Visitor patterns Type checking and Code Generation

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

Implementing Classes, Arrays, and Assignments

Chapter 5. A Closer Look at Instruction Set Architectures

ก AVR Microcontrollers

CS453 Compiler Construction

Assembly Programming (III)

Lectures 3-4: MIPS instructions

Implementing Classes and Assignments

ECE 471 Embedded Systems Lecture 5

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

ECE232: Hardware Organization and Design

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

PESIT Bangalore South Campus

ECE 375 Computer Organization and Assembly Language Programming Winter 2018 Solution Set #2

CN310 Microprocessor Systems Design

Using SRAM in AVR assembler language

EE 308: Microcontrollers

CS401 Assembly Language Solved MCQS From Midterm Papers

Understand the factors involved in instruction set

Chapter 5. A Closer Look at Instruction Set Architectures

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Thomas Polzer Institut für Technische Informatik

COMP2121: Microprocessors and Interfacing. Instruction Formats and Addressing Modes

COSC 6385 Computer Architecture. Instruction Set Architectures

Lecture 4: MIPS Instruction Set

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 11 Instruction Sets: Addressing Modes and Formats

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

EC 413 Computer Organization

Lecture 20: AVR Programming, Continued. AVR Program Visible State (ones we care about for now)

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

Practical Malware Analysis

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Subprograms, Subroutines, and Functions

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

CHAPTER 8: Central Processing Unit (CPU)

CPE 323 MSP430 INSTRUCTION SET ARCHITECTURE (ISA)

AVR Subroutine Basics

Lecture 5. Announcements: Today: Finish up functions in MIPS

Chapter 2A Instructions: Language of the Computer

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

Computer Architecture and System Software Lecture 07: Assembly Language Programming

COSC345 Software Engineering. Basic Computer Architecture and The Stack

Chapter 5. A Closer Look at Instruction Set Architectures

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Chapter 5. A Closer Look at Instruction Set Architectures

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

Assembler. Lecture 8 CS301

Lecture 4: Instruction Set Architecture

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

CS61C : Machine Structures

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

COMP3221: Microprocessors and Embedded Systems

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Instruction Set Architecture

Lecture 4: Instruction Set Design/Pipelining

COMPUTER ORGANIZATION AND DESIGN

Digital System Design Using Verilog. - Processing Unit Design

ECE 372 Microcontroller Design Assembly Programming. ECE 372 Microcontroller Design Assembly Programming

MIPS Instruction Set Architecture (2)

EC-801 Advanced Computer Architecture

S3.0 : A multicore 32-bit Processor

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

ECE 15B Computer Organization Spring 2010

ECE 30 Introduction to Computer Engineering

CS 2504 Intro Computer Organization Test 1

Computer Systems Lecture 9

APPENDIX A FOR SKEE3732 LABORATORY 1 SHEET

Trap Vector Table. Interrupt Vector Table. Operating System and Supervisor Stack. Available for User Programs. Device Register Addresses

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

55:132/22C:160, HPCA Spring 2011

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Procedures and Stacks

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

In Java we have the keyword null, which is the value of an uninitialized reference type

Transcription:

AVR and MeggyJr Simple Today Finish AVR assembly especially for PA3ifdots.java Meggy Jr Simple run-time library CS453 Lecture AVR Assembly and Meggy Jr Simple 1

AVR Instruction Set Architecture, or Assembly ATmega328p Why assembly? AVR ISA Handling GetButton and SetPixel calls, (Calling Convention) Handling if statements (Condition Codes and Branches) Handling expression evaluation (Operations and Stack instructions) Variables on the stack and in the heap CS453 Lecture AVR Assembly and Meggy Jr Simple 2

Meggy Java program for translation to AVR (expression eval) /**! * PA3ifdots.java! *! * An example for the students to code up in AVR assembly for PA1.! * The language features will be from the PA3 grammar.! */! import meggy.meggy;! class PA3ifdots {! }! public static void main(string[] whatever){! if (Meggy.checkButton(Meggy.Button.Up)) {! Meggy.setPixel( (byte)3, (byte)(4+3), Meggy.Color.BLUE );! }! }! if (Meggy.checkButton(Meggy.Button.Down)) {! Meggy.setPixel( (byte)3, (byte)0, Meggy.Color.RED );! }! CS453 Lecture AVR Assembly and Meggy Jr Simple 3

Arithmetic: bytes and ints AVR is an 8 bit architecture, but has support for 16 bit ints. This is accomplished by having register pairs, and having certain instructions taking certain flags into account: # add r1:r0 to r3:r2 add r2,r0 # Rd = Rd + Rr sets C adc r3,r1 # Rd = Rd + Rr + C Subtraction: check out sub and sbc Multiplication: check out muls Bitwise AND: check out and CS453 Lecture AVR Assembly and Meggy Jr Simple 4

Meggy Java program for translation to AVR (expression eval) /* PA5movedot.java */!! return ((byte)(0-1) < x)!!!.file! PA5movedot.java"! # Load constant int 0! ldi r24,lo8(0)! ldi r25,hi8(0)! # push two byte expression onto stack! push r25! push r24! # Load constant int 1! ldi r24,lo8(1)! ldi r25,hi8(1)! # push two byte expression onto stack! push r25! push r24! # load a two byte expression off stack! pop r18! pop r19!! # load a two byte expression off stack! pop r24! pop r25!! # Do INT sub operation! sub r24, r18! sbc r25, r19! # push hi order byte first! # push two byte expression onto stack! push r25! push r24!! # Casting int to byte by popping! # 2 bytes off stack and only push low bits! # back on. Low bits are on top of stack.! pop r24! pop r25! push r24! CS453 Lecture AVR Assembly and Meggy Jr Simple 5

Variables on the Stack and Heap text data Registers PC ldi add sub heap r0 r1 r2 r3 stack stack pointer r29:r28 r31 ALU CS453 Lecture AVR Assembly and Meggy Jr Simple 6

Stack and heap Stack pointer: points at first available location on the run time stack varies during expression evaluation Frame pointer: a fixed pointer in the stack frame so that parameters and local variables can be associated with an offset from the frame pointer Allocating space on the heap with malloc library function: malloc allocates n consecutive bytes in the heap and returns the address of the first byte allocated. (Will see examples of this later). CS453 Lecture AVR Assembly and Meggy Jr Simple 7

Data Indirect addressing Some register pairs are used for indirect addressing. There are special names for these Indirect Address Registers X=R27:R26, Y=R29:R28, Z=R31:R30 in r28, SP_L in r29, SP_H!! // putting the stack pointer into r29:r28!! ldd r24, Y+3 // load byte that is 3 bytes from address in r29:r28! // r24 = M[r29:r28 + 3]!! std Y+1, r24 // store value in r24 to address r29:r28+1! // M[r29:r28 + 1] = r24! There are pre-decrement and post-increment indirect addressing modes for data structure (Stack) manipulation The run time stack is implicitly manipulated with (push) and (pop) instructions, SP is the name of the stack pointer CS453 Lecture AVR Assembly and Meggy Jr Simple 8

Meggy Jr Simple Library Key concepts LED screen (pixels) Auxiliary LEDs Buttons Speaker Check the AVR-G++ generated code for library calls, and their calling sequence. AVR-G++ (and also MeggyJava) links in run time libraries: Meggy Jr Library provided an interface to set and read values in the Display Memory Meggy Jr Simple lies on top of Meggy Jr library, and provides a higher level API with names for e.g. colors Michelle Strout and students (honors projects / theses) added some functionality to the Meggy Jr Simple library CS453 Lecture AVR Assembly and Meggy Jr Simple 9

Meggy Jr Simple Library functions ClearSlate() DrawPx(x,y,color) DisplaySlate() SetAuxLEDS(value) -- erase the whole slate -- set pixel (x,y) to color -- copy slate to LED Display Memory -- 8 LEDS above screen numbered 1, 2,4,..,128 (left to right) value is a byte encoding in binary which LEDs are set SETAuxLEDS(53) sets LEDS 1,4,16, and 32 ReadPx(x,y) -- returns byte value of pixel (x,y) CheckButtonsDown() -- sets 6 variables: Button_(A B Up Down Left Right) GetButtons() returns a byte (B,A,Up,Down,Left.Right: 1,2,4,8,16,32) ToneStart(divisor, duration) -- starts a tone of frequency 8 Mhz/divisor for ~duration milliseconds There are predefined tones. Check out MeggyJrSimple.h CS453 Lecture AVR Assembly and Meggy Jr Simple 10

Example AVR-G++ program /* 1/24/11, MS, goal is to exercise all of the routines in MeggyJrSimple */! #include "MeggyJrSimple.h"! #include <util/delay.h>! int main (void) {! }! MeggyJrSimpleSetup();! DrawPx(0, 1, Red); // should display red LED! DisplaySlate();! // If <0,1> pixel is red, set auxiliary light! if (ReadPx(0,1)==Red) { SetAuxLEDs (4); }! while (1){! CheckButtonsDown();! if (Button_A) { Tone_Start(ToneC3, 1000); }! if (Button_B) { SetAuxLEDs(16); }! if (4 & GetButtons()) { SetAuxLEDs(31); } //! if (Button_Up) { delay_ms(256); }! }! return 0;! CS453 Lecture AVR Assembly and Meggy Jr Simple 11

Mapping Meggy Java Interface to Meggy Simple Interface Let s look at some examples of how this works. CS453 Lecture AVR Assembly and Meggy Jr Simple 12

Before Next Time Finish PA1 and try checking it in online. It is due in 5 days! Check out the discussion board on Canvas. CS453 Lecture AVR Assembly and Meggy Jr Simple 13