In the call to "num" edx is zeroed before the return. In the "C" part, all that is done after the return is push eax and call cf2. So edx is 0 (zero)
Assuming that the C compiler leaves edx intact! Actually, I had not looked at the C code, and tested the asm code in isolation - my fault. So,
if the C compiler kindly does not touch edx before calling c2f,
then edx is still zero, and the cdq is not needed. Still, it would be very bad to rely on that behaviour (besides, mov edx, 0 is 5 bytes, cdq is only one). Furthermore, the
cdq with
idiv allows negative temperatures, the
mov edx, 0 doesn't.
Here is complete code for use with Pelles C and MASM:
#include <stdio.h>
#pragma comment(linker, "c2f.obj /subsystem:console" )
extern int num();
extern double c2f(int n);
int main() {
int number = num();
// __asm int 3;
double ret = c2f(number);
printf("c2f returned %f\n", ret);
_getch();
return 0;
}
.486 ; create 32 bit code
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive
include \Masm32\include\msvcrt.inc
includelib \Masm32\lib\msvcrt.lib
include \Masm32\include\Kernel32.inc
includelib \Masm32\lib\Kernel32.lib
.data
message db "The temperture in Fahrenheits: %d Reminder: %d", 10, 0
request db "Enter a temperature in Celsius: ", 0
celsius dd 0 ; 32-bits integer = 4 bytes
formatin db "%d", 0
txCrt db "msvcrt.dll", 0
txPrintf db "printf", 0
txScanf db "scanf", 0
.code
num proc C
; Ask for an integer
push offset request
invoke LoadLibrary, offset txCrt
invoke GetProcAddress, eax, offset txPrintf
call dword ptr eax ; crt_printf
add esp, 4 ; remove the parameter
push offset celsius ; address of integer1, where the input is going to be stored (second parameter)
push offset formatin ; arguments are right to left (first parameter)
invoke LoadLibrary, offset txCrt
invoke GetProcAddress, eax, offset txScanf
call dword ptr eax ; crt_printf
add esp, 8 ; remove the parameters
; Move the value under the address integer1 to EAX
; mov edx, 0 wrong
mov eax, celsius
ret
num endp
c2f proc C arg
mov eax, arg
imul eax,9
add eax,160
mov ebx, 5
; INT 3
cdq ; allow negative
idiv ebx ; temperatures
push eax
fild dword ptr [esp]
pop eax
ret
c2f endp
END