Linking Assembly Subroutine with a C Program

Similar documents
Lecture 36 April 25, 2012 Review for Exam 3. Linking Assembly Subroutine with a C Program. A/D Converter

Microcontrollers. Microcontroller

Sample Problem Set #1

MC9S12 Assembler Directives A Summary of MC9S12 Instructions Disassembly of MC9S12 op codes. Summary of HCS12 addressing modes ADDRESSING MODES

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3. You will be able to use all of the Motorola data manuals on the exam.

EE319K Final Fall 2005 Solution C. (3) Question 1. (3) Question 2. short function(const short in){ return in+5; } const

Introduction to the 9S12 Microcontroller

Introduction to the MC9S12 Hardware Subsystems

CodeWarrior. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

(Embedded) Systems Programming Overview

Lecture 9 Subroutines

Exam 1 Feb. 23, 25, 27?

2. Arithmetic Instructions addition, subtraction, multiplication, divison (HCS12 Core Users Guide, Sections 4.3.4, and ).

Lab 7: Asynchronous Serial I/O

History of the Microprocessor. ECE/CS 5780/6780: Embedded System Design. Microcontrollers. First Microprocessors. MC9S12C32 Block Diagram

Introduction to Embedded Systems and Chapter 1: Introduction to HCS12/MC9S12. EE383: Introduction to Embedded Systems University of Kentucky

Decimal, Hexadecimal and Binary Numbers Writing an assembly language program

Using the stack and the stack pointer

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

Coe538 Final Study Guide 2016 (Questions & Answers)

Comparison of C and Assembly How to compile a C program using CodeWarrior

Microcontrollers and the Freescale/Motorola HC11

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

Lab 1 MC9S12 Assembler and Monitor

ME4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 7

Programming the Motorola MC68HC11 Microcontroller

1. Memory Mapped Systems 2. Adding Unsigned Numbers

Lecture #4 Microcontroller Instruction Set Embedded System Engineering Philip Koopman Monday, 25-Jan-2016

Most of the HC12 s instructions access data in memory There are several ways for the HC12 to determine which address to access

Chapter 4: Advanced Assembly Programming. EE383: Introduction to Embedded Systems University of Kentucky. Samir Rawashdeh

Microcontrollers. 2IN60: Real-time Architectures (for automotive systems) Mike Holenderski,

Computer Systems Lecture 9

538 Lecture Notes Week 5

; export symbols ; export 'Entry' symbol. ; include derivative specific macros PORTA EQU $0000 PORTB EQU $0001 DDRA EQU $0002 DDRB EQU $0003

MIGRATING TO THE 68HC12 IN C

; export symbols XDEF Entry ; export 'Entry' symbol ABSENTRY Entry ; for assembly entry point

538 Lecture Notes Week 5

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

VLSI Design Lab., Konkuk Univ. Yong Beom Cho LSI Design Lab

ECE331 Handout 3- ASM Instructions, Address Modes and Directives

CS/ECE 5780/6780: Embedded System Design

PIC Microcontroller and

Lab 1 MC9S12 Assembler and Monitor

Design of Embedded Systems Using 68HC12/11 Microcontrollers

AVR Training Board-I. VLSI Design Lab., Konkuk Univ. LSI Design Lab

Lab 2 Part 1 Assembly Language Programming and 9S12 Ports

MC9S12 Address Space

Wed. Aug 23 Announcements

Exam I Review February 2017

ARM Architecture and Assembly Programming Intro

ECE 367 -Experiment #1 Fall 2012

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

Quick Guide to Using Code Warrior

Fuzzy Logic Controllers. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3

ECE 331: PC Lab 3 Stack and Subroutines

CHAPTER 8. Solutions for Exercises

EE 308 Spring The HCS12 has 6 addressing modes

Somes French translations :

Department of Electronics and Instrumentation Engineering Question Bank

ARM Cortex-M4 Programming Model

EE 308: Microcontrollers

Comparison of C and Assembly How to compile a C program using CodeWarrior

A B C D E F 0480 FE B F5 3B FC F3 E 1A 1D 2A 2D 3A 3D 4A 4D 5A 5D 6A 6D 7A 7D

ECE 471 Embedded Systems Lecture 2

