0% found this document useful (0 votes)
132 views2 pages

(Assembly) Interfacing With High Level Language

Interfacing with high level language

Uploaded by

mforytb1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views2 pages

(Assembly) Interfacing With High Level Language

Interfacing with high level language

Uploaded by

mforytb1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Interfacing High-Level and Assembly

Languages
This section explores how high-level and assembly languages can interact with each other,
specifically focusing on calling functions between these paradigms. The key mechanism for this
interoperation is the standard calling convention, which defines rules for passing arguments,
returning values, and managing the stack.

Calling Assembly from High-Level Languages

To call an assembly function from a high-level language like C or C++, you typically declare the
assembly function as an external symbol using the extern keyword. This informs the compiler
that the function's definition is located outside the current source file.

Example: C++ Main Calling an Assembly Function

C++

#include <iostream>

extern "C" void stats(int[], int, int *, int *);

int main() {
// ... (rest of the C++ code)
}

The extern "C" declaration ensures that the function is linked using the C calling convention,
which is often compatible with assembly language conventions.

Calling High-Level from Assembly

While less common, it's possible to call high-level language functions from assembly. This
typically involves:

1. Obtaining Function Addresses: Determine the memory address of the high-level function
using techniques like symbol lookup or dynamic loading.
2. Preparing Arguments: Place arguments on the stack according to the calling convention.
3. Calling the Function: Use a jump or call instruction to transfer control to the function's
address.
4. Retrieving Return Values: If the function returns a value, retrieve it from the appropriate
register or stack location.
Compilation and Linking

To link high-level and assembly language code together, use the appropriate compiler and
assembler tools. For example, in a Linux environment using GCC and YASM:

Bash

gcc -c main.c # Compile the C code


yasm -f elf64 stats.asm # Assemble the assembly code
gcc main.o stats.o -o program # Link the object files

Additional Considerations

● Data Types: Ensure that data types used in high-level and assembly languages are
compatible.
● Register Usage: Be mindful of register conventions used by both languages to avoid
conflicts.
● Stack Management: Properly manage the stack to avoid overflows and underflows.
● Debugging: Use debuggers to step through code, inspect variables, and identify issues.

By understanding the standard calling convention and following these guidelines, you can
effectively interface between high-level and assembly languages to create more efficient or
specialized programs.

You might also like