算法下午茶系列-重温汇编(3)[调用C库函数]

首先我们先用汇编编写一个helloworld,注意我们直接在汇编代码中调用C语言的printf函数将"hello,world\n" 输出在屏幕上。

.section .data
  output:
  .asciz "hello,world\n"  
.section .text
   .global  main
   main:
   push $output
   call printf
   addl $4,%esp
   push $0
   call exit

 

上述代码中,

push $output将参数入栈,以便printf调用,

然后调用printf,printf会在栈中取出它需要的参数
2)我们直接使用GCC编译后运行 

deepfuture@ubu-s:~$ gcc -o  test test.s
deepfuture@ubu-s:~$ ./test
hello,world

 

3)那么调用C库函数所需要的参数入栈的顺序是什么?

再看一个例子

.section .data
  myvalue:
     .byte 67,68,69,70,0
  mygs:
     .asciz "%s\n"
   
.section .text
.globl main
   main:
    movl $myvalue,%ecx
    push %ecx
    push $mygs    
    call printf
    push $0
    call exit

        
    
    
67,68,69,70是C、D、E、F的ASCII码,0是字符串终结符
 这段代码的功能是输出“CEDF”,相当于下面的C代码

 

#include <stdio.h>
int main( void )
{
     char myvalue[]={67,68,69,70,0};
     printf( "%s\n" ,myvalue);
     return 0;
}

 其中,后面的0表示字符串的终结符。

 

第一个参数最后一个入栈,按调用的相反顺序入栈

 

 

如果转载请注明来源,如有错误之处,请及时指出。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值