Chapter 2: HCS12 Assembly Programming. EE383: Introduction to Embedded Systems University of Kentucky. Samir Rawashdeh

C programming for embedded microcontroller systems.

Ryerson University Department of Electrical and Computer Engineering ELE 538 Microprocessor Systems Final Examination December 8, 2003

ECE3120: Computer Systems Hardware & Software Development Tools

Ali Karimpour Associate Professor Ferdowsi University of Mashhad

CMS-8GP32. A Motorola MC68HC908GP32 Microcontroller Board. xiom anufacturing

538 Lecture Notes Week 1

ELC4438: Embedded System Design Embedded Processor

Choosing a Micro for an Embedded System Application

538 Lecture Notes Week 7

Motorola HC11. Fun with Microcontrollers and Embedded Systems

CMPEN 472 Sample EXAM II

Cross Assembly and Program Development

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-470/570: Microprocessor-Based System Design Fall 2014.

Chapter 1. Microcontroller Overview

Overview The Microcontroller The Flex Board Expansion boards Multibus board Demo board How to: Compile demo Flash & Run Demos

HCS12 Microcontroller and Embedded Systems: Using Assembly and C with CodeWarrior 1 st Edition

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-470/570: Microprocessor-Based System Design Fall 2014.

C Language Programming, Interrupts and Timer Hardware

Codewarrior for ColdFire (Eclipse) 10.0 Setup

Module 1-D. Control Structure Applications. Tim Rogers 2017 [1.D]-1

538 Lecture Notes Week 3

ECE372 CodeWarrior Simulator Andreou/Michaelides

Lecture 5 Assembly Programming: Arithmetic

AN Kbyte Addressing with the M68HC11. Overview

EE319 K Lecture 3. Introduction to the 9S12 Lab 1 Discussion Using the TExaS simulator. University of Texas ECE

Silicore Corporation

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

538 Lecture Notes Week 2

EE 3170 Microcontroller Applications

Fredrick M. Cady. Assembly and С Programming forthefreescalehcs12 Microcontroller. шт.

N bit is set if result of operation in negative (MSB = 1) Z bit is set if result of operation is zero (All bits = 0)

EE345L Spring 2006 May 10, 2006, 2-5pm Page 1 of 8

Transcription:

Linking Assembly Subroutine with a C Program To link an assembly subroutine to a C program, you have to understand how parameters are passed. For the CodeWarrior C compiler, one parameter is passed in the registers. The other parameters are passed on the stack. The left-most parameter is pushed onto the stack first, then the next-to-left, etc. The right-most parameter is passed in the registers An 8-bit parameter is passed in the B regisiter A 16-bit parameter is passed in the D register A 32-bit parameter is passed in the {X:D} register combination. A value returned from the assembly language program is returned in the registers: An 8-bit parameter is returned in the B regisiter A 16-bit parameter is returned in the D register A 32-bit parameter is returned in the {X:D} register combination. In the assembly language program, declare things the C program has to know about as XDEF: XDEF foo In the C program, declare things in the assembly program as you would an other functions: int foo(int x); In the assembly language program, use the stack to store local variables Need to keep close track of stack frame 1

Consider an assembly-language function fuzzy which uses two 8-bit arguments arg1 and arg2, and returns an 8-bit argument result. Declare the function in the C program as char fuzzy(char arg1, char arg2); Here is how the function may be called in the C program: char x,y,result; result = fuzzy(x, y); When the program is compiled, the value of the variable x is pushed onto the stack, the value of the variable y is loaded into the B register, and the function is called with a JSR instruction: 15: result = fuzzy(x,y); /* call the assembly function */ 000b f60000 [3] LDAB x 000e 37 [2] PSHB 000f f60000 [3] LDAB y 0012 160000 [4] JSR fuzzy In the assembly language function, you may need to use some local variables, which need to be allocated on the stack. If the fuzzy function needs two local 8-bit variables var1 and var2, you will need to allocate two bytes on the stack for them. Here s what the start of the assembly language program will look like: fuzzy: leas -2,sp ; Room on stack for var1 and var2 ; Stack frame after leas -2,sp ; ; SP -> var1 ; SP + 1 -> var2 ; SP + 2 -> Return address high ; SP + 3 -> Return address low ; SP + 4 -> 1st parameter of function (arg1) ; ; 2nd paramter (arg2) passed in B register In the assembly language program, you access arg1, var1 and var2 with indexed addressing mode: stab 1,SP ; Save arg2 into var2 ldaa 4,SP ; Put arg1 into ACCA staa 0,SP ; Save arg1 into var1 2

