搜索
bottom↓
回复: 7

汇编怎么得到C定义的变量

[复制链接]

出0入0汤圆

发表于 2008-1-17 02:43:38 | 显示全部楼层 |阅读模式
请教马老师:
在C 语言中,定义了一个变量,怎么才能使汇编能够拿到呢?

例:
unsigned char flash_enable;
main()
{
.....

asm("LDS        R21,(flash_enable地址)") ;
}

谢谢

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

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2008-1-17 08:49:02 | 显示全部楼层
那也要看什么C编译器了,各家的规定不一样

出0入0汤圆

发表于 2008-1-17 08:54:54 | 显示全部楼层
这个问题,我也想知道,我用的ICCAVR 就 请问在这个编译环境下的吧!如果可以,说一下 CVAVR 也很好, 因为马老师 书上讲的就是CVAVR ,说不定那一天 我也能用上CVAVR!

出0入0汤圆

发表于 2008-1-17 12:24:09 | 显示全部楼层
1楼说是对,这个要根据你所使用的C编译器来决定的.具体的方法在它们的HELP中有说明.

下面时ICC中的说明部分:
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.

具体的例子,可参考我编写的<M128>一书第五章中的BOOTLOAD的例程,里面有LZ想要的方式.

=================================================================================
下面是CVAVR的用法:
Calling Assembly Functions from C

The following example shows how to access functions written in assembly language from a C program:

// function in assembler declaration
// this function will return a+b+c
#pragma warn- // this will prevent warnings
int sum_abc(int a, int b, unsigned char c) {
#asm
    ldd   r30,y+3 ;R30=LSB a
    ldd   r31,y+4 ;R31=MSB a
    ldd   r26,y+1 ;R26=LSB b
    ldd   r27,y+2 ;R27=MSB b
    add   r30,r26 ;(R31,R30)=a+b
    adc   r31,r27
    ld    r26,y   ;R26=c
    clr   r27     ;promote unsigned char c to int

    add   r30,r26 ;(R31,R30)=(R31,R30)+c
    adc   r31,r27
#endasm
}
#pragma warn+ // enable warnings

void main(void) {
int r;
// now we call the function and store the result in r
r=sum_abc(2,4,6);
}

The compiler passes function parameters using the Data Stack.
First it pushes the integer parameter a, then b, and finally the unsigned char parameter c.
On every push the Y register pair decrements by the size of the parameter (4 for long int, 2 for int, 1 for char).

For multiple byte parameters the MSB is pushed first.
As it is seen the Data Stack grows downward.
After all the functions parameters were pushed on the Data Stack, the Y register points to the last parameter c, so the function can read its value in R26 using the instruction: ld r26,y.
The b parameter was pushed before c, so it is at a higher address in the Data Stack.
The function will read it using: ldd r27,y+2 (MSB) and ldd r26,y+1 (LSB).
The MSB was pushed first, so it is at a higher address.

The a parameter was pushed before b, so it is at a higher address in the Data Stack.
The function will read it using: ldd r31,y+4 (MSB) and ldd r30,y+3 (LSB).

The functions return their values in the registers (from LSB to MSB):
    R30 for char and unsigned char
    R30, R31 for int and unsigned int
    R30, R31, R22, R23 for long and unsigned long.

So our function must return its result in the R30, R31 registers.

After the return from the function the compiler automatically generates code to reclaim the Data Stack space used by the function parameters.

The #pragma warn- compiler directive will prevent the compiler from generating a warning that the function does not return a value.
This is needed because the compiler does not know what it is done in the assembler portion of the function.

出0入0汤圆

发表于 2008-1-17 12:50:24 | 显示全部楼层
哦!是这样,我对ENGLISH 不太懂!惭愧!不过上面的我大致看懂了,其实C 和汇编 参数传递是通过寄存器传递的,这个和KEIL C51说的差不多!

出0入0汤圆

发表于 2008-1-17 12:58:29 | 显示全部楼层
是的,原理差不多.只是细节上有不同.不过参数多的情况需要注意,并不是都在寄存器中.

出0入0汤圆

发表于 2008-1-17 13:37:50 | 显示全部楼层
我把马老师那段英文前半部分 大致意思用中文表达一下,供英文和我一样差的又想学单片机的朋友看看,有错误恳请指出,大家一起学习!
下面时ICC中的说明部分:
第一个参数 如果是(int)就用R16/R17 (long 或float)就用 R16/R17/R18/R19
第二个参数 如果可以的话 用  R18/R19
其他 软件堆栈

如果R16/R17 用与第一个参数 第二个参数是(long 或float)那么底两个字节 在 R18/R19 高两个字节 用软件堆栈!

如果参数是一个字节 第一个参数 放在 R16 第二个在软件堆栈中

=============================================================
基本意思出来了,但不完善.比如字节怎么处理的?怎样获得在软件堆栈中的参数?

出0入0汤圆

 楼主| 发表于 2008-1-17 15:12:53 | 显示全部楼层
谢谢马老师,

谢谢大家。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-7-23 09:26

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

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