Ready? Set. Go

This commit is contained in:
parys 2024-01-26 12:54:07 +01:00
commit f2c08962ec
18 changed files with 601 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin/

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# Simple programs written in x86-64 asm as an learning exercises
## Requirements
- [yasm](https://yasm.tortall.net/)
- [ld](https://www.gnu.org/software/binutils/)
## Compiling
```
$ ./build.sh <name>
```

46
addcarry.asm Normal file
View File

@ -0,0 +1,46 @@
section .data
global print
print:
mov rsi, 0
iter:
mov al, byte [rdi + rsi]
inc rsi
cmp al, 0
jne iter
dec rsi
mov rdx, rsi
mov rsi, rdi
mov rax, 1
mov rdi, 1
syscall
mov rax, 0
ret
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
var1 ddq 0x1A000000000000000
var2 ddq 0x2C000000000000000
sum ddq 0
section .text
global _start
_start:
mov rax, qword [var1]
mov rdx, qword [var1 + 8]
add rax, qword [var2]
adc rdx, qword [var2 + 8]
mov qword [sum] , rax
mov qword 8[sum], rdx
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

23
addfloats.asm Normal file
View File

@ -0,0 +1,23 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
x dd 3.4
y dd 3.5
var dd 0.0
section .text
global _start
_start:
movss xmm0, dword [x]
movss xmm1, dword [y]
addss xmm0, xmm1
movss dword [var], xmm0
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

44
build.sh Executable file
View File

@ -0,0 +1,44 @@
#!/bin/sh
create_bin="n"
set -e
if [ ! $1 ]; then
echo "Provide a filename to compile!"
exit 1
fi
if [ ! -f $1.asm ]; then
echo "Provided filename doesn't corespond to an exisitng file!"
exit 1
fi
if [ ! -d "./bin/" ]; then
echo "bin directory doesn't exist"
read -p "Should I create one? (y/N) " create_bin
if [ ! $create_bin ]; then
exit 1
fi
if [ $create_bin = "y" ]; then
mkdir bin
else
exit 1
fi
fi
cd bin
if [ -d "$1" ]; then
cd $1
else
mkdir $1 && cd $1
fi
yasm -Worphan-labels -g dwarf2 -f elf64 ../../$1.asm -o $1.o -l $1.lst
ld -g -o $1 $1.o
echo "Compilation successful!"

23
execaprogram.asm Normal file
View File

@ -0,0 +1,23 @@
section .data
NULL equ 0
newline db 10, NULL
newline_size equ $-newline
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
progName db "/bin/sh", NULL
section .text
global _start
_start:
mov rax, 59
mov rdi, progName
syscall
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

26
howmanyargs.asm Normal file
View File

@ -0,0 +1,26 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
argc db 0, 0
section .text
global _start
_start:
pop r8
add r8, 0x30
mov byte [argc], r8b
mov byte [argc + 1], 10
mov rax, 1
mov rdi, 1
mov rsi, argc
mov rdx, 2
syscall
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

25
loop.asm Normal file
View File

@ -0,0 +1,25 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
iter_cnt db 10
sum dw 0
section .text
global _start
_start:
mov cl, byte [iter_cnt]
mov ax, 2
iter:
add word [sum], ax
shl ax, 1
dec cl
cmp cl, 0
jne iter
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

18
main.asm Normal file
View File

@ -0,0 +1,18 @@
section .data
NULL equ 0
newline db 10, NULL
newline_size equ $-newline
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
section .text
global _start
_start:
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

19
mul.asm Normal file
View File

@ -0,0 +1,19 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
result dw 0
section .text
global _start
_start:
mov al, 0x05
mov bl, 0x04
imul bl
mov word [result], ax
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

65
printanumber.asm Normal file
View File

@ -0,0 +1,65 @@
section .data
NULL equ 10
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
num dd 1234567
num_len db 0
section .bss
string resb 255
section .text
global _start
_start:
mov eax, dword [num]
mov r10d, 10
mov rbx, 0
iter0:
mov rdx, 0
div r10d
inc byte [num_len]
cmp ax, 0
jne iter0
mov rbx, 0
mov rax, 0
mov rsi, 0
mov cl, [num_len]
mov eax, dword [num]
iter1:
mov rdx, 0
div r10d
add edx, 0x30
push rdx
inc rsi
loop iter1
mov cl, [num_len]
mov bl, [num_len]
inc bl
mov rsi, 0
iter2:
mov rdx, 0
pop rdx
mov byte [string + rsi], dl
inc rsi
loop iter2
mov byte [string + rsi], NULL
mov rax, 1
mov rdi, 1
mov rsi, string
movzx rdx, bl
syscall
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

70
printcliargs.asm Normal file
View File

@ -0,0 +1,70 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
newline db 10
newline_sz equ $-newline
arge db "Not enough arguments!", 10
arge_size equ $-arge
section .text
global _start
_start:
pop rax
mov r8, rax
mov r9, 0
cmp rax, 2
jb print_error
pop rax
dec r8
_loop0:
pop rax
mov rdx, 0
_loop1:
mov bl, [rax + rdx]
inc rdx
cmp bl, 0
jne _loop1
mov r10, rax
dec rdx
mov rax, 1
mov rdi, 1
mov rsi, r10
syscall
mov rcx, 0
mov rdx, 0
mov r11, 0
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, newline_sz
syscall
mov rdx, 0
inc r9
cmp r8, r9
jne _loop0
jmp exit
print_error:
mov rax, 1
mov rdi, 1
mov rsi, arge
mov rdx, arge_size
syscall
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

78
printfuncandmacro.asm Normal file
View File

@ -0,0 +1,78 @@
%macro printl 2
push rax
push rdi
push rsi
push rdx
push r11
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
pop r11
pop rdx
pop rsi
pop rdi
pop rax
%endmacro
global print
print:
mov rsi, 0
iter:
mov al, byte [rdi + rsi]
inc rsi
cmp al, 0
jne iter
dec rsi
mov rdx, rsi
mov rsi, rdi
mov rax, 1
mov rdi, 1
syscall
mov rax, 0
ret
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
newline db 10, 0
newline_size equ $-newline
msg db "TEST", 0
msg_len equ $-msg
msg2 db "LOLOLLMAOLLLL L L L MDR", 0
msg2_len equ $-msg2
section .bss
input_size equ 255
input resb input_size
section .text
global _start
_start:
printl msg, msg_len
printl newline, newline_size
mov rdi, msg2
call print
mov rdi, newline
call print
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

24
sdivandrem.asm Normal file
View File

@ -0,0 +1,24 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
var db 1
res db 0
section .text
global _start
_start:
mov eax, dword [var]
cdq
mov ebx, 17
idiv ebx
mov dword [res], eax
add ebx, edx
mov dword [rem], ebx
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

27
signedmul.asm Normal file
View File

@ -0,0 +1,27 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
var1 dw 1200
var2 dw -2000
isum dw 0
vsum dd 0
section .text
global _start
_start:
mov ax, word [var1]
imul ax, -13
mov word [isum], ax
mov ax, word [var1]
imul word [var2]
mov word [vsum], ax
mov word [vsum + 2], dx
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

39
simplefunction.asm Normal file
View File

@ -0,0 +1,39 @@
global print
print:
push rax
push rdi
mov rax, 1
mov rdi, 1
syscall
pop rdi
pop rax
ret
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
newline db 10
newline_size equ $-newline
msg db "TEST"
msg_len equ $-msg
section .text
global _start
_start:
mov rsi, msg
mov rdx, msg_len
call print
mov rsi, newline
mov rdx, newline_size
call print
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

37
sumlist.asm Normal file
View File

@ -0,0 +1,37 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
list dd 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
list_len equ $-list
sum dd 0
bsn db 10
section .text
global _start
_start:
mov rsi, 0
mov rbx, list_len
shr rbx, 2
iter:
mov eax, dword [list + (rsi * 4)]
add dword [sum], eax
inc rsi
cmp rsi, rbx
jne iter
mov rax, 1
mov rsi, sum
mov rdx, 1
syscall
mov rax, 1
mov rsi, bsn
mov rdx, 1
syscall
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall

24
udivwithrem.asm Normal file
View File

@ -0,0 +1,24 @@
section .data
EXIT_SUCCESS equ 0
SYS_EXIT equ 60
var db 70
divisor db 3
res db 0
rem db 0
section .text
global _start
_start:
mov ax, 0
mov al, byte [var]
div byte [divisor]
mov byte [res], al
mov byte [rem], ah
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall