1

I am working on a NASM project for a class, and keep running into an issue when I try to move a register value to a memory address. I have a longer file, but the below code recreates the situation and outcome identically

section .text

global _start

_start:

    mov esi, x
    add esi, 2
    mov [x], esi

section .data
    x equ 2

When running this code (using the compiler at https://www.jdoodle.com/compile-assembler-nasm-online/), it returns the "command terminated by signal 11" message. the problem appears to be in the "mov [x], esi" line. Can someone explain what this issue is, and how I can correct it? A cursory google search indicates it is because I am overflowing the address [x], but I am not sure what to do with that information.

fuz
  • 88,405
  • 25
  • 200
  • 352
glaukon
  • 13
  • 4
  • What do you think happens after `mov [x], esi` is executed? – fuz Apr 24 '20 at 23:31
  • Also: I recommend to avoid using online assemblers for learning assembly. Debugging is a lot harder if you don't run your code on your own system. Just get yourself Linux and learn from there. – fuz Apr 24 '20 at 23:36
  • @fuz, The indicated duplicate isn’t really the problem. The fault is in the mov instruction. (But, if the mov instruction didn’t fault, then you would hit the problem in the duplicate.) – prl Apr 25 '20 at 00:08
  • @prl Well then... shouldn't answer question while being dead tired... – fuz Apr 25 '20 at 00:09
  • 1
    The [former duplicate](https://stackoverflow.com/questions/49674026/what-happens-if-there-is-no-exit-system-call-in-an-assembly-program) might still be worth a read because that's going to be the problem after you fix the one that's currently crashing your program. Which is that you wrote `x equ 2` instead of `x dd 2`. – fuz Apr 25 '20 at 00:10

1 Answers1

2

The equ pseudo-op gives the symbol x the value 2, so the mov instruction is trying to write to address 2, which is a protected memory address on most OSes.

To define x as a symbol in the data section, write it as a label:

 x:

To provide space in the data section, use a different pseudo-op that reserves space. For example, dd, which also lets you give a value to the space:

x: dd 2

This defines a dword (4 bytes) with the value 2.

Once you fix this problem, you will get another different fault, as described in What happens if there is no exit system call in an assembly program?.

prl
  • 11,716
  • 2
  • 13
  • 31
  • You probably also want to change the first line to `mov esi, [x]` to load the value of x rather than its address. – prl Apr 25 '20 at 00:22
  • Thank you so much for your responses. These solved the problem, and things make more sense. Much appreciated – glaukon Apr 25 '20 at 14:13