section .data
num1 dd 1250
num2 dd 250
msgAdd db "Addition = "
lenAdd equ $-msgAdd
msgSub db "Subtraction = "
lenSub equ $-msgSub
msgMul db "Multiplication = "
lenMul equ $-msgMul
msgDiv db "Division = "
lenDiv equ $-msgDiv
section .bss
buffer resb 20
section .text
global _start
_start:
; =================================
; ADDITION
; 12.50 + 2.50 = 15.00
; =================================
mov eax, [num1]
\
add eax, [num2]
push rax
mov eax, 1
mov edi, 1
mov rsi, msgAdd
mov edx, lenAdd
syscall
pop rax
call print_float
; =================================
; SUBTRACTION
; 12.50 - 2.50 = 10.00
; =================================
mov eax, [num1]
sub eax, [num2]
push rax
mov eax, 1
mov edi, 1
mov rsi, msgSub
mov edx, lenSub
syscall
pop rax
call print_float
; =================================
; MULTIPLICATION
; 12.50 × 2.50 = 31.25
; =================================
mov eax, [num1]
mov ebx, [num2]
imul eax, ebx
; Divide by 100 to maintain decimal scale
mov ebx, 100
xor edx, edx
div ebx
push rax
mov eax, 1
mov edi, 1
mov rsi, msgMul
mov edx, lenMul
syscall
pop rax
call print_float
; =================================
; DIVISION
; 12.50 ÷ 2.50 = 5.00
; =================================
mov eax, [num1]
; Multiply by 100 before division
; to maintain two decimal places
imul eax, 100
xor edx, edx
mov ebx, [num2]
div ebx
push rax
mov eax, 1
mov edi, 1
mov rsi, msgDiv
mov edx, lenDiv
syscall
pop rax
call print_float
; =================================
; EXIT
; =================================
mov eax, 60
xor edi, edi
syscall
; =================================
; PRINT FLOAT
; =================================
print_float:
; Divide by 100
xor edx, edx
mov ebx, 100
div ebx
; Save fractional part
push rdx
; Print integer part
mov edi, eax
call print_number
; Print decimal point
mov byte [buffer], '.'
mov eax, 1
mov edi, 1
mov rsi, buffer
mov edx, 1
syscall
; Get fractional part
pop rax
; If fraction is less than 10,
; print leading zero
cmp eax, 10
jae print_fraction
push rax
mov byte [buffer], '0'
mov eax, 1
mov edi, 1
mov rsi, buffer
mov edx, 1
syscall
pop rax
print_fraction:
mov edi, eax
call print_number
; New line
mov byte [buffer], 10
mov eax, 1
mov edi, 1
mov rsi, buffer
mov edx, 1
syscall
ret
; =================================
; PRINT INTEGER
; =================================
print_number:
mov eax, edi
mov ecx, 10
mov rsi, buffer + 19
convert:
xor edx, edx
div ecx
add dl, '0'
dec rsi
mov [rsi], dl
test eax, eax
jnz convert
mov rdx, buffer + 19
sub rdx, rsi
mov eax, 1
mov edi, 1
syscall
ret
To embed this project on your website, copy the following code and paste it into your website's HTML: