Google

Dec 7, 2007

Assembly Language Programs for Addition and Subtraction

Program for adding and subtracting numbers on microprocessor

for more detailed way i have broken down the problem into 4 sub problems they are

a) 32-bit addition for signed and unsigned numbers
b) 32-bit subtraction for signed and unsigned numbers
c) Adding two ASCII and BCD numbers
d) Subtracting two ASCII and BCD numbers

now let me write programs to each one of them::

1)
To write 8086 Assembly Language Program to add two 32-bit signed & unsigned number.

code:

MOV AX,5000H ; Initialize DATA SEGMENT
MOV DS,AX ; to 5000H
MOV AX,[1000H] ; take lower 16-bit of NUM1 in AX
MOV BX,[2000H] ; take lower 16-bit of NUM2 in BX
ADD AX,BX ; AX = AX + BX
MOV [3000H],AX ; Store lower 16-bit result at NUM3
MOV AX,[1002H] ; take higher 16-bit of NUM1 in AX
MOV BX,[2002H] ; take higher 16-bit of NUM2 in BX
ADC AX,BX ; AX = AX + BX + CF (add with carry)
MOV [3002H],AX ; Store higher 16-bit result at NUM3
HLT ; Halt 8086

i have given these inputs
NUM1 Unsigned Signed
[5000:1000] = 78H (LSB) = 88 H (LSB)
[5000:1001] = 56H = A9 H
[5000:1002] = 34H = CB H
[5000:1003] = 12H (MSB) =ED H (MSB)

NUM2
[5000:2000] = 34H (LSB) = CC H (LSB)
[5000:2001] = 12H = ED H
[5000:2002] = 34H = CB H
[5000:2003] = 12H (MSB) = ED H (MSB)


and the output is this
NUM3
[5000:3000] = ACH (LSB) = 54 H (LSB)
[5000:3001] = 68H = 97 H
[5000:3002] = 68H = 97 H
[5000:3003] = 24H (MSB) = DB H (MSB)

2)
To write 8086 Assembly Language Program to Subtract two 32-bit signed & unsigned number

MOV AX,5000H ;Initialize DATA SEGMENT
MOV DS,AX ;to 5000H
MOV AX,[1000H] ;take lower 16-bit of NUM1 in AX
MOV BX,[2000H] ;take lower 16-bit of NUM2 in BX
SUB AX,BX ;AX = AX - BX
MOV [3000H],AX ;Store lower 16-bit result at NUM3
MOV AX,[1002H] ;take higher 16-bit of NUM1 in AX
MOV BX,[2002H] ;take higher 16-bit of NUM2 in BX
SBB AX,BX ;AX = AX - BX - CF (subtract with barrow)
MOV [3002H],AX ;Store higher 16-bit result at NUM3
HLT ; Halt 8086

Input:
NUM1 Unsigned Signed
[5000:1000] = 78H (LSB) = 88 H (LSB)
[5000:1001] = 56H = A9 H
[5000:1002] = 78H = 87 H
[5000:1003] = 56H (MSB) =A9 H (MSB)

NUM2
[5000:2000] = 34H (LSB) = CC H (LSB)
[5000:2001] = 12H = ED H
[5000:2002] = 34H = CB H
[5000:2003] = 12H (MSB) = ED H (MSB)

Output:
NUM3
[5000:3000] = 44H (LSB) = BCH (LSB)
[5000:3001] = 44H = BB H
[5000:3002] = 44H = BB H
[5000:3003] = 44H (MSB) = BB H (MSB)


3)
To write 8086 Assembly Language Program to Add two ASCII & BCD number.

;ALP to add two ASCII numbers
MOV AX,5000H ;Initialize DATA SEGMENT
MOV DS,AX ;to 5000H
MOV AX,0000H ;Clear AX register
MOV AL,[1000H] ;take first ASCII number NUM1 in AL
MOV BL,[2000H] ; take second ASCII number NUM2 in BL
ADD AL,BL ;AL = AL + BL
AAA ; ASCII Adjust After Addition
OR AX,3030H ; logical OR with contents of AX & 3030H
MOV [3000H],AX ;Store ASCII result at NUM3
HLT ; Halt 8086

