C++ compiler explorer

General FreeBASIC programming questions.
Post Reply
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

C++ compiler explorer

Post by srvaldez »

the C++ compiler explorer is a great tool to learn assembly https://godbolt.org/
here's a Windows-32 example to illustrate, in the compiler selection list choose x86 msvc v19.0 (WINE) with compiler options /arch:SSE2 /O2
then paste this C code in the left pane

Code: Select all

#include <math.h>

void div_dbl (double *result, double *x, double *y)
{
	*result = (*x) / (*y);
}
the resulting asm code on the right pane, shows

Code: Select all

_result$ = 8                                            ; size = 4
_x$ = 12                                                ; size = 4
_y$ = 16                                                ; size = 4
void div_dbl(double *,double *,double *) PROC                         ; div_dbl, COMDAT
        mov     eax, DWORD PTR _x$[esp-4]
        movsd   xmm0, QWORD PTR [eax]
        mov     eax, DWORD PTR _y$[esp-4]
        divsd   xmm0, QWORD PTR [eax]
        mov     eax, DWORD PTR _result$[esp-4]
        movsd   QWORD PTR [eax], xmm0
        ret     0
void div_dbl(double *,double *,double *) ENDP                         ; div_dbl
after minor editing you have a FB version

Code: Select all

const _result = 8                                            ' size = 4
const _x = 12                                                ' size = 4
const _y = 16                                                ' size = 4
sub divl_dbl Naked Cdecl(byref result as double, byref x as double, byref y as double)
	asm
        mov     eax, DWORD PTR _x[esp-4]
        movsd   xmm0, QWORD PTR [eax]
        mov     eax, DWORD PTR _y[esp-4]
        divsd   xmm0, QWORD PTR [eax]
        mov     eax, DWORD PTR _result[esp-4]
        movsd   QWORD PTR [eax], xmm0
        ret     0
	end asm
end sub

dim as double res, x=7, y=9
divl_dbl(res, x, y)
Print res
but what is really outstanding is the mouse over help, simply place the the mouse pointer over any asm instruction to get a detailed explanation of what that instruction does

result from x64 msvc v19.0 (WINE)

Code: Select all

result$ = 8
x$ = 16
y$ = 24
void div_dbl(double * __ptr64,double * __ptr64,double * __ptr64) PROC                          ; div_dbl, COMDAT
        movsd   xmm0, QWORD PTR [rdx]
        divsd   xmm0, QWORD PTR [r8]
        movsd   QWORD PTR [rcx], xmm0
        ret     0
void div_dbl(double * __ptr64,double * __ptr64,double * __ptr64) ENDP
FB version

Code: Select all

sub divl_dbl Naked Cdecl(byref result as double, byref x as double, byref y as double)
	asm
        movsd   xmm0, QWORD PTR [rdx]
        divsd   xmm0, QWORD PTR [r8]
        movsd   QWORD PTR [rcx], xmm0
        ret     0
	end asm
end sub

dim as double res, x=7, y=9
for x=1 to 8
	divl_dbl(res, x, y)
	Print res
next
Post Reply