Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 
main.cpp
#include <stdio.h>

extern "C" double FtoC(double deg_f);
extern "C" double CtoF(double deg_c);

int main(int argc, char* argv[])
{
    double deg_fvals[] = {-459.67, -40.0, 0.0, 32.0, 72.0, 98.6, 212.0};
    int nf = sizeof(deg_fvals) / sizeof(double);

    for (int i = 0; i < nf; i++)
    {
        double deg_c = FtoC(deg_fvals[i]);
        printf("i: %d  f: %10.4lf c: %10.4lf\n", i, deg_fvals[i], deg_c);
    }

    printf("\n");

    double deg_cvals[] = {-273.15, -40.0, -17.77, 0.0, 25.0, 37.0, 100.0};
    int nc = sizeof(deg_cvals) / sizeof(double);

    for (int i = 0; i < nc; i++)
    {
        double deg_f = CtoF(deg_cvals[i]);
        printf("i: %d  c: %10.4lf f: %10.4lf\n", i, deg_cvals[i], deg_f);
    }

    return 0;
}
temperatureconversions.asm
; Name:     temperatureconversions.asm
;
; Build:    g++ -c -m32 main.cpp -o main.o
;           nasm -f elf32 -o temperatureconversions.o temperatureconversions.asm
;           g++ -m32 -o temperatureconversions temperatureconversions.o main.o
;
; Source:   Modern x86 Assembly Language Programming p.104

global FtoC
global CtoF

section .data

    r8_SfFtoC:    dq    0.5555555556    ; 5 / 9
    r8_SfCtoF:    dq    1.8                ; 9 / 5
    i4_32:        dw    32

section .text

; extern "C" double FtoC(double f);
;
; Description:  Converts a temperature from Fahrenheit to Celsius.
;
; Returns:      Temperature in Celsius.

%define    f    [ebp+8]

FtoC:
    push    ebp
    mov     ebp,esp
    fld     qword [r8_SfFtoC]       ;load 5/9
    fld     qword f                 ;load 'f'
    fild    dword [i4_32]           ;load 32
    fsubp                           ;ST(0) = f - 32
    fmulp                           ;ST(0) = (f - 32) * 5/9
    pop     ebp
    ret

; extern "C" double CtoF(double c)
;
; Description:  Converts a temperature from Celsius to Fahrenheit.
;
; Returns:      Temperature in Fahrenheit.

%define    c    [ebp+8]

CtoF:
    push    ebp
    mov     ebp,esp
    fld     qword c                 ;load 'c'
    fmul    qword [r8_SfCtoF]       ;ST(0) = c * 9/5
    fiadd   dword [i4_32]           ;ST(0) = c * 9/5 + 32
    pop     ebp
    ret
build
g++ -c -m32 main.cpp -o main.o
nasm -f elf32 -o temperatureconversions.o temperatureconversions.asm
g++ -m32 -o temperatureconversions temperatureconversions.o main.o