Jimmy Suh
Jimmy Suh

Reputation: 212

Accessing the first byte of buffer in assembly?

I am trying to call isdigit, and to do so I need first byte of my buffer, which is defined as following.

...

.equ ARRAYSIZE, 20

    .section ".bss"
buffer:
    .skip ARRAYSIZE

...

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

Thus, buffer is allocated a memory space of 20 byte and I put some value using scanf, as shown in input.

Now I want to access the first 4 bytes in order to call isdigit. How can I access them?

My initial guess is to use movl buffer, %eax, since eax register is 4 byte-size, and will store the first 4 bytes in buffer. But I am not sure this is how it works.

Please let me know if I can only access the first 4 bytes of buffer, or any other methods to apply isdigit to those first 4 bytes. Thank you.

Upvotes: 2

Views: 1117

Answers (1)

Sep Roland
Sep Roland

Reputation: 39166

You will want to apply isdigit to those 4 bytes separately. You can fetch them from the buffer one by one using a loop that does 4 iterations. The counter is setup in the %ecx register and a pointer to the buffer is setup in the %esi register.

    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

Alternatively

    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

Upvotes: 2

Related Questions