1

I have a program where I have to pass arguments to my functions through registers, stack, and public variables, and it seems that I lose data between them, because even after the first function I get 0.0 in "save" variable I think that I messed up something about adressing variables, because I am very new to MASM32 syntax, and it has some weird limitations. Though maybe I'm just trying to print it wrong

Main file

extern funcNum1:proto
extern funcNum2:proto
extern funcNum3:proto
public giveVal1, giveVal2

    .data?
      save dd ?
      giveVal1 dd ?
      giveVal2 dd ?

    .data
      array dq 12.0, 0.87, 9.7, 8.64

.code

start:
    call main
    inkey
    exit
main proc

    cls
    print "List number 27, variant 6",10

    lea ecx, array
    lea edx, array+24
    lea eax, save
    call funcNum1    


    push eax
    lea ecx, array+16
    push ecx
    call funcNum2
    pop eax ; save

    printf("Result is %f\n", save) 

    mov esi, dword ptr array
    mov dword ptr giveVal2, esi
    mov esi, dword ptr array+8
    mov dword ptr giveVal1, esi
    call funcNum3
    fld giveVal1
    fld qword ptr [edi]
    fxch
    fdiv
    fstp save

    printf("Result is %f\n", save)    

    ret

main endp

end start

File with functions

extrn giveVal1:DWORD, giveVal2:DWORD
public funcNum1, funcNum2, funcNum3

.data?
    valuef dq ?
    reg1 dq ?
    reg2 dq ?

.data      
    value1 dq 53
    value2 dq -2
    value3 dq 4
    

.code       ; code section

funcNum1 proc
    fld qword ptr [ecx]
    fld qword ptr [edx]
    fdiv
    fsin
    fstp qword ptr [eax]
    ret
funcNum1 endp


funcNum2 proc
    pop edx
    pop eax ; array[2]
    pop ecx ; save
    fild value1
    fld qword ptr [ecx]
    fsub
    fstp valuef
    finit
    fild value2
    fld qword ptr [eax]
    fmul
    fld valuef
    fadd
    fstp qword ptr [ecx]
    finit
    push ecx
    push edx
    ret
funcNum2 endp

funcNum3 proc
    fld qword ptr [giveVal1]
    fild qword ptr [value3]
    fdiv
    fld qword ptr [giveVal2]
    fsub
    fstp giveVal1
    ret
funcNum3 endp

end

Mirinum
  • 33
  • 5
  • 1
    `save dd ?` reserves space for one dword, but you're using qword (`double`) x87 instructions. Also, balance your `fld` operations with `fstp` (or pushes with pops in general) so you don't overflow the x87 stack, without having to randomly do `finit` in the middle of your code. If you'd rather just have flat registers instead of a stack, use SSE2 instructions like `movsd xmm0, [eax]` / `divsd xmm0, [edx]`. – Peter Cordes Jun 09 '21 at 08:45
  • @PeterCordes thank you, it fixed the output after the first two functions, but I still get some weird results in the end. No matter what values I set, I always get 1.#INF00. I think that the problem is within the third function, where I calculate the denominator, so I'm gonna look into that – Mirinum Jun 09 '21 at 08:52
  • The low dword of a `double` is all zeros, if it's a nice round number. Or if you put a dword `float` in the bottom of a qword `double` bit-pattern, it represents a very tiny value. So dividing by it could easily give +INF. Make sure you have the right operand-size for everything, including `mov dword ptr giveVal2, esi` which is only copying 4 bytes, so that won't work if you widen your `giveVal` vars to qword. Anyway, **single-step your code with a debugger and see where you get an Infinity.** – Peter Cordes Jun 09 '21 at 09:00
  • @PeterCordes I will, thank you for the advice. It is probably because of the wrong operand-sizes – Mirinum Jun 09 '21 at 09:08

0 Answers0