gpt4 book ai didi

c - 移植 AT&T inline-asm inb/outb 包装器以与 gcc -masm=intel 一起工作

转载 作者:太空狗 更新时间:2023-10-29 15:56:33 25 4
gpt4 key购买 nike

我目前正在开发我的 x86 操作系统。我尝试从 here 实现 inb 函数它给了我 Error: Operand type mismatch for `in'

这也可能与outbio_wait相同。

我正在使用 Intel 语法 (-masm=intel),但我不知道该怎么做。

代码:

#include <stdint.h>
#include "ioaccess.h"

uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile ( "inb %1, %0"
: "=a"(ret)
: "Nd"(port) );
return ret;
}

使用 AT&T 语法这确实有效。


对于 outb,我在反转操作数后遇到了不同的问题:

void io_wait(void)
{
asm volatile ( "outb $0x80, %0" : : "a"(0) );
}

错误:“out”的操作数大小不匹配

最佳答案

如果需要使用-masm=intel您将需要确保您的内联汇编使用 Intel 语法。 Intel 语法是 dst, src(AT&T 语法是相反的)。这有点related answer有一些关于 NASM 的 Intel 变体1(不是 GAS 的变体)和 AT&T 语法之间的一些差异的有用信息:

Information on how you can go about translating NASM Intel syntax to GAS's AT&T syntax can be found in this Stackoverflow Answer, and a lot of useful information is provided in this IBM article.

[snip]

In general the biggest differences are:

  • With AT&T syntax the source is on the left and destination is on the right and Intel is the reverse.
  • With AT&T syntax register names are prepended with a %
  • With AT&T syntax immediate values are prepended with a $
  • Memory operands are probably the biggest difference. NASM uses [segment:disp+base+index*scale] instead of GAS's syntax of segment:disp(base, index, scale).

您代码中的问题是源操作数和目标操作数必须与您使用的原始 AT&T 语法相反。这段代码:

asm volatile ( "inb %1, %0"
: "=a"(ret)
: "Nd"(port) );

需要:

asm volatile ( "inb %0, %1"
: "=a"(ret)
: "Nd"(port) );

关于您的更新:问题是在 Intel 语法中,立即值没有前缀 $ .这一行有问题:

asm volatile ( "outb $0x80, %0" : : "a"(0) );

应该是:

asm volatile ( "outb 0x80, %0" : : "a"(0) );

如果你有合适的 outb功能你可以做这样的事情:

#include <stdint.h>
#include "ioaccess.h"

uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile ( "inb %0, %1"
: "=a"(ret)
: "Nd"(port) );
return ret;
}

void outb(uint16_t port, uint8_t byte)
{
asm volatile ( "outb %1, %0"
:
: "a"(byte),
"Nd"(port) );
}

void io_wait(void)
{
outb (0x80, 0);
}

同时支持 AT&T 和 Intel 的稍微复杂的版本 dialects :

Multiple assembler dialects in asm templates On targets such as x86, GCC supports multiple assembler dialects. The -masm option controls which dialect GCC uses as its default for inline assembler. The target-specific documentation for the -masm option contains the list of supported dialects, as well as the default dialect if the option is not specified. This information may be important to understand, since assembler code that works correctly when compiled using one dialect will likely fail if compiled using another. See x86 Options.

If your code needs to support multiple assembler dialects (for example, if you are writing public headers that need to support a variety of compilation options), use constructs of this form:

{ dialect0 | dialect1 | dialect2... }

在 x86 和 x86-64 目标上有两种方言。 Dialect0 是 AT&T 语法,Dialect1 是 Intel 语法。这些功能可以这样重新设计:

#include <stdint.h>
#include "ioaccess.h"

uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile ( "inb {%[port], %[retreg] | %[retreg], %[port]}"
: [retreg]"=a"(ret)
: [port]"Nd"(port) );
return ret;
}

void outb(uint16_t port, uint8_t byte)
{
asm volatile ( "outb {%[byte], %[port] | %[port], %[byte]}"
:
: [byte]"a"(byte),
[port]"Nd"(port) );
}

void io_wait(void)
{
outb (0x80, 0);
}

我还给出了约束符号名称,而不是使用 %0%1使内联程序集更易于阅读和维护。从 GCC 文档中,每个约束都有以下形式:

[ [asmSymbolicName] ] constraint (cvariablename)

地点:

asmSymbolicName

Specifies a symbolic name for the operand. Reference the name in the assembler template by enclosing it in square brackets (i.e. ‘%[Value]’). The scope of the name is the asm statement that contains the definition. Any valid C variable name is acceptable, including names already defined in the surrounding code. No two operands within the same asm statement can use the same symbolic name.

When not using an asmSymbolicName, use the (zero-based) position of the operand in the list of operands in the assembler template. For example if there are three output operands, use ‘%0’ in the template to refer to the first, ‘%1’ for the second, and ‘%2’ for the third.

无论你用-masm=intel 编译,这个版本都应该工作2-masm=att选项


脚注

  • 1虽然 NASM Intel 方言和 GAS (GNU Assembler) Intel 语法相似,但也存在一些差异。一是 NASM Intel 语法使用 [segment:disp+base+index*scale],其中可以在 [] 中指定段并且 GAS 的 Intel 语法需要在 segment:[disp+base+index*scale] 之外的段。
  • 2虽然代码可以运行,但您应该将所有这些基本功能放在 ioaccess.h 中直接归档并从 .c 中删除它们包含它们的文件。因为你把这些基本功能放在一个单独的.c文件(外部链接),编译器无法尽可能地优化它们。您可以将函数修改为 static inline 类型并将它们直接放在页眉中。然后,编译器将能够通过消除函数调用开销和减少额外加载和存储的需要来优化代码。您将希望使用高于 -O0 的优化进行编译.考虑 -O2-O3 .
  • 关于操作系统开发的特别说明:
    1. 有许多玩具操作系统(示例、教程,甚至 OSDev Wiki 上的代码)在优化时不起作用。许多失败是由于bad/poor inline assembly或使用未定义的行为。内联汇编应该作为最后的手段使用。如果您的内核运行时未进行优化,则很可能不是编译器中的错误(有可能只是不太可能)。
    2. 注意@PeterCordes 回答中有关可能触发 DMA 读取的端口访问的建议。

关于c - 移植 AT&T inline-asm inb/outb 包装器以与 gcc -masm=intel 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54962333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com