Im trying to make a BMI calculator MASMx64 just as a basic ASM project and i cant get printf or pow to work

46 Views Asked by At

as it says in the title im having issues with calling pow and printf doing having a Access violation reading location. Im also using c17. heres what the code looks like

includelib legacy_stdio_definitions.lib

extern printf_s:proc
extern scanf_s:proc
extern pow:proc

.data
    height real8 1.73
    mass real8 70.0
    square real8 2.0
    scanfGetDouble db "%lf", 0
    printfPrintDouble db "%lf", 0
    
.code
main proc
    sub rsp, 100h
    lea rcx, qword ptr scanfGetDouble
    lea rdx, qword ptr height
    call scanf_s
    
    lea rcx, qword ptr printfPrintDouble
    movsd xmm1, real8 ptr height
    movq rdx, xmm1
    call printf_s

    lea rcx, qword ptr printfPrintDouble
    movsd xmm1, real8 ptr mass
    movq rdx, xmm1
    call printf_s

    movsd xmm0, mmword ptr height
    movsd xmm1, mmword ptr square
    call pow
    
    movsd xmm1, mmword ptr mass
    divsd xmm0, xmm1
    movq rdx, xmm0
    lea rcx, qword ptr printfPrintDouble
    call printf_s
    xor rax, rax
    add rsp, 100h
    ret
main endp
end

the way i have been testing pow is commenting the other code out and changing sub/add rsp, 100h to 20h

overall at the end using the values already given the BMI should be arround 23.4

1

There are 1 best solutions below

3
PMF On

Calling C functions from assembly is tricky. While the exact sequence depends on the compiler, I'm quite sure those methods don't take arguments via registers, but use the stack. Also, you're apparently assuming that pow is operating on xmm (MMX) registers, which is very likely wrong. It takes arguments on the stack and uses the "normal" X87 FPU instead. If you want an MMX pow operation, directly use its assembly instruction.

Overall, this is not a good start for learning assembly. First try to avoid using C functions, as this is really tricky (you need to get the calling convention right) and also try to avoid using floating point operations until you are familiar with the standard ALU (integer) operations. You can try to use fixed-point arithmetic instead of floating point numbers.