为什么使用寄存器出于其预期目的而使用寄存器?

发布于 2025-01-22 05:05:43 字数 1847 浏览 3 评论 0原文

众所周知,X86寄存器名称具有目的,并指出了应如何使用寄存器(请参阅此网站例如或这样的发布)。 寄存器的目的应为:(

* EAX - Accumulator Register
* EBX - Base Register
* ECX - Counter Register
* EDX - Data Register
* ESI - Source Index
* EDI - Destination Index
* EBP - Base Pointer
* ESP - Stack Pointer

请注意,我还没有在英特尔的文档上找到有关此的官方信息)

ecx应该是持有我的i变量的寄存器代码:

int main()
{
    register int i = 0;
    for(i = 0 ; i <= 10 ; i++){}
    return 0;
}

我用gcc loop.c -o loop编译的代码。 然后我用objdump -d -m intel ./ loop


0000000000001138 <main>:
    1138:       f3 0f 1e fa             endbr64
    113c:       55                      push   rbp
    113d:       48 89 e5                mov    rbp,rsp
    1140:       53                      push   rbx
    1141:       bb 00 00 00 00          mov    ebx,0x0
    1146:       f3 0f 1e fa             endbr64
    114a:       bb 00 00 00 00          mov    ebx,0x0
    114f:       eb 03                   jmp    1154 <main+0x1c>
    1151:       83 c3 01                add    ebx,0x1
    1154:       83 fb 0a                cmp    ebx,0xa
    1157:       7e f8                   jle    1151 <main+0x19>
    1159:       b8 00 00 00 00          mov    eax,0x0
    115e:       5b                      pop    rbx
    115f:       5d                      pop    rbp
    1160:       c3                      ret

我们清楚地看到ebx holding i ,而不是ecx。 有历史原因吗?编译器当时是否使用了理论目的或登记册,还是仅适用于人类?

It seem to be well known that x86 registers names have a purpose and indicate on how the register should be use (see this website for example, or this SO post).
Registers purpose should be :

* EAX - Accumulator Register
* EBX - Base Register
* ECX - Counter Register
* EDX - Data Register
* ESI - Source Index
* EDI - Destination Index
* EBP - Base Pointer
* ESP - Stack Pointer

(Please note that I did not found official information about this on INTEL's documentation yet)

According to this, ecx should be the register holding my i variable on the code bellow :

int main()
{
    register int i = 0;
    for(i = 0 ; i <= 10 ; i++){}
    return 0;
}

Which I compile with gcc loop.c -o loop
And I disassemble it with objdump -D -M intel ./loop:


0000000000001138 <main>:
    1138:       f3 0f 1e fa             endbr64
    113c:       55                      push   rbp
    113d:       48 89 e5                mov    rbp,rsp
    1140:       53                      push   rbx
    1141:       bb 00 00 00 00          mov    ebx,0x0
    1146:       f3 0f 1e fa             endbr64
    114a:       bb 00 00 00 00          mov    ebx,0x0
    114f:       eb 03                   jmp    1154 <main+0x1c>
    1151:       83 c3 01                add    ebx,0x1
    1154:       83 fb 0a                cmp    ebx,0xa
    1157:       7e f8                   jle    1151 <main+0x19>
    1159:       b8 00 00 00 00          mov    eax,0x0
    115e:       5b                      pop    rbx
    115f:       5d                      pop    rbp
    1160:       c3                      ret

We clearly see that ebx is holding i, not ecx.
Is there an historical reason to this? Did compiler used theoretical purpose or registers back then or was it just for humans?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文