0

I have an array 4 ints at %r12. I'm assuming that the first element is just at %r12. So if I wanted to add a 32 bit value 1 to it, would I use the following code?

addl $1, (%r12)

How do I move to the other elements in the array to access them? Example, to add 1 to the second element, would I write

addl $1, 4(%r12)
Jester
  • 56,577
  • 4
  • 81
  • 125
WP0987
  • 179
  • 12
  • 1
    The statement `addl $1, %r12` adds one to the value stored in R12, not the value stored in the array pointed to by R12. – Ross Ridge Nov 27 '15 at 10:00
  • a quick google for `x86 addressing modes` found this: http://stackoverflow.com/questions/20608930/addressing-modes-in-assembly-language-ia-32-nasm. Remember that 32bit ints are 4 bytes wide. Also, it would be better to say that `r12` is pointing to an array. The word "at" isn't a very clear way to describe the situation. – Peter Cordes Nov 27 '15 at 10:39
  • I'm voting to close this question as off-topic because it's too trivial, and low quality. A quick google for asm arrays would turn up as much as you can expect someone to write in an answer. – Peter Cordes Nov 27 '15 at 10:56
  • Is there like a beginners, learners place to ask questions about assembly language for AT&T format? I don't even know enough about this to know how to ask the questions or to search for it. @PeterCordes the website you've cited is for Intel format which doesn't help me as I'm trying to write it in AT&T format. – WP0987 Nov 27 '15 at 11:55
  • @WP0987: Ok, that's a fair enough point. Since the Intel ref manuals are all written in NASM/Intel syntax, you'll need to be familiar with it to use the best x86 ISA reference manual. Translating between the two is fairly straightforward, just reverse the operands and drop the `%` and `$`. Addressing modes are more complicated, but a quick search for "gnu assembler x86 addressing modes" found a good resource: https://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Address_operand_syntax. – Peter Cordes Nov 27 '15 at 12:01
  • It sucks that things there are multiple different x86 syntaxes, but I'd recommend NASM first. Writing GNU C inline asm is harder than writing whole functions in asm, which you can do in AT&T or Intel syntax. – Peter Cordes Nov 27 '15 at 12:04
  • 1
    And re: your edit: Yes, that's correct now. Congrats on figuring it out for yourself. :) `addl $1, 4(%r12)` is equivalent to `add DWORD [r12+4], 1` in NASM syntax. `incl 4(%r12)` is slightly shorter (in machine-code bytes), and is a better choice in most cases. (See http://agner.org/optimize/ to learn the few cases when it's not a good choice.) – Peter Cordes Nov 27 '15 at 12:07
  • 1
    Also, the GNU assembler manual itself documents the AT&T syntax way of doing whatever you read about in a guide or tutorial that uses Intel syntax. (e.g. `.byte` instead of `DB` to encode literal data.) – Peter Cordes Nov 27 '15 at 12:09

0 Answers0