cpu怎么知道内存中的数据是数据还是命令?(C 编程) [英] how does the cpu knows that the data in memory is data or a command? (C programming)

查看:77
本文介绍了cpu怎么知道内存中的数据是数据还是命令?(C 编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望数据从例如 0x100(内存位置)和之前的所有命令开始......但我真的不确定!感谢帮助!

I expect that that data begins at for example 0x100 (memory location) and everything before that it's commands... But I am really not sure! Thanks for help!

好的,详细说明我的问题:我将内存视为一个具有一个字节空间的长数组.该空间填充有十六进制数字.但是变量可以从例如 0x0000 - 0xffff 填充内存.但是你怎么知道例如 0x002f 是一个命令(例如 'mov')还是只是一个作为数据的数字?

okay, to detail my question: I see the memory as a long array with one byte space. The space is filled with hex numbers. But the variables can fill the memory from for example 0x0000 - 0xffff. But how do you know that for example 0x002f is a command (for example 'mov') or just a number as data?

推荐答案

CPU 不知道.一切都与约定有关.

The CPU doesn't know. It's all about conventions.

当您启动计算机或嵌入式设备时,它会开始从闪存中执行引导程序.

When you start your computer or embedded device it starts executing a bootloaer from a flash storage.

反过来,引导加载程序将启动代码从持久存储加载到内存,并在加载它的地址开始执行(通常是操作系统内核).

In turn, the bootloader loads startup code from persistent storage to memory and starts executing (typically the OS kernel) at the address where it loaded it.

反过来,内核将在已知内存位置加载额外的模块和初始化代码并从那里执行.

In turn, the kernel will load additional modules and init code at known memory locations and execute from there.

在某些时候,内存虚拟化被启用,可执行文件被加载到内存中,每个文件都与一个进程及其地址空间相关联.可执行标头和操作系统约定定义了代码和数据段的位置.

At some point, memory virtualization is enabled, and executable files are loaded in memory and each is associated with a process and its address space. The executable header and OS conventions define code and data segments locations.

但是代码段可能包含嵌入的数据,动态分配的内存可能包含代码,例如在即时编译器或恶意程序中.

But the code segment may contain embedded data, and dynamically allocated memory may contain code, for instance in just-in-time compilers or malicious programs.

可以说明:想象一下正在运行的编译器的地址空间...

Ok to illustrate: imagine the address space of a running compiler...

[ code seg from .exe ] [ data seg from .exe ] [ dynamic alloc ]

这些内存区域唯一的区别是[code]是Read+Execut,[data]是ReadOnly,[dynamic]是Read+Write,有时是+Execute

The only difference between these memory regions is that [code] is Read+Execut, [data] is ReadOnly, [dynamic] is Read+Write, sometimes +Execute

  • [代码] 包含
    • 主要是机器指令
    • 但它可能包含即时数据,例如整数常量等
    • 诸如语言关键字的字符串、错误消息等.
    • 代码,因为你有机器指令字典,所以你可以生成代码
    • 运行时分配的数据结构,例如字符串、树等.
    • 运行时生成的代码,将写入编译器正在构建的 exe
    • 运行时生成的代码,将由 jit 执行以在编译时计算复杂表达式

    所以你看,你可以在任何部分拥有数据和代码.这就是让计算机变得强大的原因,另请参阅图灵机.

    So you see, you can have data and code in any section. That's what makes computers powerful, see also the turing machines.

    这篇关于cpu怎么知道内存中的数据是数据还是命令?(C 编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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