1 ; fltarith.asm show some simple C code and corresponding nasm code 2 ; the nasm code is one sample, not unique 3 ; 4 ; compile nasm -f elf -l fltarith.lst fltarith.asm 5 ; link gcc -o fltarith fltarith.o 6 ; run fltarith 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 ; 15 ;The file fltarith.c is: 16 ; #include 17 ; int main() 18 ; { 19 ; double a=3.0, b=4.0, c; 20 ; 21 ; c=5.0; 22 ; printf("%s, a=%e, b=%e, c=%e\n","c=5.0", a, b, c); 23 ; c=a+b; 24 ; printf("%s, a=%e, b=%e, c=%e\n","c=a+b", a, b, c); 25 ; c=a-b; 26 ; printf("%s, a=%e, b=%e, c=%e\n","c=a-b", a, b, c); 27 ; c=a*b; 28 ; printf("%s, a=%e, b=%e, c=%e\n","c=a*b", a, b, c); 29 ; c=c/a; 30 ; printf("%s, a=%e, b=%e, c=%e\n","c=c/a", a, b, c); 31 ; return 0; 32 ; } 33 34 extern printf ; the C function to be called 35 36 %macro pabc 1 ; a "simple" print macro 37 section .data 38 .str db %1,0 ; %1 is macro call first actual parameter 39 section .text 40 ; push onto stack backwards 41 push dword [c+4] ; double c (bottom) 42 push dword [c] ; double c 43 push dword [b+4] ; double b (bottom) 44 push dword [b] ; double b 45 push dword [a+4] ; double a (bottom) 46 push dword [a] ; double a 47 push dword .str ; users string 48 push dword fmt ; address of format string 49 call printf ; Call C function 50 add esp,32 ; pop stack 8*4 bytes 51 %endmacro 52 53 section .data ; preset constants, writeable 54 00000000 A3369FAAAAAA0A40 a: dq 3.333333333 ; 64-bit variable a initialized to 3.0 55 00000008 C279BF711CC71140 b: dq 4.444444444 ; 64-bit variable b initializes to 4.0 56 00000010 0000000000001440 five: dq 5.0 ; constant 5.0 57 00000018 25732C20613D25652C- fmt: db "%s, a=%e, b=%e, c=%e",10,0 ; format string for printf 58 00000021 20623D25652C20633D- 59 0000002A 25650A00 60 61 section .bss ; unitialized space 62 00000000 c: resq 1 ; reserve a 64-bit word 63 64 section .text ; instructions, code segment 65 global main ; for gcc standard linking 66 main: ; label 67 68 lit5: ; c=5.0; 69 00000000 DD05[10000000] fld qword [five] ; 5.0 constant 70 00000006 DD1D[00000000] fstp qword [c] ; store into c 71 pabc "c=5.0" ; invoke the print macro 72 <1> section .data 73 0000002E 633D352E3000 <1> .str db %1,0 74 <1> section .text 75 <1> 76 0000000C FF35[04000000] <1> push dword [c+4] 77 00000012 FF35[00000000] <1> push dword [c] 78 00000018 FF35[0C000000] <1> push dword [b+4] 79 0000001E FF35[08000000] <1> push dword [b] 80 00000024 FF35[04000000] <1> push dword [a+4] 81 0000002A FF35[00000000] <1> push dword [a] 82 00000030 68[2E000000] <1> push dword .str 83 00000035 68[18000000] <1> push dword fmt 84 0000003A E8(00000000) <1> call printf 85 0000003F 83C420 <1> add esp,32 86 87 addb: ; c=a+b; 88 00000042 DD05[00000000] fld qword [a] ; load a (pushed on flt pt stack, st0) 89 00000048 DC05[08000000] fadd qword [b] ; floating add b (to st0) 90 0000004E DD1D[00000000] fstp qword [c] ; store into c (pop flt pt stack) 91 pabc "c=a+b" ; invoke the print macro 92 <1> section .data 93 00000034 633D612B6200 <1> .str db %1,0 94 <1> section .text 95 <1> 96 00000054 FF35[04000000] <1> push dword [c+4] 97 0000005A FF35[00000000] <1> push dword [c] 98 00000060 FF35[0C000000] <1> push dword [b+4] 99 00000066 FF35[08000000] <1> push dword [b] 100 0000006C FF35[04000000] <1> push dword [a+4] 101 00000072 FF35[00000000] <1> push dword [a] 102 00000078 68[34000000] <1> push dword .str 103 0000007D 68[18000000] <1> push dword fmt 104 00000082 E8(00000000) <1> call printf 105 00000087 83C420 <1> add esp,32 106 107 subb: ; c=a-b; 108 0000008A DD05[00000000] fld qword [a] ; load a (pushed on flt pt stack, st0) 109 00000090 DC25[08000000] fsub qword [b] ; floating subtract b (to st0) 110 00000096 DD1D[00000000] fstp qword [c] ; store into c (pop flt pt stack) 111 pabc "c=a-b" ; invoke the print macro 112 <1> section .data 113 0000003A 633D612D6200 <1> .str db %1,0 114 <1> section .text 115 <1> 116 0000009C FF35[04000000] <1> push dword [c+4] 117 000000A2 FF35[00000000] <1> push dword [c] 118 000000A8 FF35[0C000000] <1> push dword [b+4] 119 000000AE FF35[08000000] <1> push dword [b] 120 000000B4 FF35[04000000] <1> push dword [a+4] 121 000000BA FF35[00000000] <1> push dword [a] 122 000000C0 68[3A000000] <1> push dword .str 123 000000C5 68[18000000] <1> push dword fmt 124 000000CA E8(00000000) <1> call printf 125 000000CF 83C420 <1> add esp,32 126 127 mulb: ; c=a*b; 128 000000D2 DD05[00000000] fld qword [a] ; load a (pushed on flt pt stack, st0) 129 000000D8 DC0D[08000000] fmul qword [b] ; floating multiply by b (to st0) 130 000000DE DD1D[00000000] fstp qword [c] ; store product into c (pop flt pt stack) 131 pabc "c=a*b" ; invoke the print macro 132 <1> section .data 133 00000040 633D612A6200 <1> .str db %1,0 134 <1> section .text 135 <1> 136 000000E4 FF35[04000000] <1> push dword [c+4] 137 000000EA FF35[00000000] <1> push dword [c] 138 000000F0 FF35[0C000000] <1> push dword [b+4] 139 000000F6 FF35[08000000] <1> push dword [b] 140 000000FC FF35[04000000] <1> push dword [a+4] 141 00000102 FF35[00000000] <1> push dword [a] 142 00000108 68[40000000] <1> push dword .str 143 0000010D 68[18000000] <1> push dword fmt 144 00000112 E8(00000000) <1> call printf 145 00000117 83C420 <1> add esp,32 146 147 diva: ; c=c/a; 148 0000011A DD05[00000000] fld qword [c] ; load c (pushed on flt pt stack, st0) 149 00000120 DC35[00000000] fdiv qword [a] ; floating divide by a (to st0) 150 00000126 DD1D[00000000] fstp qword [c] ; store quotient into c (pop flt pt stack) 151 pabc "c=c/a" ; invoke the print macro 152 <1> section .data 153 00000046 633D632F6100 <1> .str db %1,0 154 <1> section .text 155 <1> 156 0000012C FF35[04000000] <1> push dword [c+4] 157 00000132 FF35[00000000] <1> push dword [c] 158 00000138 FF35[0C000000] <1> push dword [b+4] 159 0000013E FF35[08000000] <1> push dword [b] 160 00000144 FF35[04000000] <1> push dword [a+4] 161 0000014A FF35[00000000] <1> push dword [a] 162 00000150 68[46000000] <1> push dword .str 163 00000155 68[18000000] <1> push dword fmt 164 0000015A E8(00000000) <1> call printf 165 0000015F 83C420 <1> add esp,32 166 167 00000162 B800000000 mov eax,0 ; exit code, 0=normal 168 00000167 C3 ret ; main returns to operating system 169 170