汇编实现square函数

square函数主要计算一个数的平方

这个函数主要用到乘法指令imull

code:

.section .data
.section .text

.global _start
fmt:
    .ascii "%d\n\0"

_start:
    pushq $2
    call square
    addq $8, %rsp               #复位%rsp

    movl %eax, %esi    
    movq $fmt, %rdi    
    xorl %eax, %eax    
    call printf            #调用printf

    movl $0, %edi
    call exit              #调用exit

.type square, @function
square:
    pushq %rbp
    movq %rsp, %rbp
    
    movl 16(%rbp), %eax
    imull %eax, %eax

    movq %rbp, %rsp
    popq %rbp
    ret

编译命令:

gcc -c square.s -g && ld ./square.o -lc -dynamic-linker /lib64/ld-linux-x86-64.so.2 && ./a.out


  TODO

  解释movl 16(%rbp), %eax指令的意思。 

你可能感兴趣的:(汇编实现square函数)