如何解决为什么在64位程序中%esp和%ebp的寄存器值始终为负?
这是一个GNU汇编程序。
.code32 #must add this line,otherwise,this program can't be complied.
.section .data
.section .text
.global _start
_start:
pushl $3 #push second argument
pushl $2 #push first argument
call power #call the function
addl $8,%esp #move the stack pointer back
pushl %eax #save the first answer before calling the next function
pushl $2 #push second argument
pushl $5 #push first argument
call power #call the function
addl $8,%esp #move the stack pointer back
#The second answer is already
#in %eax. We saved the
#first answer onto the stack,#so now we can just pop it
#out into %ebx
popl %ebx
addl %eax,%ebx #add them together
movl $1,%eax #exit (%ebx is returned)
int $0x80
.type power,@function
#first arg-> base number
#second arg-> the power to raise
power:
pushl %ebp #save old base pointer
movl %esp,%ebp #make stack pointer the base pointer
subl $4,%esp #get room for our local storage
movl 8(%ebp),%ebx
movl 12(%ebp),%ecx
movl %ebx,-4(%ebp)
power_loop_start:
cmpl $1,%ecx
je end_power
movl -4(%ebp),%eax
imull %ebx,%eax
movl %eax,-4(%ebp)
decl %ecx
jmp power_loop_start
end_power:
movl -4(%ebp),%eax
movl %ebp,%esp
popl %ebp
ret
添加了.code32
指令后即可编译。
$ as -g hello.s -o hello.o
$ ld hello.o -o hello
但是,它在运行时始终显示“段故障”。
我发现使用GDB进入power
时,%esp和%ebp的寄存器始终为负。
Breakpoint 1,_start () at hello.s:9
9 pushl $3
(gdb) n
_start () at hello.s:10
10 pushl $2
(gdb)
_start () at hello.s:11
11 call power
(gdb) s
power () at hello.s:35
35 pushl %ebp
(gdb) n
36 movl %esp,%ebp
(gdb)
37 subl $4,%esp
(gdb) n
power () at hello.s:45
45 movl 8(%ebp),%ebx
(gdb) print $ebp
$1 = -8144
(gdb) print $esp
$2 = -8148
(gdb)
如何解决此问题。我的操作系统是Ubuntu。
Linux locomotive-VirtualBox 5.4.0-52-generic #57~18.04.1-Ubuntu SMP Thu Oct 15 14:04:49 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。