Thursday, December 11, 2008

Another codes of Add, Subtract, Divide, Multiply in Assemble

.model small
.stack 256h



CR equ 13d
lf equ 10d
A equ 'A'
S equ 'S'
M equ 'M'
D equ 'D'

.data
addition db CR,LF,'[A]dd$'
subtraction db CR,LF,'[S]ub$'
multiplication db CR,LF,'[M]ul$'
divide db CR,LF,'[D]iv$'

msg db CR, LF, 'Enter your choice:$'

msg1 db CR, LF, 'Enter first number:$'
msg2 db CR, LF, 'Enter the second number:$'

msg3 db CR, LF, 'Enter first number:$'
msg4 db CR, LF, 'Enter the second number:$'


msg5 db CR, LF, 'Enter first number:$'
msg6 db CR, LF, 'Enter the second number:$'

msg7 db CR, LF, 'Enter first number:$'
msg8 db CR, LF, 'Enter the second number:$'

sum db CR, LF, 'The answer is:$'
diff db CR, LF, 'The answer is:$'
mult db CR, LF, 'The answer is:$'
dive db CR, LF, 'The answer is:$'

num1 dw ?
num2 dw ?

num3 dw ?
num4 dw ?


num5 dw ?
num6 dw ?

num7 dw ?
num8 dw ?


.code

start:

mov ax, @data
mov ds, ax

mov ax, offset addition
call put_string

mov ax, offset subtraction
call put_string
mov ax, offset subtraction
call put_string

mov ax, offset multiplication
call put_string

mov ax, offset divide
call put_string


mov ax, offset msg
call put_string

mov ah,1h
int 21h

mov bl,al

cmp bl,'A'
jne is_not_S

mov ax, offset msg1
call put_string
call get_num
mov num1, ax

mov ax, offset msg2
call put_string
call get_num

mov num2, ax

mov ax, offset sum
call put_string

mov ax, num1
add ax, num2
call put_num
jmp finish


is_not_S:

mov ax, offset msg3
call put_string
call get_num

mov num3, ax

mov ax, offset msg4
call put_string
call get_num

mov num4, ax

mov ax, offset diff
call put_string

mov ax, num3
sub ax, num4
call put_num

jmp finish

cmp bl,'M'


is_not_M:

mov ax, offset msg5
call put_string
call get_num

mov num5, ax

mov ax, offset msg6
call put_string
call get_num

mov num6, ax

mov ax, offset mult
call put_string

mov ax, num5
mul ax, num6
call put_num

jmp finish

is_not_D:


mov ax, offset msg7
call put_string
call get_num

mov num7, ax

mov ax, offset msg8
call put_string
call get_num

mov num8, ax

mov ax, offset dive
call put_string

mov ax, num7
div ax, num8
call put_num

jmp finish



finish:
mov ax, 4c00h
int 21h

put_string:
mov dx, ax
mov ah, 9h
int 21h
ret

get_char:
mov ah, 1h
int 21h
ret

put_char:
mov dl,al
mov ah, 2h
int 21h
ret

get_num:
push bx
push cx
push dx

mov dx, 1
mov bx, 0
mov cx, 0
call get_char
cmp al, ' '
jne newline
mov dx, -1
call get_char

newline:
push dx
cmp al, 13
je fin_read
sub al, '0'
mov bl, al
mov ax, 10
mul cx
mov cx, ax
add cx, bx
call get_char

read_loop:
cmp al, 13
je fin_read
sub al, '0'
mov bl, al
mov ax, 10
mul cx
mov cx, ax
add cx, bx
call get_char
jmp read_loop

fin_read:
mov ax, cx
pop dx
cmp dx, 1
je fin_getn
neg ax

fin_getn:
pop dx
pop cx
pop bx
ret

put_num:
push bx
push cx
push dx

mov dx, 0
push dx
mov cx,10
cmp ax, 0
jge calc_digits
neg ax
push ax
mov al, ' '
call put_char
pop ax

calc_digits:

div cx
add dx, '0'
push dx
mov dx, 0
cmp ax, 0
jne calc_digits

display_loop:
pop ax
cmp ax, 0
je end_display_loop
call put_char
jmp display_loop

end_display_loop:
pop dx
pop cx
pop bx
ret

end start

No comments: