Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从汇编代码中为整数调用C函数printf_C_Linux_Assembly_Printf_64 Bit - Fatal编程技术网

如何从汇编代码中为整数调用C函数printf

如何从汇编代码中为整数调用C函数printf,c,linux,assembly,printf,64-bit,C,Linux,Assembly,Printf,64 Bit,从汇编代码调用printf时遇到问题。我的函数end_power用于打印power的结果,但每当我调用printf时,就会出现分段错误。(我在linux上以64位运行该程序)唯一不起作用的部分是end_power函数,更具体地说是调用printf时涉及的行 # PURPOSE: This function is used to compute # the value of a number raised to # a power. # # INPUT: First a

从汇编代码调用printf时遇到问题。我的函数end_power用于打印power的结果,但每当我调用printf时,就会出现分段错误。(我在linux上以64位运行该程序)唯一不起作用的部分是end_power函数,更具体地说是调用printf时涉及的行

# PURPOSE: This function is used to compute
#       the value of a number raised to
#       a power.
#
# INPUT:   First argument - the base number
#       Second argument - the power to
#       raise it to
#
# OUTPUT:  Will give the result as a return value
#
# NOTES:   The power must be 1 or greater
#
# VARIABLES:
#
#       %rbx - holds the base number
#       %rcx - holds the power
#
#       -8(%rbp) - holds the current result
#
#       %rax is used for temporary storage
#

.type power, @function
power:

    pushq   %rbp              # save old base pointer
    movq    %rsp, %rbp        # make stack pointer the base pointer
    subq    $8, %rsp          # get room for our local storage

    movq    16(%rbp), %rbx    # put first argument in %rax
    movq    24(%rbp), %rcx    # put second argument in %rcx

    movq    %rbx, -8(%rbp)    # store current result

power_loop_start:

    cmpq    $1, %rcx          # if the power is 1, we are done
    je      end_power

    movq    -8(%rbp), %rax    # move the current result into %rax
    imulq   %rbx, %rax        # multiply the current result by
                          # the base number
    movq    %rax, -8(%rbp)    # store the current result
    decq    %rcx              # decrease the power
    jmp     power_loop_start    # run for the next power

end_power:

    movq    -8(%rbp), %rdi    # return value goes in %rdi

    pushq -8(%rbp)
    pushq $fmtdec
    call printf
    add $16, %rsp

    movq    %rbp, %rsp        # restore the stack pointer
    popq    %rbp              # restore the base pointer
    ret

64位Linux的调用约定与32位Linux的调用约定有很大不同。看看:

改变

pushq -8(%rbp)
pushq $fmtdec
call printf
add $16, %rsp


考虑到这会覆盖RDI中以前的“返回值”。

您是否尝试执行<代码> PrtTf(i)< /C>或<代码> PrimTf(“%d”,i)< /C>?您是否尝试过在C中编写等价物,然后查看编译器组装的内容?(即,
gcc-S
)调用约定完全不同。
%rdi
中的第一个参数(格式字符串指针),以及
%rsi
中的下一个参数!感谢您打印了两个数字相加,但没有重新计算结果。我试着在电源恢复后将其移到主功能中,但还没有成功地将其改为主功能。但是谢谢你给我指出了正确的方向。
mov $fmtdec,%rdi
mov -8(%rbp),%rsi
xor %eax, %eax
call printf