-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexander Kuleshov <[email protected]>
- Loading branch information
Showing
7 changed files
with
395 additions
and
184 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -50,3 +50,6 @@ modules.order | |
Module.symvers | ||
Mkfile.old | ||
dkms.conf | ||
|
||
# Our programs | ||
sum/sum |
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,8 +1,9 @@ | ||
This is "sum of 2 numbers" with assembly for Linux x86-64 | ||
This is a basic program that gets the sum of two integer numbers and check that it is correct. It prints the message if the sum is correct, otherwise just exits. | ||
|
||
Build it with: `make` | ||
Build the program executing: | ||
|
||
More details at - [Say hello to x64 Assembly [part 2]](https://github.com/0xAX/asm/blob/master/content/asm_2.md) | ||
|
||
[@0xAX](https://x.com/0xAX) | ||
```bash | ||
make | ||
``` | ||
|
||
Read the details in the - [Part 2. The `x86_64` concepts](https://github.com/0xAX/asm/blob/master/content/asm_2.md) |
This file contains 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,47 +1,53 @@ | ||
;initialised data section | ||
;; Definition of the .data section | ||
section .data | ||
; Define constants | ||
num1: equ 100 | ||
num2: equ 50 | ||
; http://geekswithblogs.net/MarkPearl/archive/2011/04/13/more-nasm.aspx | ||
msg: db "Sum is correct", 10 | ||
;; The first number | ||
num1 dq 0x64 | ||
;; The second number | ||
num2 dq 0x32 | ||
;; The message to print if the sum is correct | ||
msg db "The sum is correct!", 10 | ||
|
||
;; | ||
;; program code | ||
;; | ||
;; Definition of the .text section | ||
section .text | ||
global _start | ||
;; Reference to the entry point of our program | ||
global _start | ||
|
||
;; entry point | ||
;; Entry point | ||
_start: | ||
; get sum of num1 and num2 | ||
mov rax, num1 | ||
mov rbx, num2 | ||
add rax, rbx | ||
; compare rax with correct sum - 150 | ||
cmp rax, 150 | ||
; if rax is not 150 go to exit | ||
jne .exit | ||
; if rax is 150 print msg | ||
jmp .rightSum | ||
;; Set the value of the num1 to the rax | ||
mov rax, [num1] | ||
;; Set the value of the num2 to the rbx | ||
mov rbx, [num2] | ||
;; Get sum of the rax and rbx. The result is stored in the rax. | ||
add rax, rbx | ||
.compare: | ||
;; Compare the value of the rax with `150` | ||
cmp rax, 150 | ||
;; Go to the .exit label if the values of the rax and 150 are not equal | ||
jne .exit | ||
;; Go to the .correctSum label if the values of the rax and 150 are equal | ||
jmp .correctSum | ||
|
||
; Print message that sum is correct | ||
.rightSum: | ||
;; write syscall | ||
mov rax, 1 | ||
;; file descritor, standard output | ||
mov rdi, 1 | ||
;; message address | ||
mov rsi, msg | ||
;; length of message | ||
mov rdx, 15 | ||
;; call write syscall | ||
syscall | ||
; exit from program | ||
jmp .exit | ||
; Print message that the sum is correct | ||
.correctSum: | ||
;; Number of the sytem call. 1 - `sys_write`. | ||
mov rax, 1 | ||
;; The first argument of the `sys_write` system call. 1 is `stdout`. | ||
mov rdi, 1 | ||
;; The second argument of the `sys_write` system call. Reference to the message. | ||
mov rsi, msg | ||
;; The third argument of the `sys_write` system call. Length of the message. | ||
mov rdx, 20 | ||
;; Call the `sys_write` system call. | ||
syscall | ||
; Go to the exit of the program. | ||
jmp .exit | ||
|
||
; exit procedure | ||
.exit: | ||
mov rax, 60 | ||
mov rdi, 0 | ||
syscall | ||
;; Number of the system call. 60 - `sys_exit`. | ||
mov rax, 60 | ||
;; The first argument of the `sys_exit` system call. | ||
mov rdi, 0 | ||
;; Call the `sys_exit` system call. | ||
syscall |