### Arthematic Function In Assembly Language

Hello Friends,

Welcome Again To My Blog. I hope You all are Enjoying Series of Assembly Tutorials Well, Actually Assembly Is Very Important Part To Keep Strong Grep Over Exploit writing. that's why I am trying To Polish my Assembly Programming Writing Skill. So, Today Again I am going To share Example Of Codes Where I tried To Demonstrate how To Use Various Previous Posted Subroutine To Perform Arthematic Functionality.

Basically, Today I am going to Demonstrate How To Perform Addition, Division, Subtraction And Multiplication. I Hope You All Will Learn Something New.

By The Way, If you Are new And Don't Know How To Start, Compile Or Run?
Then, Don't Follow up Our Previous Blog post. Here, it's The List.

1. Hello World In Assembly
2. Find String Length In Assembly Language
3. Subroutine Demo In Assembly language
4. Some Subroutine Functionality Example
5. User Input Example In Assembly Language
6. Variable Decleration/Include function In Assembly
7. Convert Integer Into ASCII in Assembly Language

### Mathematical Functions Example In Assembly Language

#### Arthematic Function (Arthematicfunction.asm)

; Arthematic Functions Examples
;
; author : suraj singh bisht
; Email : surajsinghbisht054@gmail.com
;
; includable functions
;
; ====================================================
;              [ Includable File]
; Functions:
;       _strlen
;       _exit
;       _print
;       _printline
;
; ====================================================
; ====================================================
;              [ Includable File]
; Functions:
;       _printsingle
;       _printdigit
; ====================================================

%include 'DigitPrinter_without_start.asm'

; section .bss
SECTION .bss

; section data
SECTION .data
; ------------------------------------------------------------------------------------------------
;  Variable | Size | newline | String   | Null Character |
; -----------------------------------------------------------------------------------------------
string1  db 0Ah, "[+] Your Subtraction Answer    : ",0h ; String Decleration
rem  db      " Answer And Remainder is : ",  0h

; section text
SECTION .text

_start:
; print first string
mov eax, string0 ; print string
call _print  ; call printline

mov eax, 50 ; Load NUMBER1 into eax
mov ebx, 5 ; Load NUMBER2 into eax
add eax, ebx  ; Perform Addition [EAX = EAX + EBX ]

; print second string
call _print  ; call print line

; perform subtraction
mov eax, 50 ; Load First Number
mov ebx, 5 ; Load Second Number
sub eax, ebx  ; Perform Subtraction [EAX = EAX - EBX]
call _printdigit

; Print third string

; perform multiplication
mov eax, 10 ; Load First Number
mov ebx, 5 ; Load Second Number
mul ebx   ; Perform Multiplication, First Argument is Default Set To = EAX,
; Hence, no need to pass first register, [EAX = EAX * EBX ]

; print string
call _print  ; call printline

; perform division
mov eax, 100 ; Load First Number
mov ebx, 9 ; Load Second Number
mov edx, 0 ; Make Sure, Edx = 0
div ebx   ; perform division, First Argument is Default Set To = EAX,
; Hence, No need to pass first register, [EDX, EAX = EAX / EBX]
; Here, EAX = Quotient & EDX = Remainder
push edx  ; Save Remainder Into Stack

call _printdigit ; print answer Quotient

; print remainer text
call _print  ;

; print remainder
call _printdigit ; call printline

; exit
jmp _exit  ; exit routine

#### Digit Printer Function (DigitPrinter_without_start.asm)

;    Digit printing concept
; author : Suraj Singh Bisht
; Email   : surajsinghbisht054@gmail.com
;
; includable functions
%include 'includable_functions_without_start.asm'

; ====================================================
;              [ Includable File]
; Functions:
;       _strlen
;       _exit
;       _print
;       _printline
;
; ====================================================
;
; ====================================================
;              [ Includable File]
; Functions:
; _printsingle
; _printdigit
;
;
; ====================================================
;
; Simple Concept to print multi digit numbers
;
; For Example :
; Dividend  = 1000 (Dividend)
; Divisor   = 10
;
; ( Quotient, Remainder ) = ( Dividend / Divisor )
;
;
; While (if Quotient is Not Equal To 0){
; print (Remainder);
;
; ( Quotient, Remainder ) = ( Quotient / Divisor )
;
; }
;
;
;
; Come On! Use Mind And Calculate Above Provided Algo.
;
;
__printreverse:
; input
;  eax = address of string
;
; output
; nothing
push ebx
push ecx
push edx

; store value into stack
push eax
call _strlen  ; Find String length
mov ecx, eax  ; ECX = Length Of String
pop ebx   ; EBX = Address of string

_printreverseiterate:
call _printsingle  ; print single number over terminal
cmp ecx, 0   ; Compare if ECX is zero
je _printreversereturn  ; Return
dec ecx
jmp _printreverseiterate ; Repeat

_printreversereturn:
pop edx
pop ecx
pop ebx
ret

; routine to print single digit
_printsingle:
; Input:
;       EAX = Value
; Output:
;       nothing
push ebx        ; store register into stack

mov ebx, 0      ; EBX = 0
push ebx        ; push ebx
push eax        ; push eax
mov eax, esp    ; EAX = ESP
call _print     ; call print

pop eax         ; pop Eax
pop ebx         ; pop EBX Null Value
pop ebx         ; pop eax

ret             ; return

; Digit To ASCII Digit Convertion Subroutine
_printdigit:
; Input:
;       EAX = Digit Number
; Output
;       EAX
;

; Save Values
push eax                ; Save Digit [Previous Arguments]
push ebx                ; Push Ebx To Stack [Previous Arguments]
push ecx
push edx
mov ebx, 10  ; Set EBX = 10
mov ecx, 0  ;

; division loop
_divisionloop:
xor edx, edx  ; EDX = 0
div ebx                 ; Perform Division Between EAX, EBX [EDX, EAX = EAX / EBX ]
; EAX = Quotient And EDX = Remainder

push eax                ; Push EAX Into Stack [Division Answer]
mov eax, edx            ; EAX = EDX [EAX = Remainder]

add eax, 48             ; ASCII code Difference Is 48
mov [digitprintersave+ecx], eax  ;
;call _printsingle       ; Call printsingle routine
inc ecx;
pop eax                 ; Get EAX
cmp eax, 0              ; Compare
je _comparisionsection  ; To Avoid Print
jmp _divisionloop ; Repeat

_comparisionsection:
mov eax, digitprintersave  ;
call __printreverse
; Pop Values
pop edx
pop ecx
pop ebx                 ; Pop Digit [Previous Arguments]
pop eax                 ; Pop Digit[Previous Arguments]

ret             ; Return