轉自:http://blog.csdn.net/myhaspl/article/details/14140035
首先我們先用匯編編寫一個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表示字符串的終結符。
第一個參數最後一個入棧,按調用的相反順序入棧