Addition
Create a assembly file namedoperations_add.asm
.
global _start ; must be declared for linker
section .data
first_prompt db "Enter the first number " ; first_prompt="Enter the first number "
len_first_prompt equ $ - first_prompt ; len_first_prompt equals size of first_prompt
second_prompt db "Enter the second number " ; second_prompt="Enter the second number "
len_second_prompt equ $ - second_prompt ; len_second_prompt equals size of second_prompt
disp_prompt db "The Sum is " ; disp_prompt="The Sum is "
len_disp_prompt equ $ - disp_prompt ; len_disp_prompt equals size of disp_prompt
section .bss
first resb 2 ; Unitialized data variable first
second resb 2 ; Unitialized data variable second
sum resb 1 ; Unitialized data variable sum
section .text
_start: ; start label
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, first_prompt ; ecx=first_prompt
mov edx, len_first_prompt ; edx=len_first_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, first ; Read first input value
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, second_prompt ; ecx=second_prompt
mov edx, len_second_prompt ; edx=len_second_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, second ; Read second input value
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, disp_prompt ; ecx=disp_prompt
mov edx, len_disp_prompt ; edx=len_disp_prompt
int 0x80 ; Perform the system call
mov eax, [first] ; Moving first value to accumulator
sub eax, '0' ; Converting to ASCII value
mov ebx, [second] ; Moving second value to ebx
sub ebx, '0' ; Converting to ASCII value
add eax, ebx ; Adding the numbers in registers
add eax, '0' ; Converting to ASCII value
mov [sum], eax ; Storing the result in sum
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, sum ; ecx=sum
mov edx, 1
int 0x80 ; Perform the system call
mov eax , 1 ; sys_exit system call
mov ebx , 0 ; setting exit status
int 0x80 ; Calling interrupt handler to exit program
>> nasm -f elf32 operations_add.asm -o operations_add.o
>> ld -m elf_i386 operations_add.o -o operations_add
>> ./operations_add
>> echo $?
>> Enter the first number 2
>> Enter the second number 3
>> The Sum is 5
>> 0
Subtraction
Create a assembly file namedoperations_diff.asm
.
global _start ; must be declared for linker
section .data
first_prompt db "Enter the first number " ; first_prompt="Enter the first number "
len_first_prompt equ $ - first_prompt ; len_first_prompt equals size of first_prompt
second_prompt db "Enter the second number " ; second_prompt="Enter the second number "
len_second_prompt equ $ - second_prompt ; len_second_prompt equals size of second_prompt
disp_prompt db "The Difference is " ; disp_prompt="The Difference is "
len_disp_prompt equ $ - disp_prompt ; len_disp_prompt equals size of disp_prompt
section .bss
first resb 2 ; Unitialized data variable first
second resb 2 ; Unitialized data variable second
diff resb 1 ; Unitialized data variable diff
section .text
_start: ; start label
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, first_prompt ; ecx=first_prompt
mov edx, len_first_prompt ; edx=len_first_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, first ; Read first input value
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, second_prompt ; ecx=second_prompt
mov edx, len_second_prompt ; edx=len_second_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, second ; Read first input value
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, disp_prompt ; ecx=disp_prompt
mov edx, len_disp_prompt ; edx=len_disp_prompt
int 0x80 ; Perform the system call
mov eax, [first] ; Moving first value to accumulator
sub eax, '0' ; Converting to ASCII value
mov ebx, [second] ; Moving second value to ebx
sub ebx, '0' ; Converting to ASCII value
sub eax, ebx ; Subtracting the numbers in registers
add eax, '0' ; Converting to ASCII value
mov [diff], eax ; Storing the result in diff
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, diff ; ecx=diff
mov edx, 1
int 0x80 ; Perform the system call
mov eax , 1 ; sys_exit system call
mov ebx , 0 ; setting exit status
int 0x80 ; Calling interrupt handler to exit program
>> nasm -f elf32 operations_diff.asm -o operations_diff.o
>> ld -m elf_i386 operations_diff.o -o operations_diff
>> ./operations_diff
>> echo $?
>> Enter the first number 8
>> Enter the second number 3
>> The Difference is 5
>> 0
Multiplication
Create a assembly file namedoperations_prod.asm
.
global _start ; must be declared for linker
section .data
first_prompt db "Enter the first number " ; first_prompt="Enter the first number "
len_first_prompt equ $ - first_prompt ; len_first_prompt equals size of first_prompt
second_prompt db "Enter the second number " ; second_prompt="Enter the second number "
len_second_prompt equ $ - second_prompt ; len_second_prompt equals size of second_prompt
disp_prompt db "The Product is " ; disp_prompt="The Product is "
len_disp_prompt equ $ - disp_prompt ; len_disp_prompt equals size of disp_prompt
section .bss
first resb 2 ; Unitialized data variable first
second resb 2 ; Unitialized data variable second
prod resb 1 ; Unitialized data variable prod
section .text
_start: ; start label
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, first_prompt ; ecx=first_prompt
mov edx, len_first_prompt ; edx=len_first_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, first ; Read first input value
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, second_prompt ; ecx=second_prompt
mov edx, len_second_prompt ; edx=len_second_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, second ; Read first input value
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, disp_prompt ; ecx=disp_prompt
mov edx, len_disp_prompt ; edx=len_disp_prompt
int 0x80 ; Perform the system call
mov eax, [first] ; Moving first value to accumulator
sub eax, '0' ; Converting to ASCII value
mov ebx, [second] ; Moving second value to ebx
sub ebx, '0' ; Converting to ASCII value
mul ebx ; Multiplying the numbers in registers
add eax, '0' ; Converting to ASCII value
mov [prod], eax ; Storing the result in prod
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, prod ; ecx=prod
mov edx, 1
int 0x80 ; Perform the system call
mov eax , 1 ; sys_exit system call
mov ebx , 0 ; setting exit status
int 0x80 ; Calling interrupt handler to exit program
>> nasm -f elf32 operations_prod.asm -o operations_prod.o
>> ld -m elf_i386 operations_prod.o -o operations_prod
>> ./operations_prod
>> echo $?
>> Enter the first number 2
>> Enter the second number 3
>> The Product is 6
>> 0
Division
Create a assembly file namedoperations_div.asm
.
global _start ; must be declared for linker
section .data
first_prompt db "Enter the first number " ; first_prompt="Enter the first number "
len_first_prompt equ $ - first_prompt ; len_first_prompt equals size of first_prompt
second_prompt db "Enter the second number " ; second_prompt="Enter the second number "
len_second_prompt equ $ - second_prompt ; len_second_prompt equals size of second_prompt
disp_prompt db "The Division is " ; disp_prompt="The Division is "
len_disp_prompt equ $ - disp_prompt ; len_disp_prompt equals size of disp_prompt
section .bss
first resb 2 ; Unitialized data variable first
second resb 2 ; Unitialized data variable second
divs resb 1 ; Unitialized data variable divs
section .text
_start: ; start label
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, first_prompt ; ecx=first_prompt
mov edx, len_first_prompt ; edx=len_first_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, first ; Read first input value in num
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, second_prompt ; ecx=second_prompt
mov edx, len_second_prompt ; edx=len_second_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, second ; Read first input value in num
mov edx, 2 ; 2 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, disp_prompt ; ecx=disp_prompt
mov edx, len_disp_prompt ; edx=len_disp_prompt
int 0x80 ; Perform the system call
mov eax, [first] ; Moving first value to accumulator
sub eax, '0' ; Converting to ASCII value
mov ebx, [second] ; Moving second value to ebx
sub ebx, '0' ; Converting to ASCII value
div ebx ; Dividing the numbers in registers
add eax, '0' ; Converting to ASCII value
mov [divs], eax ; Storing the result in prod
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, divs ; ecx=divs
mov edx, 1
int 0x80 ; Perform the system call
mov eax , 1 ; sys_exit system call
mov ebx , 0 ; setting exit status
int 0x80 ; Calling interrupt handler to exit program
>> nasm -f elf32 operations_prod.asm -o operations_prod.o
>> ld -m elf_i386 operations_prod.o -o operations_prod
>> ./operations_prod
>> echo $?
>> Enter the first number 2
>> Enter the second number 3
>> The Product is 6
>> 0