-
Notifications
You must be signed in to change notification settings - Fork 0
/
Essay
42 lines (32 loc) · 2.75 KB
/
Essay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
High-Level Block Diagram of a Call Stack
```
|-------------------|
| Return Address |
|-------------------|
| Saved Registers |
|-------------------|
| Local Variables |
|-------------------|
| Function Arguments|
|-------------------|
```
Theory of Operation
1. Passing Parameters: When a C program calls an assembly language routine (Example is the extern function), the parameters are typically passed on the stack.
The C program pushes the arguments onto the stack in reverse order (right to left), so the first argument is at the top of the stack when the assembly routine starts executing.
2. Returning a Value: The return value from an assembly language routine is typically passed back to the C program in a register.
The specific register used can vary depending on the system architecture, but it's often the `eax` register for 32-bit systems and the `rax` register for x64/86-bit systems.
3. Returning to the Calling Program: After the assembly language routine has finished executing, it needs to return control to the calling C program.
This is typically done by popping the return address off the stack and jumping to that address. The `ret` instruction in assembly language handles this.
4. Cleaning Up the Stack: Cleaning the Stack on the assembly side of the program is extremely important as the stack has the return address in the register. In most Assembly programs, you will see the function 'pop eax' or a register that needs its stack cleaned.
This 'popping' of a register is necessary because the return address/the function that we need to access is below what we put on the stack.
This involves popping off the arguments that were passed to the assembly routine. If the C program is using the `cdecl` calling convention, it's responsible for
cleaning up the stack. If it's using the `stdcall` calling convention, the assembly routine is responsible for cleaning up the stack.
-Summary-
When programming in C and Assembly, which are both low-level programs, you can use both of them by referencing one another. Both C and ASM work hand in hand with each other with Windows,
in C, you can refer to almost any other programming language by using the 'extern' function, as referenced in my C code on lines 3-6; you can see that it's looking for the asm functions of each one inside the file system.
The assembly program has PUBLIC functions for each one, making it readable from the C code and the program can look for it.
References;
StackOverflow.com https://stackoverflow.com/questions/13901261/calling-assembly-function-from-c
https://stackoverflow.com/questions/8054362/mixing-c-and-assembly-files
https://stackoverflow.com/questions/65837016/how-to-call-a-function-in-an-external-assembly-file
cs.uaf.edu https://www.cs.uaf.edu/courses/cs301/2014-fall/notes/linker/