0

Why this assembly code assemble and link fine but show segment fault in runtime. Commented after the instruction to give a idea what I wanted to do.

  jmp short init

action:
  pop esi
  xor eax, eax
  mov byte [esi+24], al ;null terminating the string.
  mov dword [esi+25],24 ;length of the string

  mov al,4 ;syscall write
  mov ebx,1 ;standard out
  lea ecx,[esi]   ;<<---------- Unsure about this. probably load the address of the string to ecx  
  mov edx,[esi+25] ;<<-- load edx with string length
  int 80h


init:
  call action
  db "what a pity! not working#LLLL"

I am using NASM to assemble and ld to link. This program will run on a 64-bit machine but I want it to be 32-bit compatible.

3
  • Your string appears to be in section .text (which Nasm will default to, if you don't say) which is read-only memory. Attempting to alter it will segfault. If it ran, it would loop forever. I don't see a sys_exit in there... Commented Nov 3, 2013 at 6:15
  • Well the orginal program have a sys_exit. How to make it writiable? Commented Nov 3, 2013 at 12:56
  • Looks recursive to me - drops through to :init and calls action again... Commented Nov 4, 2013 at 18:14

1 Answer 1

1

You want to have the address of the string in ecx. So why do you pop esi ? Use pop ecx and you already have it in the correct register. You don't need it anyway for some other purpose.

Apart from that, you are writing to the code segment, which is not writable by default.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your recommendation to pop in ecx. But is here a problem with pop esi. Also how to make the code writable
section .text write will make your .o file writable. Dump it. See, writable! However, ld changes it back to readonly. :( section .kode exec write seems to work. I think there's a way to make ld do this with .text, but I don't know it. Better yet, don't!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.