我是汇编编程的新手,我从头开始读编程-乔纳森·巴特利特(Jonathan Bartlett)。我正在学习x86架构并在Ubuntu 16.04LTS中运行我的所有代码。我将代码编译为32位,但我的处理器为64位。这是我为数字7
section .data
.section .text
.globl _start
_start:
pushl $7
call get
addl $4,%esp #pushes the sp to the top of the stack,before the value 7 was pushed on the stack
movl $1,%eax
int $0x80
get:
pushl %ebp
movl %esp,%ebp #esp and ebp point to the same memory address
subl $4,%esp # pushing the stack pointer to point to the next memory address
movl 8(%ebp),%eax #storing the value of 7 in eax register
movl %eax,-4(%ebp) #moves eax register on top of the stack
movl %eax,%ebx #ebx now has the value of 7
fac:
cmpl $1,%ebx #checks if ebx is 1 so that it can stop finding the factorial of a number
je end_loop
decl %ebx #decrements ebx so that it can keep multiplying e.g: 7*6*etc
imull %ebx,%eax #stores the value in the eax register when multiplied
jmp fac #keeps repeating till the ebx becomes 1
end_loop:
popl %ebx #pops the value at the sp, which is 5040,into the ebx register
popl %ebp #pops the value at esp and puts it in ebp
ret #returns and pops the return address of the stack, now the sp is pointing to 7
7
阶乘的输出应为5040
,但是当我进行编译和链接时,我会不断得到176
。使用命令:
as --32 -gstabs -o factorial.o factorial.s
ld -m elf_i386 -o factorial factorial.o
./factorial
echo $?
执行“ echo$?
”时,我会不断获取176
我在gdb中运行了代码,并且ebx的值一直显示到5040。
有人可以帮我吗,引起我很好奇,即使gdb中的答案显示为176
,编译器为何仍显示5040
。