Skip to content

Commit

Permalink
Rework the second chapter
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
  • Loading branch information
0xAX committed Jul 17, 2024
1 parent 541e854 commit 8a804aa
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 184 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

# Our programs
sum/sum
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Have fun!
Here are links to each post:

* [Say hello to x86_64 Assembly part 1](https://github.com/0xAX/asm/blob/master/content/asm_1.md)
* [Say hello to x86_64 Assembly part 2](https://github.com/0xAX/asm/blob/master/content/asm_2.md)
* [Part 2. The `x86_64` concepts](https://github.com/0xAX/asm/blob/master/content/asm_2.md)
* [Say hello to x86_64 Assembly part 3](https://github.com/0xAX/asm/blob/master/content/asm_3.md)
* [Say hello to x86_64 Assembly part 4](https://github.com/0xAX/asm/blob/master/content/asm_4.md)
* [Say hello to x86_64 Assembly part 5](https://github.com/0xAX/asm/blob/master/content/asm_5.md)
Expand Down
473 changes: 333 additions & 140 deletions content/asm_2.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions content/assets/rax.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions content/assets/stack.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 6 additions & 5 deletions sum/README.md
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)
82 changes: 44 additions & 38 deletions sum/sum.asm
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

0 comments on commit 8a804aa

Please sign in to comment.