如果我将一个参数传递给一个函数,它应该用汇编语言翻译成push something. 我用 C 编写了以下代码:
#include <stdio.h>
int sum(int a, int b,int c)
{
int total;
total = a + b + c;
printf ("I will return now");
return(total);
}
int media(int a, int b,int c)
{
int total;
total = (a + b + c)/3;
printf ("\nI will return now\n");
return (total);
}
int main ()
{
int num1,num2,num3;
char keypress[1];
num1 = 5;
num2 = 10;
num3 = 15;
printf ("\nCalling sum function\n");
sum(num1,num2,num3);
printf ("\nWaiting a keypress to call media function\n");
scanf ("%c",keypress);
media(num1,num2,num3);
printf ("\nWaiting a keypress to end\n");
scanf ("%c",keypress);
return(0);
}
我编译它:gcc -S example.c。它生成了一个example.s文件,我期望看到一些推送指令。这是生成的汇编代码:
.file "example.c"
.section .rodata
.LC0:
.string "I will return now"
.text
.globl sum
.type sum, @function
sum:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
movl %esi, -24(%rbp)
movl %edx, -28(%rbp)
movl -24(%rbp), %eax
movl -20(%rbp), %edx
addl %eax, %edx
movl -28(%rbp), %eax
addl %edx, %eax
movl %eax, -4(%rbp)
movl $.LC0, %edi
movl $0, %eax
call printf
movl -4(%rbp), %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size sum, .-sum
.section .rodata
.LC1:
.string "\nI will return now"
.text
.globl media
.type media, @function
media:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
movl %esi, -24(%rbp)
movl %edx, -28(%rbp)
movl -24(%rbp), %eax
movl -20(%rbp), %edx
addl %eax, %edx
movl -28(%rbp), %eax
addl %edx, %eax
movl %eax, -4(%rbp)
movl -4(%rbp), %ecx
movl $1431655766, %edx
movl %ecx, %eax
imull %edx
movl %ecx, %eax
sarl $31, %eax
movl %edx, %ecx
subl %eax, %ecx
movl %ecx, %eax
movl %eax, -4(%rbp)
movl $.LC1, %edi
call puts
movl -4(%rbp), %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size media, .-media
.section .rodata
.LC2:
.string "\nCalling sum function"
.align 8
.LC3:
.string "\nWaiting a keypress to call media function"
.LC4:
.string "%c"
.LC5:
.string "\nWaiting a keypress to end"
.text
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $5, -4(%rbp)
movl $10, -8(%rbp)
movl $15, -12(%rbp)
movl $.LC2, %edi
call puts
movl -12(%rbp), %edx
movl -8(%rbp), %ecx
movl -4(%rbp), %eax
movl %ecx, %esi
movl %eax, %edi
call sum
movl $.LC3, %edi
call puts
leaq -13(%rbp), %rax
movq %rax, %rsi
movl $.LC4, %edi
movl $0, %eax
call __isoc99_scanf
movl -12(%rbp), %edx
movl -8(%rbp), %ecx
movl -4(%rbp), %eax
movl %ecx, %esi
movl %eax, %edi
call media
movl $.LC5, %edi
call puts
leaq -13(%rbp), %rax
movq %rax, %rsi
movl $.LC4, %edi
movl $0, %eax
call __isoc99_scanf
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size main, .-main
.ident "GCC: (Debian 4.7.2-5) 4.7.2"
.section .note.GNU-stack,"",@progbits
当然,我遗漏了一些东西,因为我只看到了两条pushq说明。
我的问题是我错过了什么?