why is segmention fault while printing?

后端 未结 2 1588
星月不相逢
星月不相逢 2021-01-28 05:16

This is my x86 assembly code:

section .data 
  output db \'%d\',10,0
section .text 
  global main
  extern printf 
main :
  xor ecx,ecx
  xor eax,eax
  mov eax,1         


        
相关标签:
2条回答
  • 2021-01-28 06:06

    print ends with a ret instruction, which implies that it is something that you should call. So jg print should be jng skip / call print / skip: (or just call print, because the > 0 check seems unnecessary).
    call places the return address on the stack, jg does not.

    0 讨论(0)
  • 2021-01-28 06:15

    The reason why you keep getting faults is: You are ignoring the stack!

    Your lable1 subroutine is correctly set up, but with your jg you eject from it ignoring the stack - leaving the stack corrupt.

    lable1:
      push ecx 
      push eax  
      cmp eax,0
      jg print
      pop eax
      pop ecx
      inc eax
    

    You

    xor eax,eax     ; EAX = 0 - breaking pipeline
    mov eax,1       ; EAX = 1 - which is redundant
    

    but because of your

    cmp eax,0
    jg print
    

    in the lable1 subroutine, you jump to the print: routine if EAX is greater than 0(and it is in the first iteration with EAX=1) with a stack offset of -8 caused by

    push ecx 
    push eax  
    

    in the beginning of lable1:. The retting from print:, which cleans up the stack properly by add esp,8 at the end, will cause your program to return to the address of the first entry of the stack, which you have assigned to [ESP]=(former EAX = 1) in main:mov eax,1.

    Therefore you get a SegFault, because you try to jump/return to the address [00000001] (=EAX=1,32bit).

    0 讨论(0)
提交回复
热议问题