汇编 - 程序不返回任何内容

时间:2014-09-30 05:49:28

标签: assembly x86-64

我一直在学习集会哈哈。我试图让程序进行以下计算:2 ^ 3 + 5 ^ 2。

我尝试使用gdb进行调试,但我对此并不了解,互联网搜索也没有透露太多信息。 该程序适用于x86-64。程序编译得很好,但在输入echo $?之后,我预计会33,但会得到0

以下是代码:

  1 .section .data
  2 .section .text
  3 .globl _start
  4 _start:
  5         pushq $3 #push the 2nd arg
  6         pushq $2 #push the 1st arg
  7 
  8         call power #call function
  9         addq $16, %rsp #move stack back 16 bytes to get rid of first two params
 10 
 11         pushq %rax #save the first answer before
 12 
 13         pushq $2 #push the 2nd arg
 14         pushq $5 #push the 1st arg
 15 
 16         call power #call function
 17         addq $16, %rsp #move stack back 16 bytes to get rid of first two params
 18         popq %rbx #2nd answer already in rax. We saved saved first answer onto stack and now pop it into rbx.
 19 
 20         addq %rax, %rbx #add together
 21 
 22         movq $60, %rax #exit
 23         syscall
 24 
 25 .type power, @function
 26 power:
 27         pushq %rbp #save old base pointer
 28         movq %rsp, %rbp #make stack pointer base pointer
 29         subq $8, %rsp #get room for local storage
 30 
 31         movq 16(%rbp), %rbx #put 1st arg in rbx
 32         movq 24(%rbp), %rcx # put 2nd arg in rbx
 33 
 34         movq %rbx, -8(%rbp) #store current result
 35 
 36 power_loop_start:
 37         cmpq $1, %rcx #if power is one, we are done
 38         je end_power #jump to end_power
 39 
 40         movq -8(%rbp), %rax #move current result into rax
 41         imulq %rbx, %rax #multiply current result by base number
 42 
 43         movq %rax, -8(%rbp) #store the current result
 44         decq %rcx #decrease the power
 45 
 46         jmp power_loop_start
 47 
 48 end_power:
 49         movq -8(%rbp), %rax #return value back to rax
 50         movq %rbp, %rsp #restore stack pointer
 51         popq %rbp #restore base pointer
 52         retq

提前谢谢。

1 个答案:

答案 0 :(得分:1)

因为您希望将结果值作为退出代码返回,所以您将结果存储到寄存器rdi中作为系统调用exit的参数。简而言之,请在第21行添加以下行:
movq %rbx, %rdi