码迷,mamicode.com
首页 > 其他好文 > 详细

汇编实现square函数

时间:2015-08-10 09:31:47      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

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

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函数

标签:

原文地址:http://my.oschina.net/guonaihong/blog/490007

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!