When you return from the assembly language function, put the value you want to return into B, add two to the stack (to deallocate var1 and var2), and return with an RTS. For example, it you want to return the value of the variable var2, you would do the following: ldab 1,SP ; Put var2 into B leas 2,SP ; Deallocate local variables rts ; Return to calling program Any global variables used by the program should be declared in a separate section: ; section for global variables FUZZY_RAM: SECTION ; Locations for the fuzzy input membership values I_E_PM: ds.b 1 I_E_PS: ds.b 1 Any global constants used by the program should be declared in a separate section: ; section for global variables FUZZY_RAM: SECTION ; Locations for the fuzzy input membership values I_E_PM: ds.b 1 I_E_PS: ds.b 1 FUZZY_CONST: SECTION ; Fuzzy input membership function definitions for speed error E_Pos_Medium: 170, 255, 6, 0 E_Pos_Small: 128, 208, 6, 6 The assembly language code should be put in its own section: ; code section MyCode: SECTION ; this assembly routine is called by the C/C++ application fuzzy: leas -2,sp ; Room on stack for ERROR and d_error 3

C program which calls fuzzy logic assembly function #include <hidef.h> /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ #include <stdio.h> #include <termio.h> int fuzzy(unsigned char e, unsigned char de); unsigned char ERROR[] = { 2, 54,106,158,206,255}; unsigned char d_error[] = {101,102,103,104,105,106}; void main (void) { char dpwm; int i; /* Set up SCI for using printf() */ SCI0BDH = 0x00; /* 9600 Baud */ SCI0BDL = 0x9C; SCI0CR1 = 0x00; SCI0CR2 = 0x0C; /* Enable transmit, receive */ } for (i=0;i<6;i++) { dpwm = fuzzy(error[i],d_error[i]); (void) printf("error = %3d, d_error = %3d, ", ERROR[i],d_ERROR[i]); (void) printf("dpwm: %4d\r\n", dpwm); } asm(" swi"); 4

The Assembly program ;************************************************************** ;* This stationery serves as the framework for a * ;* user application. For a more comprehensive program that * ;* demonstrates the more advanced functionality of this * ;* processor, please see the demonstration applications * ;* located in the examples subdirectory of the * ;* Freescale CodeWarrior for the HC12 Program directory * ;************************************************************** ; export symbols XDEF fuzzy ; we use export Entry as symbol. This allows us to ; reference Entry either in the linker.prm file ; or from C/C++ later on ; Include derivative-specific definitions INCLUDE derivative.inc ; Offset values for input and output membership functions E_PM equ 0 ; Positive medium error E_PS equ 1 ; Positive small error E_ZE equ 2 ; Zero error E_NS equ 3 ; Negative small error E_NM equ 4 ; Negative medium error de_pm equ 5 ; Positive medium differential error de_ps equ 6 ; Positive small differential error de_ze equ 7 ; Zero differential error de_ns equ 8 ; Negative small differential error de_nm equ 9 ; Negative medium differential error O_PM equ 10 ; Positive medium output O_PS equ 11 ; Positive small O_ZE equ 12 ; Zero output O_NS equ 13 ; Negative small O_NM equ 14 ; Negative medium output MARKER equ $FE ; Rule separator END_MARKER equ $FF ; End of Rule marker ; variable/data section FUZZY_RAM: SECTION ; Locations for the fuzzy input membership values for speed error I_E_PM: ds.b 1 I_E_PS: ds.b 1 I_E_ZE: ds.b 1 I_E_NS: ds.b 1 I_E_NM: ds.b 1 5

