首页 > 解决方案 > 访问汇编中缓冲区的第一个字节?

问题描述

我正在尝试调用 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 个字节的方法。谢谢你。

标签: assemblyx86

解决方案


您需要将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

推荐阅读