Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Practic calling C functions from x86_64 assmbly cod AND x86_64 assmbly functions

ID: 3884502 • Letter: P

Question

Practic calling C functions from x86_64 assmbly cod AND x86_64 assmbly functions from C cod (below listed each file needed)

Crat an assmbly program (asm_program.asm) which calls a C function (c_function.c).

Your C function should tak on argumnt [a pointr to a charactr string] and print it (g. printstring(const char* mystring)).

Your assmbly program should call th C function to print out a haiku (classic or slf-authord) of your choosing, and th author's nam.

Your haiku should b dclard and dfind in asm_program.asm [NOT in c_function.c].

Compil and link your cod to mak an xcutabl (asm_program).

Crat a C program (c_program.c) which calls an assmbly function (asm_function.asm).

Your assmbly function should tak on argumnt [a pointr to a charactr string] and print it.

Your C program should call th assmbly function to print out a haiku (classic or slf-authord) of your choosing, and th author's nam.

Your haiku should b dclard and dfind in c_program.c [NOT in asm_function.asm].

Compil and link your cod to mak an xcutabl (c_program).

create each of these files and LABEL each please

COMMENT THE NAME OF EACH FILE AT THE TOP PLS

asm_program.asm

asm_function.asm

c_program.c

c_function.c

asm_program

c_program

Explanation / Answer

Often, all the code for a single application is written in the same source language. This is usually a high-level language such as C or C++ which is then compiled to ARM assembly code.

However, in some situations you might want to make function calls from C/C++ code to assembly code. For example:

If you want to make use of existing assembly code, but the rest of your project is in C or C++.
If you want to manually write critical functions directly in assembly code that can produce better optimized code than compiling C or C++ code.
If you want to interface directly with device hardware and if this is easier in low-level assembly code than high-level C or C++.

For code portability, it is better to use intrinsics or inline assembly rather than writing and calling assembly functions.

To call an assembly function from C or C++:

In the assembly source, declare the code as a global function using .globl and .type:

.globl myadd .p2align 2 .type myadd,%function myadd: // Function "myadd" entry point. .fnstart add r0, r0, r1 // Function arguments are in R0 and R1. Add together and put the result in R0. bx lr // Return by branching to the address in the link register. .fnend

Note:

armclang requires that you explicitly specify the types of exported symbols using the .type directive. If the .type directive is not specified in the above example, the linker outputs warnings of the form:

Warning: L6437W: Relocation #RELA:1 in test.o(.text) with respect to myadd...

Warning: L6318W: test.o(.text) contains branch to a non-code symbol myadd.

In C code, declare the external function using extern:

#include <stdio.h> extern int myadd(int a, int b); int main() { int a = 4; int b = 5; printf("Adding %d and %d results in %d ", a, b, myadd(a, b)); return (0); }

In C++ code, use extern "C":

extern "C" int myadd(int a, int b);

Ensure that your assembly code complies with the Procedure Call Standard for the ARM Architecture (AAPCS).

The AAPCS describes a contract between caller functions and callee functions. For example, for integer or pointer types, it specifies that:

Registers R0-R3 pass argument values to the callee function, with subsequent arguments passed on the stack.
Register R0 passes the result value back to the caller function.
Caller functions must preserve R0-R3 and R12, because these registers are allowed to be corrupted by the callee function.
Callee functions must preserve R4-R11 and LR, because these registers are not allowed to be corrupted by the callee function.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote