1 ; fltarith_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 fltarith_64.lst fltarith_64.asm 5 ; link gcc -m64 -o fltarith_64 fltarith_64.o 6 ; run ./fltarith_64 7 ; 8 ; the output from running fltarith and fltarithc is: 9 ; c=5.0, a=3.000000e+00, b=4.000000e+00, c=5.000000e+00 10 ; c=a+b, a=3.000000e+00, b=4.000000e+00, c=7.000000e+00 11 ; c=a-b, a=3.000000e+00, b=4.000000e+00, c=-1.000000e+00 12 ; c=a*b, a=3.000000e+00, b=4.000000e+00, c=1.200000e+01 13 ; c=c/a, a=3.000000e+00, b=4.000000e+00, c=4.000000e+00 14 ; a=i , a=8.000000e+00, b=1.600000e+01, c=1.600000e+01 15 ; a<=b , a=8.000000e+00, b=1.600000e+01, c=1.600000e+01 16 ; b==c , a=8.000000e+00, b=1.600000e+01, c=1.600000e+01 17 ;The file fltarith.c is: 18 ; #include 19 ; int main() 20 ; { 21 ; double a=3.0, b=4.0, c; 22 ; long int i=8; 23 ; 24 ; c=5.0; 25 ; printf("%s, a=%e, b=%e, c=%e\n","c=5.0", a, b, c); 26 ; c=a+b; 27 ; printf("%s, a=%e, b=%e, c=%e\n","c=a+b", a, b, c); 28 ; c=a-b; 29 ; printf("%s, a=%e, b=%e, c=%e\n","c=a-b", a, b, c); 30 ; c=a*b; 31 ; printf("%s, a=%e, b=%e, c=%e\n","c=a*b", a, b, c); 32 ; c=c/a; 33 ; printf("%s, a=%e, b=%e, c=%e\n","c=c/a", a, b, c); 34 ; a=i; 35 ; b=a+i; 36 ; i=b; 37 ; c=i; 38 ; printf("%s, a=%e, b=%e, c=%e\n","c=c/a", a, b, c); 39 ; if(ab ", a, b, c); 41 ; if(b==c)printf("%s, a=%e, b=%e, c=%e\n","b==c ", a, b, c); 42 ; else printf("%s, a=%e, b=%e, c=%e\n","b!=c ", a, b, c); 43 ; return 0; 44 ; } 45 46 extern printf ; the C function to be called 47 48 %macro pabc 1 ; a "simple" print macro 49 section .data 50 .str db %1,0 ; %1 is macro call first actual parameter 51 section .text 52 ; push onto stack backwards 53 mov rdi, fmt ; address of format string 54 mov rsi, .str ; string passed to macro 55 movq xmm0, qword [a] ; first floating point in fmt 56 movq xmm1, qword [b] ; second floating point 57 movq xmm2, qword [c] ; third floating point 58 mov rax, 3 ; 3 floating point arguments to printf 59 call printf ; Call C function 60 %endmacro 61 62 section .data ; preset constants, writeable 63 00000000 0000000000000840 a: dq 3.0 ; 64-bit variable a initialized to 3.0 64 00000008 0000000000001040 b: dq 4.0 ; 64-bit variable b initializes to 4.0 65 00000010 0800000000000000 i: dq 8 ; a 64 bit integer 66 00000018 0000000000001440 five: dq 5.0 ; constant 5.0 67 00000020 25732C20613D25652C- fmt: db "%s, a=%e, b=%e, c=%e",10,0 ; format string for printf 68 00000029 20623D25652C20633D- 69 00000032 25650A00 70 71 section .bss ; unitialized space 72 00000000 c: resq 1 ; reserve a 64-bit word 73 74 section .text ; instructions, code segment 75 global main ; for gcc standard linking 76 main: ; label 77 78 00000000 55 push rbp ; set up stack 79 lit5: ; c=5.0; 80 00000001 DD0425[18000000] fld qword [five] ; 5.0 constant 81 00000008 DD1C25[00000000] fstp qword [c] ; store into c 82 pabc "c=5.0" ; invoke the print macro 83 <1> section .data 84 00000036 633D352E3000 <1> .str db %1,0 85 <1> section .text 86 <1> 87 0000000F 48BF- <1> mov rdi, fmt 88 00000011 [2000000000000000] <1> 89 00000019 48BE- <1> mov rsi, .str 90 0000001B [3600000000000000] <1> 91 00000023 F30F7E0425- <1> movq xmm0, qword [a] 92 00000028 [00000000] <1> 93 0000002C F30F7E0C25- <1> movq xmm1, qword [b] 94 00000031 [08000000] <1> 95 00000035 F30F7E1425- <1> movq xmm2, qword [c] 96 0000003A [00000000] <1> 97 0000003E B803000000 <1> mov rax, 3 98 00000043 E8(00000000) <1> call printf 99 100 addb: ; c=a+b; 101 00000048 DD0425[00000000] fld qword [a] ; load a (pushed on flt pt stack, st0) 102 0000004F DC0425[08000000] fadd qword [b] ; floating add b (to st0) 103 00000056 DD1C25[00000000] fstp qword [c] ; store into c (pop flt pt stack) 104 pabc "c=a+b" ; invoke the print macro 105 <1> section .data 106 0000003C 633D612B6200 <1> .str db %1,0 107 <1> section .text 108 <1> 109 0000005D 48BF- <1> mov rdi, fmt 110 0000005F [2000000000000000] <1> 111 00000067 48BE- <1> mov rsi, .str 112 00000069 [3C00000000000000] <1> 113 00000071 F30F7E0425- <1> movq xmm0, qword [a] 114 00000076 [00000000] <1> 115 0000007A F30F7E0C25- <1> movq xmm1, qword [b] 116 0000007F [08000000] <1> 117 00000083 F30F7E1425- <1> movq xmm2, qword [c] 118 00000088 [00000000] <1> 119 0000008C B803000000 <1> mov rax, 3 120 00000091 E8(00000000) <1> call printf 121 122 subb: ; c=a-b; 123 00000096 DD0425[00000000] fld qword [a] ; load a (pushed on flt pt stack, st0) 124 0000009D DC2425[08000000] fsub qword [b] ; floating subtract b (to st0) 125 000000A4 DD1C25[00000000] fstp qword [c] ; store into c (pop flt pt stack) 126 pabc "c=a-b" ; invoke the print macro 127 <1> section .data 128 00000042 633D612D6200 <1> .str db %1,0 129 <1> section .text 130 <1> 131 000000AB 48BF- <1> mov rdi, fmt 132 000000AD [2000000000000000] <1> 133 000000B5 48BE- <1> mov rsi, .str 134 000000B7 [4200000000000000] <1> 135 000000BF F30F7E0425- <1> movq xmm0, qword [a] 136 000000C4 [00000000] <1> 137 000000C8 F30F7E0C25- <1> movq xmm1, qword [b] 138 000000CD [08000000] <1> 139 000000D1 F30F7E1425- <1> movq xmm2, qword [c] 140 000000D6 [00000000] <1> 141 000000DA B803000000 <1> mov rax, 3 142 000000DF E8(00000000) <1> call printf 143 144 mulb: ; c=a*b; 145 000000E4 DD0425[00000000] fld qword [a] ; load a (pushed on flt pt stack, st0) 146 000000EB DC0C25[08000000] fmul qword [b] ; floating multiply by b (to st0) 147 000000F2 DD1C25[00000000] fstp qword [c] ; store product into c (pop flt pt stack) 148 pabc "c=a*b" ; invoke the print macro 149 <1> section .data 150 00000048 633D612A6200 <1> .str db %1,0 151 <1> section .text 152 <1> 153 000000F9 48BF- <1> mov rdi, fmt 154 000000FB [2000000000000000] <1> 155 00000103 48BE- <1> mov rsi, .str 156 00000105 [4800000000000000] <1> 157 0000010D F30F7E0425- <1> movq xmm0, qword [a] 158 00000112 [00000000] <1> 159 00000116 F30F7E0C25- <1> movq xmm1, qword [b] 160 0000011B [08000000] <1> 161 0000011F F30F7E1425- <1> movq xmm2, qword [c] 162 00000124 [00000000] <1> 163 00000128 B803000000 <1> mov rax, 3 164 0000012D E8(00000000) <1> call printf 165 166 diva: ; c=c/a; 167 00000132 DD0425[00000000] fld qword [c] ; load c (pushed on flt pt stack, st0) 168 00000139 DC3425[00000000] fdiv qword [a] ; floating divide by a (to st0) 169 00000140 DD1C25[00000000] fstp qword [c] ; store quotient into c (pop flt pt stack) 170 pabc "c=c/a" ; invoke the print macro 171 <1> section .data 172 0000004E 633D632F6100 <1> .str db %1,0 173 <1> section .text 174 <1> 175 00000147 48BF- <1> mov rdi, fmt 176 00000149 [2000000000000000] <1> 177 00000151 48BE- <1> mov rsi, .str 178 00000153 [4E00000000000000] <1> 179 0000015B F30F7E0425- <1> movq xmm0, qword [a] 180 00000160 [00000000] <1> 181 00000164 F30F7E0C25- <1> movq xmm1, qword [b] 182 00000169 [08000000] <1> 183 0000016D F30F7E1425- <1> movq xmm2, qword [c] 184 00000172 [00000000] <1> 185 00000176 B803000000 <1> mov rax, 3 186 0000017B E8(00000000) <1> call printf 187 188 intflt: ; a=i; 189 00000180 DB0425[10000000] fild dword [i] ; load integer as floating point 190 00000187 DD1425[00000000] fst qword [a] ; store the floating point (no pop) 191 0000018E D8C0 fadd st0 ; b=a+i; 'a' as 'i' already on flt stack 192 00000190 DD1425[08000000] fst qword [b] ; store sum (no pop) 'b' still on stack 193 00000197 DB1C25[10000000] fistp dword [i] ; i=b; store floating point as integer 194 0000019E DB0425[10000000] fild dword [i] ; c=i; load again from ram (redundant) 195 000001A5 DD1C25[00000000] fstp qword [c] 196 pabc "a=i " ; invoke the print macro 197 <1> section .data 198 00000054 613D69202000 <1> .str db %1,0 199 <1> section .text 200 <1> 201 000001AC 48BF- <1> mov rdi, fmt 202 000001AE [2000000000000000] <1> 203 000001B6 48BE- <1> mov rsi, .str 204 000001B8 [5400000000000000] <1> 205 000001C0 F30F7E0425- <1> movq xmm0, qword [a] 206 000001C5 [00000000] <1> 207 000001C9 F30F7E0C25- <1> movq xmm1, qword [b] 208 000001CE [08000000] <1> 209 000001D2 F30F7E1425- <1> movq xmm2, qword [c] 210 000001D7 [00000000] <1> 211 000001DB B803000000 <1> mov rax, 3 212 000001E0 E8(00000000) <1> call printf 213 214 000001E5 D90425[08000000] cmpflt: fld dword [b] ; into st0, then pushed to st1 215 000001EC D90425[00000000] fld dword [a] ; in st0 216 000001F3 DFF1 fcomip st0,st1 ; a compare b, pop a 217 000001F5 7F3B jg cmpfl2 218 pabc "a<=b " 219 <1> section .data 220 0000005A 613C3D622000 <1> .str db %1,0 221 <1> section .text 222 <1> 223 000001F7 48BF- <1> mov rdi, fmt 224 000001F9 [2000000000000000] <1> 225 00000201 48BE- <1> mov rsi, .str 226 00000203 [5A00000000000000] <1> 227 0000020B F30F7E0425- <1> movq xmm0, qword [a] 228 00000210 [00000000] <1> 229 00000214 F30F7E0C25- <1> movq xmm1, qword [b] 230 00000219 [08000000] <1> 231 0000021D F30F7E1425- <1> movq xmm2, qword [c] 232 00000222 [00000000] <1> 233 00000226 B803000000 <1> mov rax, 3 234 0000022B E8(00000000) <1> call printf 235 00000230 EB39 jmp cmpfl3 236 cmpfl2: 237 pabc "a>b " 238 <1> section .data 239 00000060 613E62202000 <1> .str db %1,0 240 <1> section .text 241 <1> 242 00000232 48BF- <1> mov rdi, fmt 243 00000234 [2000000000000000] <1> 244 0000023C 48BE- <1> mov rsi, .str 245 0000023E [6000000000000000] <1> 246 00000246 F30F7E0425- <1> movq xmm0, qword [a] 247 0000024B [00000000] <1> 248 0000024F F30F7E0C25- <1> movq xmm1, qword [b] 249 00000254 [08000000] <1> 250 00000258 F30F7E1425- <1> movq xmm2, qword [c] 251 0000025D [00000000] <1> 252 00000261 B803000000 <1> mov rax, 3 253 00000266 E8(00000000) <1> call printf 254 cmpfl3: 255 0000026B D90425[00000000] fld dword [c] ; should equal [b] 256 00000272 DFF1 fcomip st0,st1 257 00000274 753B jne cmpfl4 258 pabc "b==c " 259 <1> section .data 260 00000066 623D3D632000 <1> .str db %1,0 261 <1> section .text 262 <1> 263 00000276 48BF- <1> mov rdi, fmt 264 00000278 [2000000000000000] <1> 265 00000280 48BE- <1> mov rsi, .str 266 00000282 [6600000000000000] <1> 267 0000028A F30F7E0425- <1> movq xmm0, qword [a] 268 0000028F [00000000] <1> 269 00000293 F30F7E0C25- <1> movq xmm1, qword [b] 270 00000298 [08000000] <1> 271 0000029C F30F7E1425- <1> movq xmm2, qword [c] 272 000002A1 [00000000] <1> 273 000002A5 B803000000 <1> mov rax, 3 274 000002AA E8(00000000) <1> call printf 275 000002AF EB39 jmp cmpfl5 276 cmpfl4: 277 pabc "b!=c " 278 <1> section .data 279 0000006C 62213D632000 <1> .str db %1,0 280 <1> section .text 281 <1> 282 000002B1 48BF- <1> mov rdi, fmt 283 000002B3 [2000000000000000] <1> 284 000002BB 48BE- <1> mov rsi, .str 285 000002BD [6C00000000000000] <1> 286 000002C5 F30F7E0425- <1> movq xmm0, qword [a] 287 000002CA [00000000] <1> 288 000002CE F30F7E0C25- <1> movq xmm1, qword [b] 289 000002D3 [08000000] <1> 290 000002D7 F30F7E1425- <1> movq xmm2, qword [c] 291 000002DC [00000000] <1> 292 000002E0 B803000000 <1> mov rax, 3 293 000002E5 E8(00000000) <1> call printf 294 cmpfl5: 295 296 000002EA 5D pop rbp ; pop stack 297 000002EB B800000000 mov rax,0 ; exit code, 0=normal 298 000002F0 C3 ret ; main returns to operating system