0

So I'm trying to write a function to zero out an input buffer in static memory in my assembly program. It looks like this:

zero_input_buffer:
    mov $0, %ebx
    mov $29, %eax

    # not sure how to implement this line
    mov %ebx, input(%rip, %eax, 1)

    dec %eax
    cmp %eax, %ebx
    jle zero_input_buffer_done
    jmp zero_input_buffer
zero_input_buffer_done:
    retq

The input buffer 30 bytes long. In each loop iteration, I'm trying to 0 out %eax bytes from the input symbol. I am also trying to use rip relative addressing, just because that's what I've seen done in examples that I've seen. I know to access input starting from the first byte I can do input(%rip), but I'm not sure how to add the %eax offset. I've looked around on the internet, but nothing I try seems to work. Any help / general x64 resources would be appreciated

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Dyskord
  • 365
  • 5
  • 14
  • See the linked duplicate for the RIP-relative part. But you have multiple other bugs, like an infinite loop (resetting the loop counter inside the loop), and storing 4 bytes at every byte offset, so if you start with a pointer to the last byte, you'll write outside the buffer. Just use two partially-overlapping `movups %xmm0, input+14(%rip)` and `movups %xmm0, input(%rip)` like glibc `memset` would do to zero 30 bytes, after `xorps %xmm0, %xmm0`. Also, prefer 64-bit registers in addressing-modes, usually no reason to truncate a pointer or offset to 32-bit. – Peter Cordes Apr 18 '22 at 03:44
  • thank you! also, I was originally just going to call memset or bzero, but I couldn't find good resources on how to call it from assembly, do you know where I could find resources on figuring out how to call it? – Dyskord Apr 18 '22 at 03:53
  • You just call them the same way a compiler does; look at its asm output ([How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116)). See also [Can't call C standard library function on 64-bit Linux from assembly (yasm) code](https://stackoverflow.com/q/52126328) although that's mostly NASM-style Intel syntax. Also see [Calling printf in x86\_64 using GNU assembler](https://stackoverflow.com/q/38335212) re: calling-convention stuff, and the AT&T syntax. In this case, for a 16 to 32-byte memset, inlining takes 2 to 3 instructions, fewer than calling. – Peter Cordes Apr 18 '22 at 04:01
  • 1
    Thank you! With this help I was able to finish my program. It helped to just look at the compiler output to see how the functions were called – Dyskord Apr 18 '22 at 20:21

0 Answers0