This is a simple x86-64 Assembly calculator, written for a Linux kernel
To install and use the program, you must follow those steps:
- Download the repository with either the GitHub
Downloadbutton or with typing
git clone https://github.com/fif3x/x86-64LINUXcalculator.gitin the terminal
- Install dependencies with running the installDependencies.sh script note: it's written for debian-based systems
- Build project with running the build.sh script
- Go to
bin/and open the.outfile
Lets break down the program into multiple parts:
.datasection and its contests: In the.datasection, we store the string foroutputand its length like this:
output db 0, 10 ; space for 1 char and a newline char
length equ $ - output- Operation selection
To select an operation, we change the value of the
r14register in line 25:
mov r14, 1 ; mode (1:add, 2:sub, 3:mul, 4:div)- Printing
After preforming an operation, we store the result in
r8and call the_printfunction using
mov r8, r12 ; store in r8 to print
call _print ; call print functionthen in the _print function we firstly make r8 an ascii text, add the last byte of r8b to output, and then also add a new line to the output
add r8, '0'
mov byte [output], r8b
mov byte [output+1], 10lastly, we call the Linux write function, using the following values:
rax = 1 for syscall:write
rdi = 1 for stdout
rsi = address of output to point to it
rdx = length of output
so the code looks like this:
mov rax, 1 ; syscall: write
mov rdi, 1 ; stdout
lea rsi, [rel output] ; output address
mov rdx, length ; output length
syscall
ret ; return control- Ending program
After completing everything needed, we clear the registers using
xor reg, reg, which is a bitwise operator makingregequal to 0, and we call the syscall for exiting:
; clear registers
xor r12, r12
xor r13, r13
xor rdx, rdx
xor r8, r8
xor rsi, rsi
xor r14, r14
mov rax, 60 ; exit program
xor rdi, rdi ; code 0
syscall