1. 程式人生 > >C指針原理(4)-AT&T匯編

C指針原理(4)-AT&T匯編

glob 在屏幕上 匯編 output sci urn $0 編譯 什麽

首先我們先用匯編編寫一個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指針原理(4)-AT&T匯編