0

I am trying to modify a variable declared in the .data section and nothing seems to work. Online sources have said that I should do something like add [x], 25 but that isn't working either. The tricky thing about this is that I have found examples that claim to work but they all have ATT syntax, but I'm using Intel. Does anyone know what I am doing wrong and how I can solve my problem?

I'm assembling on MacOS with GAS: gcc -masm=intel modify_var.asm

    .global _main
    .text

_main:
    add x, 25


    mov rdi, 0
    mov rax, 0x2000001
    syscall

    .data

x:
    .long 25

Caspian Ahlberg
  • 934
  • 10
  • 19
  • gas syntax is `add dword ptr [x+rip], 25` – Jester Oct 07 '20 at 16:49
  • @Jester Thanks, I'll try that. I've done some research on the rip register, and it doesn't seem connected to this - can you explain how that works? – Caspian Ahlberg Oct 07 '20 at 16:57
  • 2
    rip is the instruction pointer also known as the program counter. It sequences the instructions; when an instruction starts running rip points to that instruction (during execution of that instruction it points just after the instruction). Using rip relative addressing offers [position independent code](https://en.wikipedia.org/wiki/Position-independent_code). – Erik Eidt Oct 07 '20 at 17:09
  • @ErikEidt That makes sense. – Caspian Ahlberg Oct 07 '20 at 17:15
  • @ErikEidt This is a bit unrelated, but how would I push a variable onto the stack? This doesn't seem to work: `push dword ptr [x + rip] ` – Caspian Ahlberg Oct 07 '20 at 17:18
  • You can't push a dword in 64 bit mode. You have to go through a register or have a 64 bit variable. – Jester Oct 07 '20 at 17:19
  • @Jester A 64 bit variable seems useful here then - would that be too much overhead or is that an okay practice? – Caspian Ahlberg Oct 07 '20 at 17:21
  • 2
    Variable sizes are dictated by the intended usage, not primarily by the available instructions (although that can be taken into account). – Jester Oct 07 '20 at 17:23
  • @Jester Thanks, got it – Caspian Ahlberg Oct 07 '20 at 17:25
  • What was the point of your edit? The linked duplicate shows the syntax for RIP-relative addressing for GAS AT&T and `.intel_syntax`, as well as NASM syntax. – Peter Cordes Oct 08 '20 at 12:49
  • @PeterCordes 2 things! 1. First, I thought that that example only showed ATT syntax, but I was wrong. 2, that example didn't help me solve my problem, Jester solved that for me. – Caspian Ahlberg Oct 08 '20 at 12:52
  • Answers in comments don't fully count for Stack Overflow to consider a question "solved", that's why I closed it as a duplicate that explained the same thing Jester did in comments. You asked a followup in comments about allowed `push` operand-sizes, but that's not part of the question you posted. If you'd searched yourself, there are several existing Q&As about push operand-size, all unrelated to the basics of how to address static data in the first place. – Peter Cordes Oct 08 '20 at 12:58
  • @PeterCordes Got it – Caspian Ahlberg Oct 08 '20 at 14:03

0 Answers0