搜索
bottom↓
回复: 1

马老师:请教你个问题:在IccAvr中,怎么做混编呢,能给个范例看看吗?在线等!

[复制链接]

出0入0汤圆

发表于 2006-4-30 09:21:51 | 显示全部楼层 |阅读模式
马老师:请教你个问题:在IccAvr中,怎么做混编呢,能给个范例看看吗?在线等!



对代码效率要求很高,所以很急,谢谢你

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2006-4-30 21:41:45 | 显示全部楼层
Inline Assembly:



Besides writing assembly functions in assembly files, inline assembly allows you to write assembly code within your C file. (Of course, you may use assembly source files as part of your project as well.) The syntax for inline assembly is:



asm("<string>");



Multiple assembly statements can be separated by the newline character
. String concatenations can be used to specify multiple statements without using additional asm keywords. To access a C variable inside an assembly statement, use the %<name> format:



register unsigned char uc;

asm("mov %uc,R0
"

    "sleep
");



Any C variable can be referenced this way (excluding C goto labels). If you use an assembly instruction that requires a CPU register, then you must use the register storage class to force a local variable to be allocated in a CPU register.

In general, using inline assembly to reference local registers is limited in power: it is possible that no CPU registers are available if you have declared too many register variables in that function. In that case, you would get an error from the assembler. There is also no way to control allocation of register variables, so your inline instruction may fail. For example, using the ldi instruction requires the register to be one of the 16 upper registers, but there is no way to request that using inline assembly. There is also no way to reference the upper half of an integer register.



Inline assembly may be used inside or outside of a C function. The compiler indents each line of the inline assembly for readability. Unlike the AVR assembler, the ImageCraft assembler allows labels to be placed anywhere (not just at the first character of the lines in your file) so you may create assembly labels in your inline assembly code. You may get a warning on asm statements that are outside of a function. You may ignore these warnings.



Assembly Interface and Calling Conventions:

External Names

External C names are prefixed with an underscore, e.g. the function main is _main if referenced in an assembly module. Names are significant to 32 characters. To make an assembly object global, use two colons after the name. For example,



_foo::



.word 1



(In the C file)



extern int foo;



Argument and Return Registers



The first argument is passed in registers R16/R17 if it is an integer and R16/R17/R18/R19 if it is a long or floating point. The second argument is passed in R18/R19 if available. All other remaining arguments are passed on the software stack.



Integer arguments smaller than ints (i.e. char) are not extended to in IF the function prototype is available. If registers are used to pass byte arguments, it will use both registers but the higher register is undefined. For example, if the first argument is a byte, both R16/R17 will be used with R17 being undefined. Byte arguments passed on the software stack take up only a single byte. In the future release, byte arguments passed in registers will be packed tightly.



If R16/R17 is used to pass the first argument and the second argument is a long or float, the lower half of the second argument is passed in R18/R19 and the upper half is passed on the software stack.



Integer values are returned in R16/R17 and longs and floats are returned in R16/R17/R18/R19. Byte values are returned in R16 with R17 undefined.



Preserved Registers



An assembly function must save and restore the following registers:



R28/R29 or Y, this is the frame pointer.

        R10/R11/R12/R13/R14/R15/R20/R21/R22/R23



These registers are called preserved registers since their contents are unchanged by a function call. You can ask the compiler not to use the registers R20, R21, R22, and R23, then you do not need to save and restore these 4 registers.



Volatile Registers



The other registers:



R0/R1/R2/R3/R4/R5/R6/R7/R8/R9/R24/R25/R26/R27/R30/R31

        SREG



can be used in a function without being saved or restored. These registers are called volatile registers since their contents may be changed by a function call.



Interrupt Handlers



Note that unlike a normal function, an interrupt handler must save and restore all registers that it uses. This is done automatically if you use the compiler capability to declare a C function as an interrupt handler. If you write a handler in assembly and if it calls normal C functions, then the assembly handler must save and restore the Volatile Registers since normal C functions do not preserve them. Since an interrupt handler operates asynchronous to the normal program operation, the interrupt handler or the functions it calls must not modify any machine registers. The exception is when you ask the compiler not to use the registers R20, R21, R22, and R23, then the interrupt handlers may use these 4 registers directly.
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-18 13:12

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表