I need a program that computes a sqrt of numbers like : 1.000 , 1.321, 1.642 ... The number is increased with every iteration of finite loop. On high-level:
for(i=1, i<end, i+=0.321):
print(sqrt(i))
How to increase a variable with every iteration?
I have something like this:
bits 32
global main
extern printf
extern scanf
section .data
format_f db 'SQRT computed %f', 0
format_lf db '%lf', 0
increment: dq 0.125
ending: dq 5.500
section .bss
x resq 1
current resq 1
section .text
main:
push x
push format_lf
call scanf ;get a starting point
add esp, 8
fld qword[increment] ;st0 = increment
fld qword [x] ;st0=x ,st1=ending
looping:
fsqrt ;st0 =sqrt(st0)
sub esp, 8
fst qword [esp] ;store st0 in esp
push format_f
call printf ;print st0 = sqrt(old_st0)
fld qword [current] ;st0 = old_st0
fadd st0, st1 ;st0 = old_st0 + increment (stored in st1)
fst qword [current] ;store new st0 in current
fcom qword [ending] ;compare st0 and ending
jl looping
ending_part:
add esp, 12
sub eax, eax
ret
The code gives an error - is executable but produces infinite number of results that look like:
8SQRT computed 349106.811107SQRT computed 349107.311107SQRT computed 349107.811107SQRT computed 349108.311106SQRT computed 349108.811106SQRT computed 349109.311106SQRT computed 349109.811105SQRT computed 349110.311105SQRT computed 349110.811104SQRT computed 349111.311104SQRT computed 349111.811104SQRT computed 349112.311103SQRT computed 349112.811103SQRT computed 349113.311103SQRT computed 349113.811102SQRT computed 349114.311102SQRT computed 349114.811102SQRT computed 349115.311101SQRT computed 349115.811101SQRT computed 349116.311101SQRT computed 349116.811100SQRT computed 349117.311100SQRT computed 349117.811099SQRT computed 349118.311099SQRT computed 349118.811099SQRT computed 349119.311098SQRT computed 349119.811098SQRT computed 349120.311098SQRT computed 349120.811097SQRT
Can someone help me point out where the problem is? 2. How Can I printf to new line each time?