Why does adding a test instruction cause a segmentation fault in GNU assembly?

StackOverflow https://stackoverflow.com/questions/21079911

  •  27-09-2022
  •  | 
  •  

Question

I'm having trouble tracking down a segmentation fault. After compiling my c program to assembly, I'm editing it and adding a few things.

I added some code, including this section:

.SB1:
         call fib
         jmp     .LBL2

That part works fine. But now I want to call test and maybe jump to another label. Right now, I'm just playing with it to see if I can learn how things work (taking baby steps). So I changed the code to this:

.SB1:
         call fib
         test    %esp, 0xfffffff
         jz      .SB2
         jmp     .LBL2
.SB2:
         jmp     .LBL2

But now I get a segmentation fault. Anyone know why? If you need more information or want the code to reproduce it let me know.

Était-ce utile?

La solution

In AT&T syntax, a literal needs to be prefixed with $. Then, also the operands need to be switched:

test    $0xfffffff, %esp

Without the $ prefix, the assembler assumes a memory address, and accessing address 0xfffffff is most likely out of your mapped memory, which causes the segmentation fault.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top