Assembly Language with GCC

Slides:



Advertisements
Similar presentations
Assembly Language and Arduino
Advertisements

The University of Adelaide, School of Computer Science
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
The 8051 Microcontroller and Embedded Systems
UEE072HM Linking HLL and ALP An example on ARM. Embedded and Real-Time Systems We will mainly look at embedded systems –Systems which have the computer.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Computer Architecture CSCE 350
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Embedded Systems 7763B Mt Druitt College of TAFE Electrical Engineering Lesson 2 LCD Display Interfacing.
Butterfly I/O Ports CS-212 Dick Steflik. I/O for our labs To get data into and out of our Butterfly its a little trickier than using printf and scanf.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Program Development Tools Programming Editors Many good editors for programming exist. Two are available most anywhere. -vi, emacs vi (pronounced “vee.
External & internal Interrupts. Interrupt Sources There are 21 different interrupts and each one has its own vector located in a predefined location at.
Tutorial Essentially all the Blackfin instruction you need for all of ENCM511. The instructions are easy. Its knowing when to use them that is the difficult.
 Speed  Not affected by compiler optimization.  r0  r18-r25  r25-r27 (X)  r30-r31 (Z)  r1 (must be cleared before returning)
Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
Embedded Systems 7763B Mt Druitt College of TAFE
RM2D Let’s write our FIRST basic SPIN program!. The Labs that follow in this Module are designed to teach the following; Turn an LED on – assigning I/O.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Program Development Tools GNU make (much of this material is adapted from “GNU Make” by Richard Stallman) The make utility automatically determines which.
CE-2810 Dr. Mark L. Hornick 1 Mixing C and assembly Safety goggles on!
Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.
Serial Peripheral Interface SPI I2C (i-squared cee)
SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers.
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club.
“ INSTRUCTIONS SET OF AVR MICROCONTROLLER ” SIGMA INSTITUTE OF ENGINEERING Prepared By: SR.NO NAME OF STUDENT ENROLLMENT 1 Abhishek Lakhara
Writing Functions in Assembly
INTRODUCTION TO AVRASSEMBLY PROGRAMMING
Embedded Systems Programming
ECE 382 Lesson 22 Readings Lesson Outline Writing Clean Code
Lecture 5: Procedure Calls
Introduction to Smart Systems
Embedded Systems Programming Examples and Comparison
Overview of Architecture Assembly Programming Concepts
Tutorial Essentially all the Blackfin instruction you need for all of ENCM511. The instructions are easy. Its knowing when to use them that is the difficult.
The University of Adelaide, School of Computer Science
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
COMP2121: Microprocessors and Interfacing
Microprocessor and Assembly Language
Writing Functions in Assembly
High-Level Language Interface
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
ATmega103 Timer0 and Interrupts
CENG2400 Revision Q1a A system has an ARM processor with a 32-bit General Purpose Input Output (GPIO) module. Two on/off switches are connected to bit-3.
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
LHO 15 C with assembly language
A Play Core Timer Interrupts
Tim Sumner, Imperial College, Rm: 1009, x47552
Chapter 11 Interfacing C and Assembly Code
Assembly Language Programming II: C Compiler Calling Sequences
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
MIPS Procedure Calls CSE 378 – Section 3.
AVR Programming in C Chapter 7
Tim Sumner, Imperial College, Rm: 1009, x47552
AVR Programming in C Chapter 7
COMP3221: Microprocessors and Embedded Systems
Logical and Decision Operations
ADC and DAC Programming in AVR
Multi-modules programming
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Lecture 6: Assembly Programs
8051 ASSEMBLY LANGUAGE PROGRAMMING
Computer Architecture
The ‘asm’ construct An introduction to the GNU C/C++ compiler’s obscure syntax for doing inline assembly language.
Presentation transcript:

Assembly Language with GCC Why use assembly language? -High level of control of code generation (don’t let the optimizer interpret) -Speed -Awkward C implementations (e.g., 16/24 bit SPI xfer for AD9851) How is it done? Two ways... -Inline assembly -assembly instructions written directly in the C code file -downright weird syntax except for really simple stuff, e.g: asm volatile ("nop"); //inline assembly code, add a nop asm volatile("sbi 0x18,0x07;"); //set some bits -Separate assembly code file -.S file contains only assembly instructions -just like writing assembly language programs but assembled and linked like another C file.

Assembly Language with GCC Separate assembly code files Discussing assembly language function calls here Questions?.... -How do we pass in arguments to the function (where do they go?) -How does the function pass back the return value (where is it?) -What registers can be uses without saving? -What registers must be saved? Registers that can be used without saving first: -r0, r1 (r1 must be cleared before returning) -r18-r25, -r26-r27 (X reg) -r30-r31 (Z reg) From the compiler’s view, these call used registers can be allocated by gcc for local data. They may be used freely in subroutines.

Assembly Language with GCC Separate assembly code files Rules for r0 and r1 r1 maybe freely used within a function but it must be cleared before returning. “clr r1”. ISRs save and clear r1 upon entering, and restore r1 upon exit in case it was non-zero at exit. r1 is assumed to be zero in any C code space. r0 can be clobbered by any C code, its a temp register. It may be used "for a while" (from the documentation!) within the function call.

Assembly Language with GCC Separate assembly code files Registers that must be saved and restored by subroutines r2 – r17 r28 - r29 Function call conventions – arguments Arguments are allocated left to right, r25 to r18 All args are aligned to start in even numbered registers. Odd sized arguments like char, have one free register above them. If there are too many arguments, those that don’t fit in registers are passed on the stack.

Assembly Language with GCC Argument and value register convention for GCC r25 r24 r23 r22 r21 r20 r19 r18 ........... uint8_t uint8_t uint8_t uint16_t uint8_t uint16_t uint32_t uint16_t arguments passed in value passed back uint8_t uint16_t uint32_t uint64_t

Assembly Language with GCC Example: SPI function call in assembly language //sw_spi.S, R. Traylor, 12.1.08 #include <avr/io.h> .text .global sw_spi //define the pins and ports, using PB0,1,2 .equ spi_port , 0x18 ;PORTB .equ mosi , 0 ;PB2 pin .equ sck , 1 ;PB0 pin .equ cs_n , 2 ;PB1 pin //r18 counts to eight, r24 holds data byte passed in sw_spi: ldi r18,0x08 ;setup counter for eight clock pulses cbi spi_port, cs_n ;set chip select low loop: rol r24 ;shift byte left (MSB first); carry set if bit7 is one brcc bit_low ;if carry not true, bit was zero, not one sbi spi_port, mosi ;set port data bit to one rjmp clock ;ready for clock pulse bit_low: cbi spi_port, mosi ;set port data bit to zero clock: sbi spi_port, sck ;sck -> one cbi spi_port, sck ;sck -> zero dec r18 ;decrement the bit counter brne loop ;loop if not done sbi spi_port, cs_n ;dessert chip select to high ret ; .end

Assembly Language with GCC Example: SPI function call in assembly language // assy_spi.c #define F_CPU 16000000UL //16Mhz clock #include <avr/io.h> #include <util/delay.h> //declare the assembly language spi function routine extern void sw_spi(uint8_t data); int main(void) { DDRB = 0x07; //set port B bit 1,2,3 to all outputs while(1){ sw_spi(0xA5); //alternating pattern of lights to spi port sw_spi(0x5A); } //while } //main

Assembly Language with GCC Example: SPI function call in assembly language PRG = assy_spi OBJ = $(PRG).o sw_spi.o MCU_TARGET = atmega128 OPTIMIZE = -Os # options are 1, 2, 3, s DEFS = LIBS = CC = avr-gcc #override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) override LDFLAGS = -Wl, -Map, $(PRG).map OBJCOPY = avr-objcopy OBJDUMP = avr-objdump all: $(PRG).elf lst text eeprom $(PRG).elf: $(OBJ) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) clean: rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak rm -rf *.lst *.map $(EXTRA_CLEAN_FILES) *~ program: $(PRG).hex sudo avrdude -p m128 -c usbasp -e -U flash:w:$(PRG).hex lst: $(PRG).lst %.lst: %.elf $(OBJDUMP) -h -S $< > $@