Thursday, February 10, 2011

Add-Substract




%OUT-----------------------------------------------------------------------------------------
%OUT- Accept two 8 digit hexadecimal numbers, add and subtract them
%out- input: Two 8 digit hexadecimal numbers
%out- output: Print the sum and the difference of the two numbers
%out – author: Ayon and Ariyam
%OUT-----------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack
.data

count equ 4 ;8 digit hexadecimal number will contain 4 bytes
operand1 db count dup(?) ;reserving bytes for the first operand, uninitialized
operand2 db count dup(?) ;reserving bytes for the second operand, uninitialized

ip1 db "Enter 1st 8-digit hexadecimal number: ","$"
;a request to prompt the user for entering 1st number

ip2 db "Enter 2nd 8-digit hexadecimal number: ","$"
;a request to prompt the user for entering 2nd number

op1 db "ADDITION: ","$" ;output message
op2 db "SUBTRACTION: ","$" ;output message
op3 db "SUBTRACTION: - ","$" ;output message
heading db "***** 8-digit HEX NUMBER ADDITION/SUBTRACTION *****","$"
;a heading

.code

main proc
clrscrn ;clearing the screen
mov dx,0105h
cursor ;setting up the cursor
showmsg heading ;displaying heading

mov dx,0303h ;pointing to next line
cursor ;setting up the cursor
showmsg ip1 ;prompt for 1st number from user
mov cx,count ;accepting 1st number and storing it in operand1 array
mov si,00h


go1: input ;byte stored in AL
mov operand1[si],al
inc si
loop go1

mov dx,0503h ;pointing to next line
cursor ;setting up the cursor
showmsg ip2 ;prompt for 2nd number from user
mov cx,count ;accepting 2nd number and storing it in operand2 array
mov si,00h

go2: input ;byte stored in AL
mov operand2[si],al
inc si
loop go2


; =============== Begin ADDITION ===========

mov dx,0703h ;pointing to next line
cursor ;setting up the cursor
showmsg op1 ;printing output msg for sum

mov di,04h ;since number of bytes in each operand is 4
xor bh,bh
;clearing BH, BH will store carry because last comparison at the end of the loop will effect carry ;flag

; ------- 32 bit addition achieved by 16-bit addition twice ----------
again: ror bh,1 ;carry loaded accordingly from rotation of BH
dec di
mov al,operand1[di]
mov bl,operand2[di]
dec di
mov ah,operand1[di]
mov bh,operand2[di]
adc ax,bx ;add with carry
mov bh,00h
adc bh,00h ;BH will store final carry
push ax
cmp di,00h
jne again


;-------- printing result (33bits) ------

mov dl,bh
add dl,30h
mov ah,02h
int 21h ;displaying MSD (final carry)

pop dx
mov cx,dx
mov bh,ch
output ;displaying 2nd & 3rd Digits from left
mov bh,cl
output ;displaying 4th & 5th Digits from left
pop dx
mov cx,dx
mov bh,ch
output ;displaying 6th & 7th Digits from left
mov bh,cl
output ;displaying 8th & 9th Digits from left


; ============ Begin SUBTRACTION ===============
mov dx,0903h ;pointing to next line
cursor ;setting up the cursor

; -------- find which operand is greater & act accordingly ---------
mov cx,04h
mov si,00h
continue: mov ah,operand1[si]
cmp ah,operand2[si]
jb swap
ja noswap
inc si
loop continue
jmp noswap

; following will swap the two operands, operand1 and operand2 when operand1=operand2

subtract: mov di,04h
;since number of bytes in each operand is 4
xor bh,bh
;clearing BH, BH will store borrow because last comparison
;at the end of the loop will effect carry flag

; ------- 32 bit subtraction achieved by 16-bit subtraction twice ----------
iter: ror bh,1
;borrow loaded accordingly from rotation of BH in the flag register
dec di
mov al,operand1[di]
mov bl,operand2[di]
dec di
mov ah,operand1[di]
mov bh,operand2[di]
sbb ax,bx
mov bh,00h
adc bh,00h ;BH will store borrow
push ax
cmp di,00h
jne iter

;-------- printing result (32bits) ------

pop dx
mov cx,dx
mov bh,ch
output ;displaying 1st & 2nd Digits from left
mov bh,cl
output ;displaying 3rd & 4th Digits from left
pop dx
mov cx,dx
mov bh,ch
output ;displaying 5th & 6th Digits from left
mov bh,cl
output ;displaying 7th & 7th Digits from left

exit
main endp
end main

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.