1 ; intlogic_64.asm show some simple C code and corresponding nasm code 2 ; the nasm code is one sample, not unique 3 ; 4 ; compile: nasm -f elf64 -l intlogic_64.lst intlogic_64.asm 5 ; link: gcc -m64 -o intlogic_64 intlogic_64.o 6 ; run: ./intlogic_64 > intlogic_64.out 7 ; 8 ; the output from running intlogic_64.asm and intlogic.c is 9 ; c=5 , a=3, b=5, c=15 10 ; c=a&b, a=3, b=5, c=1 11 ; c=a|b, a=3, b=5, c=7 12 ; c=a^b, a=3, b=5, c=6 13 ; c=~a , a=3, b=5, c=-4 14 ; 15 ;The file intlogic.c is: 16 ; /* intlogic.c */ 17 ; #include 18 ; int main() 19 ; { 20 ; long int a=3, b=5, c; 21 ; 22 ; c=15; 23 ; printf("%s, a=%d, b=%d, c=%d\n","c=5 ", a, b, c); 24 ; c=a&b; /* and */ 25 ; printf("%s, a=%d, b=%d, c=%d\n","c=a&b", a, b, c); 26 ; c=a|b; /* or */ 27 ; printf("%s, a=%d, b=%d, c=%d\n","c=a|b", a, b, c); 28 ; c=a^b; /* xor */ 29 ; printf("%s, a=%d, b=%d, c=%d\n","c=a^b", a, b, c); 30 ; c=~a; /* not */ 31 ; printf("%s, a=%d, b=%d, c=%d\n","c=~a", a, b, c); 32 ; return 0; 33 ; } 34 35 extern printf ; the C function to be called 36 37 %macro pabc 1 ; a "simple" print macro 38 section .data 39 .str db %1,0 ; %1 is first actual in macro call 40 section .text 41 mov rdi, fmt ; address of format string 42 mov rsi, .str ; users string 43 mov rdx, [a] ; long int a 44 mov rcx, [b] ; long int b 45 mov r8, [c] ; long int c 46 mov rax, 0 ; no xmm used 47 call printf ; Call C function 48 %endmacro 49 50 section .data ; preset constants, writeable 51 00000000 0300000000000000 a: dq 3 ; 64-bit variable a initialized to 3 52 00000008 0500000000000000 b: dq 5 ; 64-bit variable b initializes to 4 53 00000010 25732C20613D256C64- fmt: db "%s, a=%ld, b=%ld, c=%ld",10,0 ; format string for printf 54 00000019 2C20623D256C642C20- 55 00000022 633D256C640A00 56 57 section .bss ; unitialized space 58 00000000 c: resq 1 ; reserve a 64-bit word 59 60 section .text ; instructions, code segment 61 global main ; for gcc standard linking 62 main: ; label 63 00000000 55 push rbp ; set up stack 64 65 lit5: ; c=5; 66 00000001 B80F000000 mov rax,15 ; 5 is a literal constant 67 00000006 48890425[00000000] mov [c],rax ; store into c 68 pabc "c=5 " ; invoke the print macro 69 <1> section .data 70 00000029 633D35202000 <1> .str db %1,0 71 <1> section .text 72 0000000E 48BF- <1> mov rdi, fmt 73 00000010 [1000000000000000] <1> 74 00000018 48BE- <1> mov rsi, .str 75 0000001A [2900000000000000] <1> 76 00000022 488B1425[00000000] <1> mov rdx, [a] 77 0000002A 488B0C25[08000000] <1> mov rcx, [b] 78 00000032 4C8B0425[00000000] <1> mov r8, [c] 79 0000003A B800000000 <1> mov rax, 0 80 0000003F E8(00000000) <1> call printf 81 82 andb: ; c=a&b; 83 00000044 488B0425[00000000] mov rax,[a] ; load a 84 0000004C 48230425[08000000] and rax,[b] ; and with b 85 00000054 48890425[00000000] mov [c],rax ; store into c 86 pabc "c=a&b" ; invoke the print macro 87 <1> section .data 88 0000002F 633D61266200 <1> .str db %1,0 89 <1> section .text 90 0000005C 48BF- <1> mov rdi, fmt 91 0000005E [1000000000000000] <1> 92 00000066 48BE- <1> mov rsi, .str 93 00000068 [2F00000000000000] <1> 94 00000070 488B1425[00000000] <1> mov rdx, [a] 95 00000078 488B0C25[08000000] <1> mov rcx, [b] 96 00000080 4C8B0425[00000000] <1> mov r8, [c] 97 00000088 B800000000 <1> mov rax, 0 98 0000008D E8(00000000) <1> call printf 99 100 orw: ; c=a-b; 101 00000092 488B0425[00000000] mov rax,[a] ; load a 102 0000009A 480B0425[08000000] or rax,[b] ; logical or with b 103 000000A2 48890425[00000000] mov [c],rax ; store into c 104 pabc "c=a|b" ; invoke the print macro 105 <1> section .data 106 00000035 633D617C6200 <1> .str db %1,0 107 <1> section .text 108 000000AA 48BF- <1> mov rdi, fmt 109 000000AC [1000000000000000] <1> 110 000000B4 48BE- <1> mov rsi, .str 111 000000B6 [3500000000000000] <1> 112 000000BE 488B1425[00000000] <1> mov rdx, [a] 113 000000C6 488B0C25[08000000] <1> mov rcx, [b] 114 000000CE 4C8B0425[00000000] <1> mov r8, [c] 115 000000D6 B800000000 <1> mov rax, 0 116 000000DB E8(00000000) <1> call printf 117 118 xorw: ; c=a^b; 119 000000E0 488B0425[00000000] mov rax,[a] ; load a 120 000000E8 48330425[08000000] xor rax,[b] ; exclusive or with b 121 000000F0 48890425[00000000] mov [c],rax ; store result in c 122 pabc "c=a^b" ; invoke the print macro 123 <1> section .data 124 0000003B 633D615E6200 <1> .str db %1,0 125 <1> section .text 126 000000F8 48BF- <1> mov rdi, fmt 127 000000FA [1000000000000000] <1> 128 00000102 48BE- <1> mov rsi, .str 129 00000104 [3B00000000000000] <1> 130 0000010C 488B1425[00000000] <1> mov rdx, [a] 131 00000114 488B0C25[08000000] <1> mov rcx, [b] 132 0000011C 4C8B0425[00000000] <1> mov r8, [c] 133 00000124 B800000000 <1> mov rax, 0 134 00000129 E8(00000000) <1> call printf 135 136 notw: ; c=~a; 137 0000012E 488B0425[00000000] mov rax,[a] ; load c 138 00000136 48F7D0 not rax ; not, complement 139 00000139 48890425[00000000] mov [c],rax ; store result into c 140 pabc "c=~a " ; invoke the print macro 141 <1> section .data 142 00000041 633D7E612000 <1> .str db %1,0 143 <1> section .text 144 00000141 48BF- <1> mov rdi, fmt 145 00000143 [1000000000000000] <1> 146 0000014B 48BE- <1> mov rsi, .str 147 0000014D [4100000000000000] <1> 148 00000155 488B1425[00000000] <1> mov rdx, [a] 149 0000015D 488B0C25[08000000] <1> mov rcx, [b] 150 00000165 4C8B0425[00000000] <1> mov r8, [c] 151 0000016D B800000000 <1> mov rax, 0 152 00000172 E8(00000000) <1> call printf 153 154 00000177 5D pop rbp ; restore stack 155 00000178 B800000000 mov rax,0 ; exit code, 0=normal 156 0000017D C3 ret ; main returns to operating system