【问题标题】:Program received signal SIGSEGV, Segmentation fault. in assembly when I call printf程序收到信号 SIGSEGV,分段错误。在我调用 printf 时组装
【发布时间】:2015-12-13 09:44:53
【问题描述】:

我正在尝试在汇编中编写代码。 当我调用 printf - 它会打印必要的字符串,但它会在它之后返回 seg 错误。

请帮帮我。

.type    pstrijcpy, @function
.globl pstrijcpy
pstrijcpy:
pushl   %ebp
movl    %esp            ,%ebp
pushl   %ebx
xorl    %ebx            ,%ebx
xorl    %edx            ,%edx   #set %edx to 0
xorl    %ecx            ,%ecx
xorl    %eax            ,%eax       
movl    8(%ebp)         ,%eax   #pointer of the dst
movl    12(%ebp)        ,%edx   #pointer of src 
movb    16(%ebp)        ,%ch    #move char i to %ch
movb    20(%ebp)        ,%cl    #%cl = j
movb    %ch             ,%bl 
cmpb    %cl             ,(%eax) #if dst.size < j
jl      .printError
leal    (%eax, %ebx)    ,%eax   #move %eax to the beginning of the string after i
cmpb    %cl             ,(%edx) #if src.size < j
jl      .printError
leal    (%edx, %ebx)    ,%edx   #move %edx to the beginning of the string after i
xorl    %ebx            ,%ebx
.whileISmallerThanJ:
movb    (%edx)          ,%bl 
movb    %bl             ,(%eax) #dst[i] = src[i]
addb    $1              ,%ch    #i++
leal    1(%edx)         ,%edx
leal    1(%eax)         ,%eax
cmpb    %cl             ,%ch 
jle     .whileISmallerThanJ
.finishFunctionCopy:
movl    8(%ebp)         ,%eax   #pointer to the first char of the string, for the return value
popl    %ebx
popl    %ebp
ret     
.printError:
pushl   $error                  #push the string for printf
call    printf
jmp .finishFunctionCopy

    .section    .rodata         #read only data
readDecimal:    .string "%d"    #for scanf
error:          .string "invalid input!\n"

【问题讨论】:

    标签: assembly x86 segmentation-fault printf


    【解决方案1】:

    我认为您缺少使用 cdecl 调用约定进行的调用者清理工作。尝试在call printf 之后添加add $4, %esp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2018-04-09
      • 1970-01-01
      相关资源
      最近更新 更多