The MASM Forum

General => The Campus => Topic started by: mhmooney on August 01, 2020, 10:38:36 PM

Title: How do compilers generate asm code for functions, subs.
Post by: mhmooney 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?
Title: Re: How do compilers generate asm code for functions, subs.
Post by: hutch-- 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.
Title: Re: How do compilers generate asm code for functions, subs.
Post by: nidud on August 01, 2020, 11:06:11 PM
deleted
Title: Re: How do compilers generate asm code for functions, subs.
Post by: TouEnMasm 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

Title: Re: How do compilers generate asm code for functions, subs.
Post by: daydreamer on August 02, 2020, 12:22:21 AM
Quote from: TouEnMasm 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
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
Title: Re: How do compilers generate asm code for functions, subs.
Post by: mhmooney 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.
Title: Re: How do compilers generate asm code for functions, subs.
Post by: mineiro on August 02, 2020, 07:34:59 AM
Quote from: mhmooney on August 01, 2020, 10:38:36 PM
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.
Title: Re: How do compilers generate asm code for functions, subs.
Post by: mhmooney 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?
Title: Re: How do compilers generate asm code for functions, subs.
Post by: K_F 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 ;)
Title: Re: How do compilers generate asm code for functions, subs.
Post by: hutch-- 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