Author Topic: How do compilers generate asm code for functions, subs.  (Read 306 times)

mhmooney

  • Regular Member
  • *
  • Posts: 8
How do compilers generate asm code for functions, subs.
« on: August 01, 2020, 10:38:36 PM »
 Example:

void Sub Main()
int j=0
j=dosomething(j)
End sub

int Function dosomething(int i)
i=i+1
return i
End Function

Is it a matter of using labels, pushing params and jmp back / return to the calling procedure?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7541
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: How do compilers generate asm code for functions, subs.
« Reply #1 on: August 01, 2020, 10:41:57 PM »
Its usually done in the guts of the C compiler, there should be a switch in the settings that will do it for you. Don't know about the 64 bit version of CL.EXE.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

nidud

  • Member
  • *****
  • Posts: 1980
    • https://github.com/nidud/asmc
Re: How do compilers generate asm code for functions, subs.
« Reply #2 on: August 01, 2020, 11:06:11 PM »
You may do this online here: https://gcc.godbolt.org

TouEnMasm

  • Member
  • *****
  • Posts: 1342
    • EditMasm
Re: How do compilers generate asm code for functions, subs.
« Reply #3 on: August 01, 2020, 11:10:16 PM »
At the command line:
The  cl /Fa option and you get an asm file.
In visual studio:
Property of the project >>>output file          option Asm yes not

Fa is a musical note to play with CL

daydreamer

  • Member
  • *****
  • Posts: 1360
  • building nextdoor
Re: How do compilers generate asm code for functions, subs.
« Reply #4 on: August 02, 2020, 12:22:21 AM »
At the command line:
The  cl /Fa option and you get an asm file.
In visual studio:
Property of the project >>>output file          option Asm yes not
Turn off optimisation first to avoid getting fast but cryptic code for newbie
If you have many functions in source files, mixed asm with source option it's easier to find separate function and follow translation, use text editor search text function few times to go from proc declaration very early in asm file a lot farther down, local variables and beginning of proc, asm code and endp
Prepare for very vertical asm file

If you want to get insight on assembler works,asm combined with binary is also alternative

Edit: forgot my experience with modern c compilers you need to call it from main,otherwise it's size optimisation don't care to compile it because it's unused
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D
I love assembly,because its legal to write
princess:lea eax,luke
:)

mhmooney

  • Regular Member
  • *
  • Posts: 8
Re: How do compilers generate asm code for functions, subs.
« Reply #5 on: August 02, 2020, 05:01:42 AM »
Perhaps I'm not being clear. I have a hobby language parser I'm trying to generate the asm code so I can shell to ml and link to produce a executable. I could generate procs as needed but was wondering how in pure asm would it be done. I am new to asm and understand some basics but a guru I am not.

mineiro

  • Member
  • ****
  • Posts: 617
Re: How do compilers generate asm code for functions, subs.
« Reply #6 on: August 02, 2020, 07:34:59 AM »
Is it a matter of using labels, pushing params and jmp back / return to the calling procedure?
Not exactly but yes, correct thinking.
They follow "calling conventions","ABI", so your function can call their functions and their functions can call yours.
Basically you create a start point where your code will execute (main, _start, ...), create local (probably stack frames) or global variables depending of context, ... .
An example is that functions usually use AX,EAX,RAX register being in ms-dos,windows or linux as return function values. So, after calling dosomething function you need take value from AX,EAX,RAX register.
If you compare linux and windows as an example, will perceive that both uses different registers to be used as function parameters, and perceive that both need some registers to be preserved between calling function, while others registers can have their values destroyed by called function. So, you can create a sense here.
We can create our own conventions, so some persons create like a dispatcher function to adjust, organize function parameters to their specific rules.
So, follow the rules to be compatible.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

mhmooney

  • Regular Member
  • *
  • Posts: 8
Re: How do compilers generate asm code for functions, subs.
« Reply #7 on: August 02, 2020, 10:33:46 AM »
How would you return to the calling routine, have a label immediately after the call and jmp to it, return value in eax to resume normal execution of the calling routine?

K_F

  • Member
  • *****
  • Posts: 1609
  • Anybody out there?
Re: How do compilers generate asm code for functions, subs.
« Reply #8 on: August 03, 2020, 03:41:19 AM »
If I understand you correctly.

a) You can leave it to the assembler to do that - and check the sequence in Olly or some other debuggger.

b) You can jump to the label after the call (IOW, exiting the procedure), but this requires you adjusting the stack, which requires info (local vars, parameters..etc) on the procedure you're 'exiting'.
You'll have lots of fun doing this ;)
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7541
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: How do compilers generate asm code for functions, subs.
« Reply #9 on: August 03, 2020, 04:06:11 AM »
If its the parser you want to work on, start looking at source code that works with the assembler you choose. Now to do this you will have to learn some assembler notation but its reasonably easy stuff to parse because the mnemonics are short and the registers are limited, variables can be longer but don't have to be at the level of the parser.

Here is one of my favourites as I can always remember it without looking up anything.

  mov rax, rcx
  sub rax, 1
lbl:
  add rax, 1
  cmp BYTE PTR [rax], 0
  jnz lbl
  sub rax, rcx
  ret
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: