-
-
Notifications
You must be signed in to change notification settings - Fork 359
Reworth the 7th chapter #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ee3960
Reworth the 7th chapter
0xAX 86a6caf
asm-7: fix links
0xAX eeec2bc
asm-1: Update Linus distributions list
0xAX 18a0373
asm-7: update after editoral changes
0xAX 0a8b067
asm-7: apply editoral changes
0xAX 8b592f9
asm-7: improve introduction
0xAX c89a4e4
asm-7: last fixes
0xAX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,6 @@ sum/sum | |
stack/stack | ||
strings/reverse | ||
float/dot_product | ||
casm/casm1/casm | ||
casm/casm2/casm | ||
casm/casm3/casm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Assembly interaction with high-level programming languages | ||
|
||
These are three simple programs which provides examples of interaction of assembly and C programming languages: | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- casm1 - call C function from asm | ||
- casm2 - gcc inline assembly | ||
- casm3 - call asm function from C | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To build each program, go to the directory with it and run: | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```bash | ||
make | ||
``` | ||
|
||
For more details, read [Part 7. Interaction of assembly and high-level programming languages](https://github.com/0xAX/asm/blob/master/content/asm_7.md). | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
global _start | ||
;; Definition of the `data` section | ||
section .data | ||
;; String `msg` variable with the value `hello world!` | ||
msg db "hello, world!" | ||
|
||
extern print | ||
;; Reference to the C stdlib functions that we will use | ||
extern write, exit | ||
|
||
;; Definition of the text section | ||
section .text | ||
;; Reference to the entry point of our program | ||
global _start | ||
|
||
;; Entry point | ||
_start: | ||
call print | ||
;; Set the first argument of `write` function to 1 (`stdout`). | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mov rdi, 1 | ||
;; Set the second argument of `write` function to the reference of the `msg` variable. | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mov rsi, msg | ||
;; Set the third argument to the length of the `msg` variable's value (13 bytes). | ||
mov rdx, 13 | ||
;; Call the `write` function. | ||
call write | ||
|
||
mov rax, 60 | ||
mov rdi, 0 | ||
syscall | ||
;; Set the first argument of `sys_exit` to 0. The 0 status code is success. | ||
mov rdi, 0 | ||
;; Call the `exit` function | ||
call exit |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
build: | ||
gcc casm.c -o casm | ||
all: | ||
gcc casm.c -o casm | ||
|
||
clean: | ||
rm -rf *.o | ||
rm -rf casm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
int main() { | ||
char* str = "Hello World\n"; | ||
long len = strlen(str); | ||
int ret = 0; | ||
|
||
__asm__("movq $1, %%rax \n\t" | ||
"movq $1, %%rdi \n\t" | ||
"movq %1, %%rsi \n\t" | ||
"movl %2, %%edx \n\t" | ||
"syscall" | ||
: "=g"(ret) | ||
: "g"(str), "g" (len)); | ||
__asm__("movq $1, %%rax \n\t" // rax = 1 - Specify the number of the system call (1 is `sys_write`). | ||
"movq $1, %%rdi \n\t" // rdi = 1 - Set the first argument of `sys_write` to 1 (`stdout`). | ||
"movq %1, %%rsi \n\t" // rsi = str - Set the second argument of `sys_write` to the reference of the `str` variable. | ||
"movq %2, %%rdx \n\t" // rdx = len(str) - Set the third argument of `sys_write` to the length of the `str` variable's value. | ||
"syscall" // Call the `sys_write` system call. | ||
: "=g"(ret) // Return the result in the `ret` variable. | ||
: "g"(str), "g" (len)); // Put `str` and `len` variables in any general operand (memory, register or immediate if possible) | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
printf("Bytes written: %d\n", ret); | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
build: | ||
all: | ||
nasm -f elf64 -o casm.o casm.asm | ||
gcc casm.o casm.c -o casm | ||
|
||
clean: | ||
rm -rf *.o | ||
rm -rf casm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
global printHelloWorld | ||
|
||
;; Definition of the text section | ||
section .text | ||
printHelloWorld: | ||
;; 1 arg | ||
mov r10, rdi | ||
;; 2 arg | ||
mov r11, rsi | ||
;; call write syscall | ||
mov rax, 1 | ||
mov rdi, 1 | ||
mov rsi, r10 | ||
mov rdx, r11 | ||
syscall | ||
ret | ||
;; Reference to the entry point of our program | ||
global my_strlen | ||
|
||
;; Function that returns the length of the string passed via the first argument | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
my_strlen: | ||
;; Reset the register value to zero. It will be returned from the | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
;; function with the result. | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
xor rax, rax | ||
.loop: | ||
;; Compare the first element in the given string with the `NUL` terminator (end of the string). | ||
cmp byte [rdi + rax], 0 | ||
;; If we reached the `NUL` terminator exit from the function. | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
je .done | ||
;; Increase the counter which stores the length of the string. | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inc rax | ||
;; Repeat the operations above while we didn't reach the end of the string. | ||
klaudiagrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
jmp .loop | ||
.done: | ||
;; Exit from the function and return the result in the `rax` register. | ||
ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
extern void printHelloWorld(char *str, int len); | ||
extern int my_strlen(const char *str); | ||
|
||
int main() { | ||
char* str = "Hello World\n"; | ||
int len = strlen(str); | ||
printHelloWorld(str, len); | ||
return 0; | ||
int main(int argc, char *argv[]) { | ||
if (argc != 2) { | ||
fprintf(stderr, "Error: this program must have 1 command line argument\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
printf("The argument length is - %d\n", my_strlen(argv[1])); | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.