This is a mirror of official site: http://jasper-net.blogspot.com/

The __fortran calling convention isn't the calling convention used by FORTRAN

| Thursday, December 23, 2010
Although the Microsoft C compiler supports a calling convention called __fortran, that's just what the calling convention is called; its relationship with the FORTRAN programming language is only coincidental. The __fortran keyword is now just an old-fashioned synonym for __stdcall.

Various FORTRAN compilers use different calling conventions; the one I describe here applies to the now-defunct Microsoft Fortran PowerStation.

Fortran Powerstation pushes parameters on the stack right-to-left, with callee-cleanup. (So far, this matches __fortran aka __stdcall.) Function names are converted to all-uppercase, with an underscore at the beginning and @n appended, where n is the number of bytes of parameters. (This still matches __stdcall aside from the uppercase conversion.)

As for how the parameters are passed, well, that's where things get weird. FORTRAN natively passes all parameters by reference. This is the source of a famous classic FORTRAN bug known as constants aren't.

     PROGRAM MYSTERY
     CALL MAGIC(1)
     PRINT *, 'According to the computer, 3 + 1 is ', ADDUP(3, 1)
     END

     FUNCTION ADDUP(I, J)
     ADDUP = I + J
     END

C     What does this subroutine actually do?
     SUBROUTINE MAGIC(I)
     I = 9
     RETURN

     END
(It's been a long time since I've written a FORTRAN program, so I may have gotten some of the details wrong, but any errors shouldn't detract from the fundamental issue.)

When you run this program, it says

According to the computer, 3 + 1 is 12
How did that happen? We called a function that adds two numbers together, and instead of getting 4, we get 12?

Read more: The old new thing

Posted via email from .NET Info

0 comments: