首先我们先用汇编编写一个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表示字符串的终结符。
第一个参数最后一个入栈,按调用的相反顺序入栈
如果转载请注明来源,如有错误之处,请及时指出。
本文介绍如何在汇编语言中调用C语言的printf函数输出hello,world
,并直接使用GCC编译运行,演示了参数入栈的顺序及输出效果。同时解释了调用C库函数所需参数的入栈顺序,并提供了相关C代码作为辅助说明。

被折叠的 条评论
为什么被折叠?