; Locations for the fuzzy input membership values for speed error diff I_dE_PM: ds.b 1 I_dE_PS: ds.b 1 I_dE_ZE: ds.b 1 I_dE_NS: ds.b 1 I_dE_NM: ds.b 1 ; Output fuzzy membership values - initialize to zero M_PM: ds.b 1 M_PS: ds.b 1 M_ZE: ds.b 1 M_NS: ds.b 1 M_NM: ds.b 1 FUZZY_CONST: SECTION ; Fuzzy input membership function definitions for speed error E_Pos_Medium: 170, 255, 6, 0 E_Pos_Small: 128, 208, 6, 6 E_Zero: 88, 168, 6, 6 E_Neg_Small: 48, 128, 6, 6 E_Neg_Medium: 0, 80, 0, 6 ; Fuzzy input membership function definitions for speed error de_pos_medium: 170, 255, 6, 0 de_pos_small: 128, 208, 6, 6 de_zero: 88, 168, 6, 6 de_neg_small: 48, 128, 6, 6 de_neg_medium: 0, 80, 0, 6 ; Fuzzy output memership function definition PM_Output: 192 PS_Output: 160 ZE_Output: 128 NS_Output: 96 NM_Output: 64 ; Rule Definitions Rule_Start: E_PM,dE_PM,MARKER,O_NM,MARKER E_PM,dE_PS,MARKER,O_NM,MARKER E_PM,dE_ZE,MARKER,O_NM,MARKER E_PM,dE_NS,MARKER,O_NS,MARKER E_PM,dE_NM,MARKER,O_ZE,MARKER E_PS,dE_PM,MARKER,O_NM,MARKER E_PS,dE_PS,MARKER,O_NM,MARKER E_PS,dE_ZE,MARKER,O_NS,MARKER E_PS,dE_NS,MARKER,O_ZE,MARKER E_PS,dE_NM,MARKER,O_PS,MARKER 6

E_ZE,dE_PM,MARKER,O_NM,MARKER E_ZE,dE_PS,MARKER,O_NS,MARKER E_ZE,dE_ZE,MARKER,O_ZE,MARKER E_ZE,dE_NS,MARKER,O_PS,MARKER E_ZE,dE_NM,MARKER,O_PM,MARKER E_NS,dE_PM,MARKER,O_NS,MARKER E_NS,dE_PS,MARKER,O_ZE,MARKER E_NS,dE_ZE,MARKER,O_PS,MARKER E_NS,dE_NS,MARKER,O_PM,MARKER E_NS,dE_NM,MARKER,O_PM,MARKER E_NM,dE_PM,MARKER,O_ZE,MARKER E_NM,dE_PS,MARKER,O_PS,MARKER E_NM,dE_ZE,MARKER,O_PM,MARKER E_NM,dE_NS,MARKER,O_PM,MARKER E_NM,dE_NM,MARKER,O_PM,END_MARKER ; code section MyCode: SECTION ; this assembly routine is called by the C/C++ application ; Stack frame after leas -2,sp ; ; SP -> ERROR ; SP + 1 -> d_error ; SP + 2 -> Return address high ; SP + 3 -> Return address low ; SP + 4 -> 1st parameter of function (d_error) ; ; 2nd paramter (ERROR) passed in B register fuzzy: leas -2,sp ; Room on stack for ERROR and d_error stab 1,sp ; d_error passed in B register ldab 4,sp ; ERROR passed on stack stab 0,sp ; Save in space reserved on stack ; Fuzzification LDX #E_Pos_Medium ; Start of Input Mem func LDY #I_E_PM ; Start of Fuzzy Mem values LDAA 0,SP ; Get ERROR value LDAB #5 ; Number of iterations Loop_E: MEM ; Assign mem value DBNE B,Loop_E ; Do all five iterations LDAA 1,SP ; Get d_error value LDAB #5 ; Number of iterations Loop_dE: MEM ; Assign mem value 7

DBNE B,Loop_dE ; Do all five iterations ; Process rules LDX #M_PM ; Clear output membership values LDAB #5 Loopc: CLR 1,X+ DBNE B,Loopc ; Defuzzification LDX #Rule_Start ; Address of rule list -> X LDY #I_E_PM ; Address of input membership list -> Y LDAA #$FF ; FF -> A, clear V bit of CCR REV ; Rule evaluation LDX #PM_Output ; Address of output functions -> X LDY #M_PM ; Address of output membership values -> Y LDAB #5 ; Number of iterations WAV ; Defuzzify EDIV ; Divide TFR Y,D ; Quotient to D; B now from 0 to 255 SUBB #128 ; Subtract offset from d_pwm ; dpwm returned in B; already there leas 2,sp ; Return stack frame to entry value RTS 8

