elftest.asm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ; test source file for assembling to ELF
  2. ; build with:
  3. ; nasm -f elf elftest.asm
  4. ; gcc -o elftest elftest.c elftest.o
  5. ; (assuming your gcc is ELF)
  6. ; This file should test the following:
  7. ; [1] Define and export a global text-section symbol
  8. ; [2] Define and export a global data-section symbol
  9. ; [3] Define and export a global BSS-section symbol
  10. ; [4] Define a non-global text-section symbol
  11. ; [5] Define a non-global data-section symbol
  12. ; [6] Define a non-global BSS-section symbol
  13. ; [7] Define a COMMON symbol
  14. ; [8] Define a NASM local label
  15. ; [9] Reference a NASM local label
  16. ; [10] Import an external symbol
  17. ; [11] Make a PC-relative call to an external symbol
  18. ; [12] Reference a text-section symbol in the text section
  19. ; [13] Reference a data-section symbol in the text section
  20. ; [14] Reference a BSS-section symbol in the text section
  21. ; [15] Reference a text-section symbol in the data section
  22. ; [16] Reference a data-section symbol in the data section
  23. ; [17] Reference a BSS-section symbol in the data section
  24. ; [18] Define a non-global rodata-section symbol
  25. BITS 32
  26. GLOBAL lrotate ; [1]
  27. GLOBAL greet ; [1]
  28. GLOBAL asmstr ; [2]
  29. GLOBAL textptr ; [2]
  30. GLOBAL selfptr ; [2]
  31. GLOBAL integer ; [3]
  32. EXTERN printf ; [10]
  33. COMMON commvar 4 ; [7]
  34. SECTION .text
  35. ; prototype: long lrotate(long x, int num);
  36. lrotate: ; [1]
  37. push ebp
  38. mov ebp,esp
  39. mov eax,[ebp+8]
  40. mov ecx,[ebp+12]
  41. .label rol eax,1 ; [4] [8]
  42. loop .label ; [9] [12]
  43. mov esp,ebp
  44. pop ebp
  45. ret
  46. ; prototype: void greet(void);
  47. greet mov eax,[integer] ; [14]
  48. inc eax
  49. mov [localint],eax ; [14]
  50. push dword [commvar]
  51. mov eax,[localptr] ; [13]
  52. push dword [eax]
  53. push dword [integer] ; [1] [14]
  54. push dword printfstr ; [13]
  55. call printf ; [11]
  56. add esp,16
  57. ret
  58. SECTION .data
  59. ; a string
  60. asmstr db 'hello, world', 0 ; [2]
  61. ; a string for Printf
  62. printfstr db "integer==%d, localint==%d, commvar=%d"
  63. db 10, 0
  64. ; some pointers
  65. localptr dd localint ; [5] [17]
  66. textptr dd greet ; [15]
  67. selfptr dd selfptr ; [16]
  68. SECTION .bss
  69. ; an integer
  70. integer resd 1 ; [3]
  71. ; a local integer
  72. localint resd 1 ; [6]
  73. SECTION .rodata
  74. readonly dd readonly ; [18]