0

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
  1. 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?

  • 2
    Do you know how to do it in C? Do that, stick it in https://godbolt.org/, and see what the C compiler does. – Joseph Sible-Reinstate Monica May 30 '20 at 00:46
  • Do you insist on using legacy x87 FP math? SSE2 is actually easier because it uses a normal set of registers instead of a register-stack. So look at 64-bit GCC output, or compile with `-m32 -mfpmath=sse`. Also, you're going to want to call `printf` from libc for printing a `double`; manual FP->string conversion is non-trivial even if you know how to implement single operations like addition and division. – Peter Cordes May 30 '20 at 00:56
  • @PeterCordes Maybe lets focus on how to increment variable with each iteration (and forget for now about the other problems like printf and running it with GCC - I'm a beginner also in that). Could you give me a tip on how to do that (I don't know if I should stay with x87 or move to SSE2 - I just want it to be as simple as possible) – andrew_the_coder May 30 '20 at 10:19
  • 1
    Sure, `addsd xmm0, [some_value]`, where you define `some_value` appropriately, like `some_value: dq 0.321`. (not `dd` because you're doing a qword load, not dword). Joseph and I both told you the easiest way to see that as part of a whole loop: write it in C and compile that C with `gcc -m32 -mfpmath=sse -Og`, especially on https://godbolt.org/. I'm not interested in writing a custom tutorial on the super basics for every beginner problem; tutorials exist, go read them, and look at compiler output to find out how to do simple things. Or maybe someone else will eventually answer this. – Peter Cordes May 30 '20 at 10:28
  • If you want to find the answer yourself, https://stackoverflow.com/tags/x86/info has links to tutorials, and [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) has info on getting compilers to make useful code to look at. – Peter Cordes May 30 '20 at 10:31
  • @PeterCordes once again thanks for help, I have a simple problem left (I edited the code). Could you give me some advice on how to do that ? – andrew_the_coder May 30 '20 at 15:25
  • `fcom` compares into FP status registers. You want `fcomi` to update EFLAGS direclty, then `jb` not `jl`. – Peter Cordes May 30 '20 at 19:39

0 Answers0