由C内联汇编定义访问阵列 [英] Access array defined in inline assembler from C

查看:116
本文介绍了由C内联汇编定义访问阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在汇编声明的整数,我在下面的方式来使用它在C:

I have an integer declared in Assembler, and I use it in C in the following way:

asm(
            "number:    \n"
            ".long 0xFFFFFFFF \n
);

extern int number;
int main(){
    //do something with number
}

现在我要声明在汇编一个32字节数组。我试过如下:

Now I want to declare a 32 byte array in Assembler. I tried the following:

asm(
            "number:    \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
);

extern unsigned char* number;
int main() {
    printf("%x ", number[0]); //gives segmentation fault
}

我真的不知道汇编,但我不得不使用这个特定的变量。

I do not really know Assembler, but I have to use for this specific variable.

推荐答案

您内联汇编做到这一点。

Your inline assembler does this

asm(
            "number:    \n"
            ".long 0xFFFFFFFF \n"
            [snip rest of array]
);

您再告诉的 C 的是数量的是

extern unsigned char* number;

这说的数量的是指向一个无符号的字符。然后你访问它是这样的:

This says that number is a pointer to an unsigned character. Then you access it like this:

printf("%x ", number[0]);

这说要取消引用的数量的指针,返回的第一个字符。这本来是一样做的:

This says to de-reference the pointer in number and return the first character. It would have been the same as doing:

printf("%x ", *(number+0));

问题是,的被定义为一个指针。假设32位指针可以转换为:

Problem is that number was defined as a pointer. Assuming 32-bit pointers that translates to:

*(0xFFFFFFFF+0)

如果你得到一个段错误很可能是因为该地址为0xFFFFFFFF是不是你的程序访问。

If you get a segfault it is probably because the address 0xFFFFFFFF is not accessible to your program.

您可以更改您的的extern 的声明如下:

You can change your extern statement to read:

extern unsigned char number[32];

现在的是32无符号的字符数组。我倾向于使用 inttypes.h 头并将其声明为:

Now number is an array of 32 unsigned characters. I'd be inclined to use the inttypes.h header and declare it as:

extern uint8_t number[32];

uint8_t有保证是8位(或1个字节)。 另一方面被定义为最小的8位(但可以更多)。然而的sizeof(char)的将始终返回1.我preFER uint8_t有(无符号的8位整数)只是这样你知道你正在处理这似乎是这里的情况8位值。我修改code是:

uint8_t is guaranteed to be 8 bits (or 1 byte). char on the other hand is defined as being a minimum of 8 bits (but can be more). However sizeof(char) will always return 1. I prefer uint8_t (unsigned 8 bit integers) just so you know you are dealing with 8 bit values which seems to be the case here. I'd modify the code to be:

#include <stdio.h>
#include <inttypes.h>

__asm__(
            "number:    \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFE \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
            ".long 0xFFFFFFFF \n"
);

extern uint8_t number[32];

int main() {
    printf("%x ", number[0]);
    return 0;
}

另外请注意,如果你打算使用GCC作为C99(将与GCC的C89以及工作)是preferable用于编译 __ ASM __ 而不是 ASM 因为GCC的默认是禁用 ASM 关键字(除非 -fasm )使用 -std = C99 选项。

Also note that if you intend to compile using GCC as C99 (will work with GCC's C89 as well) it is preferable to use __asm__ instead of asm since GCC's default is to disable the asm keyword (unless overridden with -fasm) when using -std=c99 option.

的可能是不适合的无符号字符数组一个好名字。这可能会导致混乱,当有人来陪伴和维护code。

number is probably not a good name for an array of unsigned characters. It may cause confusion when someone has to come along and maintain your code.

这篇关于由C内联汇编定义访问阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