assemblyx86irvine32

Caling procedure in conversion.asm from main .asm


I have two asm files, one is conversion.asm and one is main.asm, I am using conversion.asm in main.asm. I am using floating point stack but I am not getting the right output.

main.asm

Include conversion.asm

.386
.stack 4096

ExitProcess PROTO, dwExitCode:DWORD

.data
Cel DD 25
Faren DD ?

.code
main PROC
  push dword ptr Cel
  fld dword ptr [esp]

  call C2F

  fstp dword ptr [Faren]
  mov ebx, [Faren]
  INVOKE ExitProcess, ebx
main ENDP
END main

conversion.asm


.model flat, stdcall

ExitProcess PROTO, dwExitCode:DWORD
.stack 4096

.data 
Cfirst DD 2
Csecond DD 1
common DD 32

C2F PROC
    push dword ptr Cfirst
    fld dword ptr [esp]
    add esp,4
    fmulp
    sub esp,4
    push dword ptr Csecond
    fld dword ptr [esp]
    add esp,4
    fdivp
    sub esp,4
    push dword ptr common
    fld dword ptr [esp]
    add esp,4
    faddp
    sub esp,4
    RET
C2F ENDP

Please help me out


Solution

  • fmul, fdiv, fadd edit data in floating stack directly, so instead of pulling from stack to register, do operation directly in floating stack.

    Correct usage of floating point stack in conversion.asm:

    .DATA
    five DWORD 5.0
    nine DWORD 9.0
    ttw DWORD 32.0
    
    .CODE
    C2F proc
        fmul nine
        fdiv five
        fadd ttw
        ret
    C2F ENDP
    

    For reading float and writing float I used Irvine library, which uses floating point stack:

    Include Irvine32.inc
    Include conversion.asm
    
    .data
    Celprompt BYTE "Enter a value in C:",0
    Cel DWORD 0
    Resprompt BYTE "In Farenheit that value is - "
    Faren DD ?
    
    .code
    main PROC
      mov edx, OFFSET Celprompt
      call WriteString
      call Crlf
      call ReadFloat
      call C2F
      call Crlf
      mov edx, OFFSET Resprompt
      call WriteString
      call WriteFloat
    main ENDP
    END main