1 ; intarith_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 intarith_64.lst intarith_64.asm 5 ; link: gcc -m64 -o intarith_64 intarith_64.o 6 ; run: ./intarith_64 7 ; 8 ; the output from running intarith_64.asm and intarith.c is: 9 ; c=5 , a=3, b=4, c=5 10 ; c=a+b, a=3, b=4, c=7 11 ; c=a-b, a=3, b=4, c=-1 12 ; c=a*b, a=3, b=4, c=12 13 ; c=c/a, a=3, b=4, c=4 14 ; 15 ;The file intarith.c is: 16 ; /* intarith.c */ 17 ; #include 18 ; int main() 19 ; { 20 ; long int a=3, b=4, c; 21 ; c=5; 22 ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=5 ", a, b, c); 23 ; c=a+b; 24 ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=a+b", a, b, c); 25 ; c=a-b; 26 ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=a-b", a, b, c); 27 ; c=a*b; 28 ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=a*b", a, b, c); 29 ; c=c/a; 30 ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=c/a", a, b, c); 31 ; return 0; 32 ; } 33 extern printf ; the C function to be called 34 35 %macro pabc 1 ; a "simple" print macro 36 section .data 37 .str db %1,0 ; %1 is first actual in macro call 38 section .text 39 mov rdi, fmt4 ; first arg, format 40 mov rsi, .str ; second arg 41 mov rdx, [a] ; third arg 42 mov rcx, [b] ; fourth arg 43 mov r8, [c] ; fifth arg 44 mov rax, 0 ; no xmm used 45 call printf ; Call C function 46 %endmacro 47 48 section .data ; preset constants, writeable 49 00000000 0300000000000000 a: dq 3 ; 64-bit variable a initialized to 3 50 00000008 0400000000000000 b: dq 4 ; 64-bit variable b initializes to 4 51 00000010 25732C20613D256C64- fmt4: db "%s, a=%ld, b=%ld, c=%ld",10,0 ; format string for printf 52 00000019 2C20623D256C642C20- 53 00000022 633D256C640A00 54 55 section .bss ; unitialized space 56 00000000 c: resq 1 ; reserve a 64-bit word 57 58 section .text ; instructions, code segment 59 global main ; for gcc standard linking 60 main: ; label 61 00000000 55 push rbp ; set up stack 62 lit5: ; c=5; 63 00000001 B805000000 mov rax,5 ; 5 is a literal constant 64 00000006 48890425[00000000] mov [c],rax ; store into c 65 pabc "c=5 " ; invoke the print macro 66 <1> section .data 67 00000029 633D35202000 <1> .str db %1,0 68 <1> section .text 69 0000000E 48BF- <1> mov rdi, fmt4 70 00000010 [1000000000000000] <1> 71 00000018 48BE- <1> mov rsi, .str 72 0000001A [2900000000000000] <1> 73 00000022 488B1425[00000000] <1> mov rdx, [a] 74 0000002A 488B0C25[08000000] <1> mov rcx, [b] 75 00000032 4C8B0425[00000000] <1> mov r8, [c] 76 0000003A B800000000 <1> mov rax, 0 77 0000003F E8(00000000) <1> call printf 78 79 addb: ; c=a+b; 80 00000044 488B0425[00000000] mov rax,[a] ; load a 81 0000004C 48030425[08000000] add rax,[b] ; add b 82 00000054 48890425[00000000] mov [c],rax ; store into c 83 pabc "c=a+b" ; invoke the print macro 84 <1> section .data 85 0000002F 633D612B6200 <1> .str db %1,0 86 <1> section .text 87 0000005C 48BF- <1> mov rdi, fmt4 88 0000005E [1000000000000000] <1> 89 00000066 48BE- <1> mov rsi, .str 90 00000068 [2F00000000000000] <1> 91 00000070 488B1425[00000000] <1> mov rdx, [a] 92 00000078 488B0C25[08000000] <1> mov rcx, [b] 93 00000080 4C8B0425[00000000] <1> mov r8, [c] 94 00000088 B800000000 <1> mov rax, 0 95 0000008D E8(00000000) <1> call printf 96 97 subb: ; c=a-b; 98 00000092 488B0425[00000000] mov rax,[a] ; load a 99 0000009A 482B0425[08000000] sub rax,[b] ; subtract b 100 000000A2 48890425[00000000] mov [c],rax ; store into c 101 pabc "c=a-b" ; invoke the print macro 102 <1> section .data 103 00000035 633D612D6200 <1> .str db %1,0 104 <1> section .text 105 000000AA 48BF- <1> mov rdi, fmt4 106 000000AC [1000000000000000] <1> 107 000000B4 48BE- <1> mov rsi, .str 108 000000B6 [3500000000000000] <1> 109 000000BE 488B1425[00000000] <1> mov rdx, [a] 110 000000C6 488B0C25[08000000] <1> mov rcx, [b] 111 000000CE 4C8B0425[00000000] <1> mov r8, [c] 112 000000D6 B800000000 <1> mov rax, 0 113 000000DB E8(00000000) <1> call printf 114 115 mulb: ; c=a*b; 116 000000E0 488B0425[00000000] mov rax,[a] ; load a (must be rax for multiply) 117 000000E8 48F72C25[08000000] imul qword [b] ; signed integer multiply by b 118 000000F0 48890425[00000000] mov [c],rax ; store bottom half of product into c 119 pabc "c=a*b" ; invoke the print macro 120 <1> section .data 121 0000003B 633D612A6200 <1> .str db %1,0 122 <1> section .text 123 000000F8 48BF- <1> mov rdi, fmt4 124 000000FA [1000000000000000] <1> 125 00000102 48BE- <1> mov rsi, .str 126 00000104 [3B00000000000000] <1> 127 0000010C 488B1425[00000000] <1> mov rdx, [a] 128 00000114 488B0C25[08000000] <1> mov rcx, [b] 129 0000011C 4C8B0425[00000000] <1> mov r8, [c] 130 00000124 B800000000 <1> mov rax, 0 131 00000129 E8(00000000) <1> call printf 132 133 diva: ; c=c/a; 134 0000012E 488B0425[00000000] mov rax,[c] ; load c 135 00000136 BA00000000 mov rdx,0 ; load upper half of dividend with zero 136 0000013B 48F73C25[00000000] idiv qword [a] ; divide double register edx rax by a 137 00000143 48890425[00000000] mov [c],rax ; store quotient into c 138 pabc "c=c/a" ; invoke the print macro 139 <1> section .data 140 00000041 633D632F6100 <1> .str db %1,0 141 <1> section .text 142 0000014B 48BF- <1> mov rdi, fmt4 143 0000014D [1000000000000000] <1> 144 00000155 48BE- <1> mov rsi, .str 145 00000157 [4100000000000000] <1> 146 0000015F 488B1425[00000000] <1> mov rdx, [a] 147 00000167 488B0C25[08000000] <1> mov rcx, [b] 148 0000016F 4C8B0425[00000000] <1> mov r8, [c] 149 00000177 B800000000 <1> mov rax, 0 150 0000017C E8(00000000) <1> call printf 151 152 00000181 5D pop rbp ; pop stack 153 00000182 B800000000 mov rax,0 ; exit code, 0=normal 154 00000187 C3 ret ; main returns to operating system 155