Microcontroller Architectures Things to Consider Performance vs. Cost Speed (instructions/second) Precision (8, 16, 32 or 64 bits, fixed or floating point) Princeton or Harvard Architecture RISC or CISC? Voltage Peripherals RISC: Reduced Instruction Set Computer Very few instructions (8-bit PIC uses 33 instructions) Each instruction takes one cycle to execute Each instruction takes one word of memory Reduces hardware size, increases software size Easier to implement pipelines, etc. CISC: Complex Instruction Set Computer Larger number of more specialized instructions Increases hardware size, reduces software size A/D converter (number of bits) COM ports (how many, what type SCI, SPI I 2 C) USB Ethernet Timers Specialized items Memory PWM Media control (Compact Flash, Secure Digital cards) Many others Address bus size RAM EEPROM Flash EEPROM Special Requirements Low power for battery applications Radiation hardened for space applications Temperature range 9

Development Tools Software Tools Assembler C Compiler IDE Hardware tools Familiarity Evaluation boards In Circuit Emulators Background Debug Mode Different lines from same manufacturer often have similar programming models and instruction forms For example, consider writing the byte $AA to address held in the X register: Motorola: movb #$AA, 0,X Intel: mov [ECX] 0AAH Consider the way the 16-bit $1234 number is stored in memory location $2000 1. Motorola: $12 is stored in address $2000, $34 is stored in address $2001 2. Intel: $34 is stored in address $2000, $12 is stored in address $2001 10

Freescale (Motorola) Microcontrollers HC08 (8 bit) $1.00 each 8 pins to 80 pins 128 bytes to 2 KB RAM 1.5 KB to 7680 KB Flash EEPROM 2 MHz to 8 MHz clock Lots of different peripherals HCS08 (8 bit) $2.00 each (and higher) 8 pins to 64 pins 512 bytes to 4 KB RAM 4 KB to 60 KB Flash EEPROM 8 MHz or 20 MHz clock Lots of different peripherals HCS12 (16 bit) $10.00 each (and higher) 48 pins to 112 pins 2 KB to 12 KB RAM 1 KB to 4 KB EEPROM 32 KB to 512 KB Flash EEPROM 25 MHz to 50 MHz clock 11

Lots of different peripherals S12X (16 bit) $20.00 each (and higher) 48 pins to 112 pins 4 KB to 12 KB RAM 1 KB to 4 KB EEPROM 32 KB to 512 KB Flash EEPROM 25 MHz clock Lots of different peripherals 56800 DSP (32 bit) $7.00 each (and higher) 48 pins to 112 pins 4 KB to 32 KB RAM 16 KB to 512 KB Flash EEPROM 32 MHz to 120 MHz clock Specialized for such things as audio processing MAC (32 bit) $20.00 each (and higher) 32-bit upgrade of 9S12 line for automotive applications 112 pins to 208 pins 16 KB to 48 KB RAM 384 KB to 1024 KB Flash EEPROM 40 MHz to 50 MHz clock Specialized for such things as audio processing ColdFire (32 bit) $40.00 each (and higher) 144 pins to 256 pins 16 MHz to 266 MHz clock Power PC (32 bit) $40.00 each (and higher) 272 pins to 388 pins 26 KB to 32 KB RAM 448 KB to 1024 KB Flash EEPROM 40 MHz to 66 MHz clock 12

Other Manufacturers Low end (8 bit) PIC from Microchip Very inexpensive ($0.50) Low pin count (6 to 100) Often small memory (16 bytes RAM, 128 bytes ROM) RISC 8051 (Originally Intel, now National, TI) Z8 (Zilog similar to 8051) Mid-Range (16 bits) Z80 and Z180 from Rabbit High End (32 bit) ARM - licensed to Intel, TI, many others MIPS - licensed to Hitachi Soft Core Altera NIOS Can customize to meet needs Speed vs. size (number of logic gates) 16-bit or 32-bit Fixed point or floating point Memory management or no memory management Can build specialized instructions to increase performance Xilinx ARM (soft core or hard core) 13