Are I/O statements in FreeBasic compiled as function calls?

115 views Asked by At

Example:

Dim x As Integer, y As Integer
Input "x=", x
y = x ^ 3 + 3 * x ^ 2 - 24 * x + 30
Print y
End

When I used FreeBasic compiler to generate the assembly code of this source code, I found

.globl _main
_main:

and

call ___main

in assembly code. In addition, it looks like that the Input statement is compiled as

call _fb_ConsoleInput@12

and

call _fb_InputInt@4

The "^" operator is compiled as

call _pow

(I am not sure whether the math function library of FreeBasic is integrated or external)

and the Print statement is compiled as

call _fb_PrintInt@12

and the End statement is compiled as

call _fb_End@4

The question is: How is FreeBasic source code compiled? Why _main and ___main appeared in assembly code? Are I/O statements compiled as function calls?


Reference: Assembly code generated by FreeBasic compiler

    .intel_syntax noprefix

.section .text
.balign 16

.globl _main
_main:
push ebp
mov ebp, esp
and esp, 0xFFFFFFF0
sub esp, 20
mov dword ptr [ebp-4], 0
call ___main
push 0
push dword ptr [ebp+12]
push dword ptr [ebp+8]
call _fb_Init@12
.L_0002:
mov dword ptr [ebp-8], 0
mov dword ptr [ebp-12], 0
push -1
push 0
push 2
push offset _Lt_0004
call _fb_StrAllocTempDescZEx@8
push eax
call _fb_ConsoleInput@12
lea eax, [ebp-8]
push eax
call _fb_InputInt@4
push dword ptr [_Lt_0005+4]
push dword ptr [_Lt_0005]
fild dword ptr [ebp-8]
sub esp,8
fstp qword ptr [esp]
call _pow
add esp, 16
fild dword ptr [ebp-8]
fild dword ptr [ebp-8]
fxch st(1)
fmulp
fmul qword ptr [_Lt_0005]
fxch st(1)
faddp
mov eax, dword ptr [ebp-8]
imul eax, 24
push eax
fild dword ptr [esp]
add esp, 4
fxch st(1)
fsubrp
fadd qword ptr [_Lt_0006]
fistp dword ptr [ebp-12]
push 1
push dword ptr [ebp-12]
push 0
call _fb_PrintInt@12
push 0
call _fb_End@4
.L_0003:
push 0
call _fb_End@4
mov eax, dword ptr [ebp-4]
mov esp, ebp
pop ebp
ret

.section .data
.balign 4
_Lt_0004:   .ascii  "x=\0"
.balign 8
_Lt_0005:   .quad   0x4008000000000000
.balign 8
_Lt_0006:   .quad   0x403E000000000000
1

There are 1 answers

4
MemReflect On

Yes, things like PRINT are implemented as function calls, though i am not sure why this matters to you unless you are currently learning assembly.

As for _main, that is the ASM name for the main() C function used as the main program. On x86, it is common for global/exported function names in C to be preceded by _ in the ASM output.

___main is the ASM name for the __main() C function called by the MinGW C runtime library startup code before anything in _main is executed. Again, you'll see the extra _ preceding the C function name.

After that is a call to fb_Init(argc, argv, FB_LANG_FB) to initialize the FreeBASIC runtime library with the default "fb" FreeBASIC dialect and argc elements in the argument vector argv. The @12 means the argument list is 12 bytes long (e.g., 4+4+4=12 as with fb_Init here); see __stdcall | Microsoft Docs for more information on that.