I reckoned that, as a guy who works in IT, I ought to know the languages of yesteryear, and the languages that the machines actually interpret on a low level, before I call mysef a programmer.
So, I figured I'd brush up on the basics of Assembly! This, my dear friends, is a basic calculator, currently, a WIP (still implementing int-to-string conversion and print loop), capable of addition and subtraction.
I will be adding additional functions on a regular basis. As I'm not a fan of Github, and prefer to do version control by simply keeping my various save-revisions in a "versions" folder, I figured - Neocities is as good a place as any to post this code!
Please do keep in mind - this is nothing complex, and merely a rudimentary program; I'm posting it here in case it is of interest to anyone, and to show as a portfolio project - accessible easily through a link. Cheers!
section .data
menu_message db “1 - Add Num1 and Num2, 2 - Subtract Num2 from Num1, 3 - Exit”, 0xA ; message + newline
result_message db "Result: "
menu_length equ $ - menu_message ; menu string length
result_length equ $ - result_message ; result string length
section .bss
input resb 10
selection dd 0
num1 dd 0
num2 dd 0
result resd 1
int_to_str_buffer resb 16
int_word_len resb 1
section .text
global _start
_start:
mov eax, 3 ; syscall number for read
mov ebx, 0 ; file descriptor 0 = stdin
mov ecx, input ; buffer address
mov edx, 10 ; number of bytes to read
int 0x80 ; perform syscall
; input Number 1 for calculation
; INPUT HERE
; end of input block
mov al, [input]
and al, 0x0F
mov [num1], al
mov eax, 3 ; syscall number for read
mov ebx, 0 ; file descriptor 0 = stdin
mov ecx, input ; buffer address
mov edx, 10 ; number of bytes to read
int 0x80 ; perform syscall
; input Number 2 for calculation
; INPUT HERE
; end of input block
mov al, [input]
and al, 0x0F
mov [num2], al
jmp menu
menu:
mov eax, 4 ; syscall: write
mov ebx, 1 ; file descriptor: stdout
mov ecx, menu_message ; pointer to message
mov edx, menu_length ; message length
int 0x80 ; print Menu
mov eax, 3 ; syscall number for read
mov ebx, 0 ; file descriptor 0 = stdin
mov ecx, input ; buffer address
mov edx, 10 ; number of bytes to read
int 0x80 ; perform syscall
;input Menu Selection
mov al, [input]
and al, 0x0F
mov [selection], al
mov eax, [selection]
cmp eax, 1
je addition
cmp eax, 2
je subtraction
cmp eax, 3
je exit
addition:
mov eax, [num1]
mov ebx, [num2]
add eax, ebx
mov [result], eax
call print_result
jmp menu
subtraction:
mov eax, [num1]
mov ebx, [num2]
sub eax, ebx
mov [result], eax
call print_result
jmp menu
print_result:
mov eax, 4 ; syscall: write
mov ebx, 1 ; file descriptor: stdout
mov ecx, result_message ; pointer to message
mov edx, result_length ; message length
int 0x80 ; print Menu
mov eax, [result]
mov edi, int_to_str_buffer
mov ecx, 0
ret
convert_loop:
exit:
xor eax, eax
mov ebx, 0 ;set exit code to 0
mov eax, 1
int 0x80