assembly - 访问汇编中缓冲区的第一个字节?

标签 assembly x86

我正在尝试调用 isdigit,为此我需要缓冲区的第一个字节,其定义如下。

...

.equ ARRAYSIZE, 20

    .section ".bss"
buffer:
    .skip ARRAYSIZE

...

input:
    pushl $buffer
    pushl $scanFormat
    call  scanf
    addl  $8, %esp

因此,缓冲区被分配了 20 字节的内存空间,我使用 scanf 放置了一些值,如输入所示。

现在我想访问前 4 个字节以便调用 isdigit。我如何访问它们?

我最初的猜测是使用 movl 缓冲区 %eax,因为 eax 寄存器是 4 字节大小,并且会将前 4 个字节存储在缓冲区中。但我不确定它是如何工作的。

如果我只能访问缓冲区的前 4 个字节,或者使用任何其他方法将 isdigit 应用于前 4 个字节,请告诉我。谢谢。

最佳答案

您需要分别将isdigit应用于这4个字节。您可以使用执行 4 次迭代的循环从缓冲区中一一获取它们。计数器设置在 %ecx 寄存器中,指向缓冲区的指针设置在 %esi 寄存器中。

    movl    $4, %ecx          ; Counter
    movl    $buffer, %esi     ; Pointer
More:
    movsbl  (%esi), %eax      ; Get next byte sign-extending it
    push    %eax
    call    isdigit
    addl    $4, %esp
    ...
    incl    %esi              ; Advance the pointer
    decl    %ecx              ; Decrement the counter
    jnz     More              ; Continu while counter not exhausted

或者

    xorl    %esi, %esi        ; Offset start at 0
More:
    movsbl  buffer(%esi), %eax ; Get next byte sign-extending it
    push    %eax
    call    isdigit
    addl    $4, %esp
    ...
    incl    %esi              ; Advance the offset
    cmpl    $4, %esi          ; Test for max offset
    jb      More              ; Continu while offset not max-ed

关于assembly - 访问汇编中缓冲区的第一个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56208619/

相关文章:

assembly - 在哪里可以找到 x86 寄存器名称?

assembly - 当 CX 不是基址或索引寄存器时,为什么 CX 在 16 位汇编中的 '[ ]' 中工作?

c - _mm_lfence() 时间开销是不确定的?

c++ - 在x86_64模式下,寄存器中无法容纳64位数字

c - 外壳代码 : access violation exception for no obvious reason

assembly - 如何在汇编中打印字符串的长度

assembly - x86 的简单寄存器分配方案

visual-studio - 编译器在设置调用堆栈时生成了意外的 `IN AL, DX`(操作码 `EC`)

assembly - 如何在Linux上从c源代码生成nasm可编译的汇编代码?

c - 使用 _asm 指向成员的指针