News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How do compilers generate asm code for functions, subs.

Started by mhmooney, August 01, 2020, 10:38:36 PM

Previous topic - Next topic

mhmooney

 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--

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.

nidud

deleted

TouEnMasm

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

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
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

mhmooney

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

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.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

mhmooney

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

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--

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