Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between a label and a function in assembly

have been following an assembly tutorial on youtube here through AT&T syntax. I have just learned about declaring(if that's the correct term here) a function with the .type directive, such as:

.type MyFunction, @function

Now I can define my function like:

MyFunction:
    <code here>

And subsequently call it whenever:

call MyFunction

I know that previous to this in the tutorials, we were simply creating a label that was attached to some code:

MyLabel:
    <code here>

which could then be called like this:

call MyLabel

So my questions are:

What exactly is the difference between a function declared with .type, and and 'function' declared simply with a label? When should one be used over the other, or does it matter?

like image 571
Davies Jeffrey Avatar asked Jul 14 '15 11:07

Davies Jeffrey


People also ask

What is a label in assembly?

A label is a symbol that represents the memory address of an instruction or data. The address can be PC-relative, register-relative, or absolute. Labels are local to the source file unless you make them global using the EXPORT directive. The address given by a label is calculated during assembly.

Is label a function?

Function labels consist of an identifier, followed by a colon. Each such label points to a statement in a function and its identifier must be unique within that function. Other functions may use the same name for a label.

How are functions called in assembly?

A function is called with the instruction “call foo”. This instruction transfers the control of the program to foo function. The two special registers ebp (base pointer) and esp (stack pointer) handles call and return mechanisms of function calls. The values are returned to the calling program via register eax.

What is the benefit of having labels in assembly language?

A "LABEL" is something so that you don't have to manually work out the addresses to jump to. It is not an instruction and takes up no space (excepting as the assembler might align code to make it valid).


1 Answers

Here's what the binutils docs say about the .type directive (assuming you are using the GNU assembler):

This directive is used to set the type of a symbol.

...

For ELF targets, the .type directive is used like this:

.type name , type description

According to the docs on Symbol Type:

The type attribute of a symbol contains relocation (section) information, any flag settings indicating that a symbol is external, and (optionally), other information for linkers and debuggers. The exact format depends on the object-code output format in use.

.type MyFunction, @function marks the label MyFunction (which is a symbol) as a function for the linker or debugger.

When writing pure assembly files by hand, this is unnecessary. Here's an example of a file in which an itoa function is called. As you can see, there are no directives preceding the label.

In summary, labels can be marked as functions, but to executing code, it's just a jump target either way.

like image 90
General Grievance Avatar answered Oct 19 '22 16:10

General Grievance