;ALP to add two BCD numbers
MOV AX,5000H ;Initialize DATA SEGMENT
MOV DS,AX ;to 5000H
MOV AX,0000H ;Clear AX register
MOV AL,[1000H] ;take first BCD number NUM1 in AL
MOV BL,[2000H] ; take second BCD number NUM2 in BL
ADD AL,BL ;AL = AL + BL
DAA ; Decimal Adjust After Addition
MOV [3000H],AL ;Store BCD result at NUM3
HLT ; Halt 8086

Input:
NUM1 ASCII BCD NUM2 ASCII BCD
[5000:1000] = 39H = 45H [5000:2000] = 39H = 49H

Output:
NUM3 ASCII BCD
[5000:3000] = 38H (LSB) = 94H
[5000:3001] = 31H (MSB) = 00H



4) To write 8086 Assembly Language Program to Subtract two ASCII & BCD number.


;ALP to subtract two ASCII numbers

MOV AX,5000H ;Initialize DATA SEGMENT
MOV DS,AX ;to 5000H
MOV AX,0000H ;Clear AX register
MOV AL,[1000H] ;take first ASCII number NUM1 in AL
MOV BL,[2000H] ; take second ASCII number NUM2 in BL
SUB AL,BL ;AL = AL - BL
AAS ; ASCII Adjust After Subtraction
OR AX,3030H ; logical OR with contents of AX & 3030H
MOV [3000H],AX ;Store ASCII result at NUM3
HLT ; Halt 8086

;ALP to subtract two BCD numbers

MOV AX,5000H ;Initialize DATA SEGMENT
MOV DS,AX ;to 5000H
MOV AX,0000H ;Clear AX register
MOV AL,[1000H] ;take first BCD number NUM1 in AL
MOV BL,[2000H] ; take second BCD number NUM2 in BL
SUB AL,BL ;AL = AL - BL
DAS ; Decimal Adjust After Subtraction
MOV [3000H],AL ;Store BCD result at NUM3
HLT ; Halt 8086

Input:
NUM1 ASCII BCD NUM2 ASCII BCD
[5000:1000] = 39H = 99H [5000:2000] = 34H = 49H

Output:
NUM3 ASCII BCD
[5000:3000] = 35H (LSB) = 40H
[5000:3001] = 30H (MSB) = 00H


You might be also interested in:

Assembly program for strings
:: Assembly Language Programs for Multiplication and Division
:: Traffic light control system using 8086

9 comments:

Nitin said...

sir please tell me .How to write the the addition program for two 128 bit BCD number???

bharatsir said...

@nitin:
split the two 128 bit numbers into bytes... you will get 16 sets of two one byte numbers.
first add the lowest bytes uing ADD instruction. follow it up with DAA. remember the resuklt of your addition must be in AL registr.
store this as the lowest 8 bits of your final resukt.
repeat the above process for all subsequent higher bytes but use ADC instruction to add them and follow up each addition by a DAA instruction.
after 16 such 8-bit steps you will get your final 128-bit answer. check for the final carry.

Thoufeeq said...

Sir, I want to learn 8086 assembly language programming. I am looking for a guide or pdf that teaches the same. Pls tell me where I can get one.

JISSO said...

Write an ALP to find the maximum positive number from a given array of signed 8-bit numbers stored at location ARRAY1. The result must be stored at location MAX_P. Use the small memory model for this program.

JISSO said...

Write an ALP to find the maximum positive number from a given array of signed 8-bit numbers stored at location ARRAY1. The result must be stored at location MAX_P. Use the small memory model for this program.

JISSO said...

Write an ALP to find the maximum positive number from a given array of signed 8-bit numbers stored at location ARRAY1. The result must be stored at location MAX_P. Use the small memory model for this program.

Vickie said...

This was really helpful.... thanks a lot sir.......

nancy said...

hello sir.. nancy here.. sir i want to knw thet why we have done the "OR" part in addition of two ascii no..???

deepthi said...

sir please tell me how to write addition of two 8 bit numbers with carry with out using ADC