dosseg
.model small

.data
    greating_msg db "Welcome in 8086-Calculator type simple equations to solve",10,"   Examples : 1+1 or 5*8",10,"$"
    error_msg db 10,"Exit due error$"
    division_reminder_msg db " and remainder=$"
    input db 3 DUP('0')

.code
    ; printing greating_msg
    mov dx,@data ;move offsets to data segment
    mov ds,dx
    
    mov ah,9
    mov dx,offset greating_msg
    int '!'         ; '!' = 21h
    
    mov bx,0
take_input:                        ; loop to take input and print dl for each letter
    mov ah,8        ; to input 1 character
    int '!'
    mov cl,al
    cmp bx,1        ; if it's the second character then it's opreation
    je check_cl_opreation
    call check_cl_number
    cmp al,1
    je assign
    jmp print_error_and_quit
check_cl_opreation:    ; to check if the input operation is a supported one
    cmp cl,'+'
    je assign
    cmp cl,'-'
    je assign
    cmp cl,'*'
    je assign
    cmp cl,'/'
    je assign
    jmp print_error_and_quit

assign:
    call print_cl
    mov input[bx],cl
    inc bx
    cmp bx,3
    jne take_input

calculate_input:
    mov cl,input[0]         ; First number
    mov bl,input[1]         ; the mark between the numbers
    mov ch,input[2]          ; Second number
    cmp bl,'+'
    je adding
    cmp bl,'-'
    je subtracting
    cmp bl,'*'
    je multiplying
    cmp bl,'/'
    je dividing
print_error_and_quit:
    mov ah,9
    mov dx,offset error_msg
    int '!'         ; '!' = 21h
    
quit: ; Quit the program
    mov ah,'L'  ; 'L' = 4Ch
    int '!'
    
adding:
    call print_equal_sign
    sub ch,'0'
    add cl,ch
    mov ch,'0' ; ch will be used as 10's number
    jmp check_and_print_cx
    
subtracting:
    call print_equal_sign
    sub ch,'0'
    sub cl,ch
    call check_cl_number
    cmp al,1
    je print_sub
    mov bh,cl
    mov cl,'-'
    call print_cl
    mov cl,':'     ; ':' = '9'+1
    sub bh,'0'    ; make bh contains exact number
    add bh,10   ; add 10 to get number in positive in next instruction
    sub cl,bh     ; remove bh from cl to get positive number in cl
print_sub:
    call print_cl
    jmp quit

multiplying:
    call print_equal_sign
    sub ch,'0'
    sub cl,'0'
    mov al,ch
    mul cl
    mov cl,al
    mov ch,ah
    add cl,'0'
    add ch,'0'
    call check_and_print_cx
    
check_and_print_cx:
    call check_cl_number
    cmp al,1
    je print_cx
    inc ch
    sub cl,10
    jmp check_and_print_cx
print_cx:
    cmp ch,'0'
    je print_units
    mov bh,cl ; using bh as temp
    mov cl,ch
    call print_cl
    mov cl,bh
print_units:
    call print_cl
    jmp quit

dividing:
    call print_equal_sign
    sub cl,'0'                      ; to use the real value of cl not '0'+value
    sub ch,'0'                     ; to use the real value of ch not '0'+value
    mov ah,0                    ; clear ah to use ax in division
    mov al,cl
    mov bl,ch
    div bl
    mov ch,ah
    mov cl,al
    add cl,'0'
    call print_cl
    mov ah,9
    cmp ch,0
    je quit_division
    mov dx,offset division_reminder_msg
    int '!'         ; '!' = 21h
    mov cl,ch
    add cl,'0'
    call print_cl
quit_division:
    jmp quit

print_equal_sign:
    mov ah,2
    mov dl,'='
    int '!'
    ret
    
print_cl:
    mov ah,2
    mov dl,cl
    int '!'
    ret

check_cl_number: ; check if the CL is a number if so return 1 in AL else return 0 in AL
    cmp cl,'0'
    jb not_number
    cmp cl,'9'
    ja not_number
    mov al,1
    ret
not_number:
    mov al,0
    ret
end
 
by

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret