gcc 어 셈 블 리

  http://blog.chinaunix.net/u1/48280/showart_1423294.html

1.gcc
(1). gcc ?
  1. static void * __memcpy(void * to, const void * from, size_t n) 
  2. int d0,d1,d2; 
  3. __asm__ __volatile__( 
  4.    "rep;movsl/n/t" 
  5.    "testb $2,%b4/n/t" 
  6.    "je 1f/n/t" 
  7.    "movsw/n" 
  8.    "1:/ttestb $1,%b4/n/t" 
  9.    "je 2f/n/t" 
  10.    "movsb/n" 
  11.    "2:" 
  12.    :"=&c" (d0), "=&D" (d1), "=&S" (d2) 
  13.    :"0" (n/4), "q" (n), "1" ((long) to), "2" ((long) from) 
  14.    :"memory"); 
  15. return (to); 

0,1,2 3,5,6 :
a. 3,5,6 n/4,to from ecx,edi,esi ;
b. 0,1,2 ecx,edi,esi d0,d1,d2 。
c. "&" , , 0 3 ecx, "0" ,
"0" (n/4) "c" (n/4), "=&c" 。
(2). gcc "&" ?
"&" ,
  1. int bar,foo; 
  2. __asm__ __volatile__( 
  3. "call func /n/t" 
  4. "mov ebx,%1" 
  5. :"=a" (foo) 
  6. :"r" (bar)); 

gcc bar eax , "=a" "=&a", foo eax, bar 。
(3). _start main ?
main gcc , ld as _start。libc _start main 。
a. _start :as -o a.o a.s,ld -o a a.o; gcc -g -c a.s,ld -o a a.o。( gcc -g,
gcc -o a a.s gcc main , libc _start 。 gcc -o a a.s
, " _start" " main")
b. main C :gcc -o a a.s;gcc -o a a.c。
(4).section .previous
.section .previous , , section ( .text ),
。.section .previous 。
2. AT&T
(1).data,.section , , assembler
(2).section , .data,.text,.bss
(3).globl export,
(4).bss ,
(5) open,read int $80
(6)MOVL $FOO,%EAX FOO EAX , MOVL FOO,%EAX FOO EAX
(7).include " ",
(8) ? c :
  1. struct para 
  2. char Firstname[40]; 
  3. char Lastname[40]; 
  4. char Address[240]; 
  5. long Age;//4 bytes 
  6. .section data 
  7. record1: 
  8. .ascii "Fredrick/0" 
  9. .rept 31 #Padding to 40 bytes 
  10. .byte 0 
  11. .endr 
  12. .ascii "Bartlett/0" 
  13. .rept 31 #Padding to 40 bytes 
  14. .byte 0 
  15. .endr 
  16. .ascii "4242 S Prairie/nTulsa, OK 55555/0" 
  17. .rept 209 #Padding to 240 bytes 
  18. .byte 0 
  19. .endr 
  20. .long 45 

.rept n .endr n ,
(9) , :
as write-records.s -o write-records.o (gcc -g -c write-records.s)
as write-record.s -o write-record.o (gcc -g -c write-record.s)
ld write-record.o write-records.o -o write-records
(10) ?
#helloworld-lib.s
.section .data
helloworld:
.ascii "hello world/n/0"
.section .text
.globl _start
_start:
pushl $helloworld
call printf
pushl $0
call exit
as helloworld-lib.s -o helloworld-lib.o
ld -dynamic-linker /lib/ld-linux.so.2 -o helloworld-lib helloworld-lib.o -lc
:ld -shared write-record.o read-record.o -o librecord.so
(11)
as write-record.s -o write-record.o
as read-record.s -o read-record.o
ld -shared write-record.o read-record.o -o librecord.so
as write-records.s -o write-records.o
ld -L . -dynamic-linker /lib/ld-linux.so.2 -o write-records -lrecord write-records.o
write-records /etc/ld.so.conf , ldconfig
(12)
as --gstabs a.s -0 a.o gcc -g -c a.s

(1)
#Parameter #N <--- N*4+4(%ebp)
#...
#Parameter 2 <--- 12(%ebp)
#Parameter 1 <--- 8(%ebp)
#Return Address <--- 4(%ebp)
#Old %ebp <--- (%ebp)
#Local Variable 1 <--- -4(%ebp)
#Local Variable 2 <--- -8(%ebp) and (%esp)
(2)
  1. .include "external_func.s" 
  2. .section .data 
  3. data_array:      # long  
  4. .long 3,67,34,0          
  5. data_strings:     #  
  6. .ascii "Hello there/0" 
  7. data_long: # long  
  8. .long 5 
  9. .section .bss 
  10. .lcomm my_buffer, 500    # 500  
  11. .section .text 
  12. .equ LINUX_SYSCALL, 0x80 # LINUX_SYSCALL 0x80 
  13. .globl _start 
  14. _start: 
  15. pushl %edx 
  16. movl data_long,%edx      # data_long edx  
  17. movl $data_long,%edx     # data_long edx  
  18. popl %edx 
  19. pushl $3       #push second argument 
  20. pushl $2       #push first argument 
  21. call power     #call the function 
  22. addl $8, %esp #move the stack pointer back 
  23. pushl %eax     #save the first answer before,calling the next function 
  24. movl $1, %eax #exit (%ebx is returned) 
  25. int $LINUX_SYSCALL      
  26. .type power, @function # power 
  27. power: 
  28. pushl %ebp          #save old base pointer 
  29. movl %esp, %ebp     #make stack pointer the base pointer 
  30. subl $4, %esp       #get room for our local storage 
  31. movl 8(%ebp), %eax #put first argument in %eax 
  32. movl 12(%ebp), %ebx #put second argument in %ebx 
  33. imull %ebx,%eax 
  34. movl %ebp, %esp     #restore the stack pointer 
  35. popl %ebp           #restore the base pointer 
  36. ret 

좋은 웹페이지 즐겨찾기