1 ; test source file for assembling to MACH-O 2 ; build with : 3 ; yasm -f macho -m amd64 machotest64.asm 4 ; gcc -m64 -o machotest64 machotest64.c machotest64.o 5 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 (note: printf replaced by another call) 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] Perform a 64 Bit relocation in the text section 25 26 [BITS 64] 27 [GLOBAL _lrotate] ; [1] 28 [GLOBAL _greet] ; [1] 29 [GLOBAL _asmstr] ; [2] 30 [GLOBAL _textptr] ; [2] 31 [GLOBAL _selfptr] ; [2] 32 [GLOBAL _integer] ; [3] 33 [EXTERN _druck] ; [10] 34 [COMMON _commvar 4] ; [7] 35 [GLOBAL _getstr] ; 36 [GLOBAL _readgreet] ; 37 38 [SECTION .text] 39 40 ; prototype: long lrotate(long x, int num); 41 _lrotate: ; [1] 42 push rcx 43 mov rax,rdi 44 mov rcx,rsi 45 .label rol rax,1 ; [4] [8] 46 loop .label ; [9] [12] 47 pop rcx 48 ret 49 50 _getstr: 51 mov rax,qword _asmstr 52 ret 53 54 _readgreet: 55 mov rax,[qword localint] ; [18] 56 ret 57 58 _retrievelabel: 59 mov rax,[qword localptr] 60 ret 61 62 ; prototype: void greet(void); 63 ; calls "void druck(a,b,c,d); 64 _greet mov rax,[_integer wrt rip] ; [14] 65 inc rax 66 mov [localint wrt rip],rax ; [14] 67 push rdi 68 push rsi 69 push rdx 70 push rcx 71 mov rdi,qword _printfstr 72 mov rsi,[_integer wrt rip] 73 mov rdx,[localptr wrt rip] 74 mov rdx,[rdx] 75 mov rcx,[_commvar wrt rip] 76 call _druck 77 pop rcx 78 pop rdx 79 pop rsi 80 pop rdi 81 ret 82 83 ; some internal calls 84 call _greet 85 call _retrievelabel 86 87 [SECTION .data] 88 89 ; a string for Printf 90 _printfstr db "integer==%d, localint==%d, commvar=%d" 91 db 10, 0 92 93 ; some pointers 94 localptr dq localint ; [5] [17] 95 _textptr dq _greet ; [15] 96 _selfptr dq _selfptr ; [16] 97 98 ;[section .data2 align=16] 99 100 ; a string 101 _asmstr db 'hello, world', 0 ; [2] 102 103 104 105 [SECTION .bss] 106 107 ; an integer 108 _integer resq 1 ; [3] 109 110 ; a local integer 111 localint resq 1 ; [6] 112