简体   繁体   English

C程序中的asm指令替代

[英]asm instruction alternative in c program

I am writing a user space C program to read the hard disk. 我正在编写一个用户空间C程序来读取硬盘。

I need to convert an assembler instruction to C program code. 我需要将汇编指令转换为C程序代码。 How can this be done? 如何才能做到这一点?

mov eax, [rsi+0x0C]

Here eax can be any variable. 这里eax可以是任何变量。 However, rsi is the base address register with value 0xc1617000 . 但是, rsi是值为0xc1617000的基地址寄存器。 This value does not change. 该值不变。

You can assign values to pointers in C. Try this: 您可以在C中为指针分配值。请尝试以下操作:

uint8_t *rsi = (uint8_t*)(uintptr_t) 0xc1617000; // The uintptr_t cast isn't really needed, but might help portability.
uint32_t value = *(uint32_t *)(rsi + 0x0C);

A shorter version, of course is: 较短的版本,当然是:

uint32_t value = *(uint32_t *)0xc161700C;

Basically you interpret that constant as a pointer to uint32_t , and then dereference it. 基本上,您可以将该常量解释为指向uint32_t的指针,然后取消对其的引用。

Following http://www.cs.virginia.edu/~evans/cs216/guides/x86.html : 遵循http://www.cs.virginia.edu/~evans/cs216/guides/x86.html

mov eax, [rsi+0x0C]

means 手段

move the 4 Byte word at the address rsi+0x0C to the EAX register 将地址rsi+0x0C的4字节字移到EAX寄存器

that's what this line of assembler means; 这就是汇编程序这一行的意思; you say 你说

Here eax can be any variable 这里eax可以是任何变量

Typically, EAX is the return value of some function, but I'll not go into this. 通常,EAX是某些函数的返回值,但我将不再赘述。

Since this is trivial: 由于这很简单:

int variable = *((unsigned int*) 0xc161700C; 

notice that it's totally up to your compiler whether it actually copies over that value -- in many cases, the compiler will be able to do that only when the value of variable is actually used. 请注意,是否实际复制该值完全取决于您的编译器-在许多情况下,仅当实际使用variable的值时,编译器才能够这样做。 If asking for the address of variable , you might either be getting a new address, or actually 0xc161700C . 如果询问variable的地址,则可能是获得一个新地址,或者实际上是0xc161700C

Since this is basic C, I'm not so confident I want to let you play with my hard drive ;) notice that for programs running in unprivileged (non-kernel mode), access to physical memory addresses is impossible in general. 由于这是基本的C语言,因此我不太确定要让您使用我的硬盘驱动器;)请注意,对于以非特权(非内核模式)运行的程序,通常无法访问物理内存地址。

EDIT 编辑

On linux the program is crashing when accessing the location. 在linux上,访问该位置时程序崩溃。 May be because its outside the bound of process memory. 可能是因为它超出了进程内存的范围。 Any idea how to access the memory outside the bound of process memory 任何想法如何访问进程内存范围之外的内存

As I said here and in the comments: If your code is running as a program (in userland ), you can never access raw physical memory addresses. 如我在这里和评论中所述:如果您的代码作为程序运行(在userland中 ),则您永远无法访问原始物理内存地址。 Your process sees its own memory with physical memory being mapped there in pages -- there's no possibility to access raw physical memory without the help of kernel mode. 您的进程看到了自己的内存,并且物理内存被映射到页面中-如果没有内核模式的帮助,就不可能访问原始物理内存。 That is the beauty of memory mapping as done on any modern CPU: programs can't fiddle directly with hardware. 这就是在任何现代CPU上完成的内存映射的美妙之处:程序无法直接与硬件打交道。

Under Linux, things might be relatively easy: open or mmap /dev/mem as root and access the right position in that file -- it's an emulation of direct access to memory as accessible by the operating system. 在Linux下,事情可能相对容易:以root身份打开或mmap /dev/mem并访问该文件中的正确位置-这是对操作系统直接访问内存的直接模拟。

However, what you're doing is hazardous, and Linux usually already supports as much AHCI as it should -- are you sure you're already using a linux kernel of the last ten years? 但是,您正在做的事情很危险,Linux通常已经尽可能多地支持AHCI -您确定您已经使用了最近十年的linux内核吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM