Interfacing C to Assembler

You can easily interface your C programs to routines written in XC16x/C16x/ST10 assembly language.

The A166 Assembler is a macro assembler that emits object modules in OMF166 format.

By following a few programming rules, you can call assembly routines from C and vice versa.

Public variables declared in the assembly module are available to your C programs.

There are several reasons to call an assembly routine from your C program.

  • You have assembly code already written that you wish to use.
  • You need to improve the speed of a particular function.
  • You want to manipulate SFRs or memory-mapped I/O devices directly from assembly.

This section describes how to write assembly routines that can be directly interfaced to C programs.

For an assembly routine to be called from C, it must be aware of the parameter passing

and return value conventions used in C functions.

For all practical purposes, the assembly routine must appear to be a C function.

Function Parameters

C functions pass up to five parameters in registers (R8-R12).
Bit parameters are passed in R15.
Parameters that do not fit into R8-R12 are passed on the user stack
and are accessed using [R0+#disp] instructions.

The following examples demonstrate the C166 parameter passing conventions:

void func1 (
char a) /* 1st parameter passed in R8 */

The above function has one argument that easily fits into a single 16-bit register (R8).

void func2 (
int b, /* 1st parameter passed in R8 */
int c, /* 2nd parameter passed in R9 */
int near *d, /* 3rd parameter passed in R10 */
char e, /* 4th parameter passed in R11 */
char f) /* 5th parameter passed in R12 */

The above function has five arguments. All fit into R8-R12.

void func3 (
long g, /* 1st parameter passed in R8/R9 */
int far *h, /* 2nd parameter passed in R10/R11 */
int i, /* 3rd parameter passed in R12 */
long j) /* 4th parameter cannot be located in registers */

The above function has four arguments. The first three use all available registers. The fourth argument must be passed on the user stack.

void func4 (
double k, /* 1st parameter passed in R8/R9/R10/R11 */
long j) /* 2nd parameter LSW passed in R12 */
/* MSW passed on the user stack */

The above function has two arguments. The first argument uses four of the five registers.
The LSW of the second argument is passed in R12 and the MSW is passed on the user stack.

void func5 (
bit m, /* 1st parameter passed in R15.0 */
bit n) /* 2nd parameter passed in R15.1 */

The above function has two bit arguments that passed in R15.

void func6 (
char o, /* 1st parameter passed in R8 */
bit p, /* 2nd parameter passed in R15.0 */
char q, /* 3rd parameter passed in R9 */
bit r) /* 4th parameter passed in R15.1 */

The above function has four arguments (two are bits) that are passed in registers and in R15 (for the bits).

Function Return Values

Function return values are always passed using MCU registers.
The following table lists the possible return values and the registers used for each.

Return Type Register Description
bit R4.0 Single bit returned in R4.0.
char,
unsigned char
RL4 Single byte type returned in RL4.
int,
unsigned int,
near pointer
R4 Two byte (16-bit) type returned in R4.
long,
unsigned long,
far pointer,
huge
pointer
R4 & R5 LSB in R4, MSB in R5.
float R4 & R5 32-Bit IEEE format.
double R4-R7 64-Bit IEEE format.

Register Usage

Assembler functions may destroy the contents of R1-R12, PSW, MDL, MDH, MDC, and DPP0.

When invoking a C function from assembly, assume that these registers are destroyed.

When using the MAC directive the compiler uses in addition the MAC registers MSW, MAL, MAH, MRW, and IDX0.

When code is generated with the MAC directive, these registers are used by C functions (in addition to the register listed above).

When invoking a C function form assembly you need therefore to assume that these registers are destroyed too.

The following registers have special meaning and must be preserved by the assembler subroutine.

Register Description
R0

R0 is the user stack pointer.
At the end of the assembler subroutine the value of R0 must be the same as at the start of the routine.

R13

This register may be used but its contents must be saved (on entry) and restored (before returning).

R14

This register may be used but its contents must be saved (on entry) and restored (before returning).

R15

This register may be used but its contents must be saved (on entry) and restored (before returning).

DPP1

This DPP register may not be modified by assembler subroutines.
It contains the memory class page and may be used in an interrupt service routine that interrupts the assembler subroutine.

DPP2

This DPP register may not be modified by assembler subroutines.
It contains the memory class page and may be used in an interrupt service routine that interrupts the assembler subroutine.

DPP3

If DPP3 is modified in the assembler subroutine, it must be reset to 3 (SYSTEM PAGE) before returning.

Note

  • DPP0 is used only when the MOD167 C166 Compiler directive is not specified.
  • If your assembler programs alter DPP0, the DPPUSE L166 Linker directive may not be used to assign DPP0 to the near memory area.
  • If your assembler programs alter DPP0 or DPP3, the NODPPSAVE C166 Compiler directive may not be used to bypass saving and restoring DPP0 and DPP3 in interrupts.

C166 Interfacing C to Assembler的更多相关文章

  1. 关于如何在C语言中嵌入汇编命令

    转载自:http://www.keil.com/support/docs/2308.htm C51: GETTING INLINE ASSEMBLY TO WORK Information in th ...

  2. Fixed-point multiplication (C166 A*B/B)

    I want to multiply two fixed point numbers. After the multiplication I have to shift the result so t ...

  3. Assembler : The Basics In Reversing

    Assembler : The Basics In Reversing Indeed: the basics!! This is all far from complete but covers ab ...

  4. Jena TDB 101 Java API without Assembler

    Update on 2015/05/12 ongoing tutorials site on https://github.com/zhoujiagen/semanticWebTutorialUsin ...

  5. Jena TDB assembler syntax

    1 introduction Assembler is a DSL of Jena to specify something to build, models and dataset, for exa ...

  6. Solaris 11 system package 安装与更新(如:assembler)

    最近在VirtualBox虚拟机中导入了Solaris 11.3.在里面安装Oracle数据库时,先行条件检查没通过,提示缺少程序包assembler. 在网上看了许多,这方面的信息还比较少.最后在O ...

  7. More x64 assembler fun-facts–new assembler directives(转载)

    原文地址 The Windows x64 ABI (Application Binary Interface) presents some new challenges for assembly pr ...

  8. x64 assembler fun-facts(转载)

    原文地址 While implementing the x64 built-in assembler for Delphi 64bit, I got to “know” the AMD64/EM64T ...

  9. c166 -div

    unsigned short a=10; unsigned short b; unsigned short c;unsigned long d; b = (unsigned short)(d/2400 ...

随机推荐

  1. GetPrivateProfileStringA的文件名要小心写

    ::GetPrivateProfileStringA("REMOTE", "host", "http://xxx.comftp.php", ...

  2. ASP.NET静态页生成

    由于业务需要,得把页面按照模板页生成静态页面,所以自己就琢磨了下,写些思路,以备日后需要的时候用. 静态页生成用到最多的就是匹配跟替换了,首先得读取模板页的html内容,然后进行你自己定义的标签匹配, ...

  3. 将cocos2dx项目从Visual Studio 迁移到 xcode

    因为Visual Studio和XCode的巨大差异性,一开始选择任何一个IDE,都会有一个迁移的过程,XCode的迁移到Visual Studio相对非常简单,不用再介绍.将项目从Visual St ...

  4. iframe根据子页面自动调整大小

    //iframe高度自适应 function IFrameReSize(iframename) { var pTar = document.getElementById(iframename); if ...

  5. Connection failed: NT_STATUS_ACCOUNT_RESTRICTION

    今天在linux机器上想要远程重启一台window的机器,输入命令后报错,如下 Google了下,有说是window禁止远程空密码登录,于是到window系统中添加了密码,这下再运行 这下执行就正常了

  6. map,hash_map和unordered_map 实现比较

    map介绍 Map是STL[1]的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处 ...

  7. .NET中的Newtonsoft.Json.JsonConvert.SerializeObject(string a)

    1.將string a 序列化為Json格式: 2.使用條件:將Newtonsoft.Json.dll作為引用添加到項目中.下载地址在这:http://json.codeplex.com/

  8. 从四大音乐APP首页设计对比分析产品方向

    原帖:http://www.ui.cn/detail/63201.html 本文章中作者例举四个音乐APP应用:虾米.网易.百度.QQ首页 1. 推荐内容:作者将四个首页界面划分出官方推荐与个性化推荐 ...

  9. Python 最佳实践

    前言 对我来说,以前每次面试是我审视自己,检验自己的一种方式.每次准备面试,以及被面试官问住的时候才会发现,其实我python我学的还不够好.工作中也是,可以从其他的同事那里获得成长.但是我今天说的是 ...

  10. 使用arm开发板搭建无线mesh网络(二)

    上篇博文介绍了无线mesh网络和adhoc网络的区别,这篇文章将介绍无线mesh网络的骨干网节点的组建过程.首先需要介绍下骨干网节点的设计方案:每个骨干网节点都是由一块友善之臂的tiny6410 ar ...