[erlang-questions] Understanding BEAM bytecode [Code section]
Gabriel Rivas
gabriel.alberto.rivas@REDACTED
Fri Sep 15 15:11:20 CEST 2017
Thanks Dmytro, I understand it now, you are doing a great job with BEAM
Wisdoms.
On Fri, Sep 15, 2017 at 4:24 AM, Dmytro Lytovchenko <
dmytro.lytovchenko@REDACTED> wrote:
> Similar to bif0...bif3 gc_bifN uses import index from the import table.
> Import table contains triples {Mod, Fun, Arity} for external functions used
> in the current module:
>
> http://beam-wisdoms.clau.se/en/latest/indepth-beam-
> instructions.html#bif1-11-bif2-152-bif3
>
> So Bif=00 is the 0th index in the import table.
>
> http://beam-wisdoms.clau.se/en/latest/indepth-beam-file.
> html#impt-imports-table
>
> 2017-09-14 23:16 GMT+02:00 Gabriel Rivas <gabriel.alberto.rivas@REDACTED>
> :
>
>> Hello all,
>>
>> I am trying to understand BEAM files most specifically the Code section.
>> For that objective I wrote a simple program consisting of just a
>> subtraction function
>> taking 2 parameters:
>>
>> -module(ex3).
>> -export([sub/2]).
>>
>> sub(X,Y) ->
>> X-Y.
>>
>> From that file I created the .S version, and I can see that the assembly
>> version for the subtraction is:
>>
>> {gc_bif,'-',{f,0},2,[{x,0},{x,1}],{x,0}}.
>> return.
>>
>> In the BEAM file I can see that the bytecode part that correlated with
>> this
>> function is (in hex numbers):
>>
>> 7D 05 20 00 03 13 03 13
>>
>> After some reading of the BEAM book and some analysis, I could translate
>> these hex numbers to:
>>
>> 7D 05 20 = {gc_bif2,
>> Lbl = 05,
>> Live X registers = 2,
>> Bif = 00,
>>
>> 03 13 03 13 = args: X0,X1
>> return: X0
>> 13 -> k_return
>>
>> But I still don't see how the subtraction happens. Can anybody shed some
>> light
>> on the internals of calling a BIF in the erlang module?
>>
>> Regards,
>> Gabriel
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170915/7d8d77ca/attachment.htm>
More information about the erlang-questions
mailing list