0% found this document useful (0 votes)
31 views20 pages

MASM Program for Hex Addition

The document contains multiple assembly language programs written for the x86 architecture using DOS interrupts. The programs include functionalities for adding hexadecimal numbers, taking a character input, performing addition and subtraction of 16-bit numbers, and calculating the GCD and LCM of three numbers. Each program is accompanied by an analysis explaining its structure, operations, and the use of macros for improved readability.

Uploaded by

Utsav Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views20 pages

MASM Program for Hex Addition

The document contains multiple assembly language programs written for the x86 architecture using DOS interrupts. The programs include functionalities for adding hexadecimal numbers, taking a character input, performing addition and subtraction of 16-bit numbers, and calculating the GCD and LCM of three numbers. Each program is accompanied by an analysis explaining its structure, operations, and the use of macros for improved readability.

Uploaded by

Utsav Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write and test a masm program to add two Hexadecimal


Numbers.
CODE:

.model small

.stack 100h

.data

prompt1 db 13,10,"enter the 1st number: $"

prompt2 db 13,10,"enter the 2nd number: $"

prompt3 db 13,10,"the result of the addition is: $"

.code

main proc

mov ax,@data

;for moving data to data segment

mov ds,ax

xor bx,bx

;initially bx value is equal to 0

mov cl,4

lea dx, prompt1

;show num1 prompt

mov ah, 9

int 21h

mov ah,1

;for taking input

int 21h
input1:

cmp al,0dh

;compare whether the pressed key is 'enter' or not

je line1

;if it is equal to 'enter' then stop taking rst value

cmp al,39h

;compare the input whether it is letter or digit.39h is the ascii value of 9

jg letter1

and al,0fh

;if it is digit then convert it's ascii value to real value by masking

jmp shift1

letter1:

;if it is letter then subtract 37h from it to nd it's real value

sub al,37h

shift1:

shl bx, cl

or bl,al

;making 'or' will add the current value with previous value

int 21h

jmp input1

line1:

lea dx, prompt2

;show num2 prompt

mov ah, 9
fi
fi
int 21h

xor dx,dx

;set dx value zero

mov ah,1

int 21h

input2:

cmp al,0dh

;compare whether the pressed key is 'enter' or not

je line2

;if it is equal to 'enter' then stop taking rst value

cmp al,39h

;compare the input whether it is letter or digit.39h is the ascii value of 9

jg letter2

and al,0fh

;if it is digit then convert it's ascii value to real value by masking

jmp shift2

letter2:

;if it is letter then subtract 37h from it to nd it's real value

sub al,37h

shift2:

shl dx, cl

or dl,al
fi
fi
;making 'or' will add the current value with previous value

int 21h

jmp input2

line2:

xor cx,cx

mov cx,dx

mov dh,16

sum:

add bx,cx

;add two number which are stored in bx and cs register

jc pc1

;if the register is over owed then print an extra 1

mov cl, 4

lea dx, prompt3

;show answer prompt

mov ah, 9

int 21h

output:

;level for printing their sum

mov ch,bh

shr ch, cl

and ch,0fh
fl
cmp ch,10

;convert decimal to binary

add ch,'0'

cmp ch,':'

jl tag

add ch,7

tag:mov dl,ch

mov ah,2

int 21h

mov ch,bh

and ch,0fh

cmp ch,10

add ch,'0'

cmp ch,':'

jl tag1

add ch,7

tag1:mov dl,ch

mov ah,2

int 21h

mov ch,bl

shr ch, cl

and ch,0fh

cmp ch,10

add ch,'0'

cmp ch,':'

jl tag2

add ch,7

tag2:mov dl,ch
mov ah,2

int 21h

mov ch,bl

and ch,0fh

cmp ch,10

add ch,'0'

cmp ch,':'

jl tag3

add ch,7

tag3:mov dl,ch

mov ah,2

int 21h

jmp exit

pc1:

;level for printing over owed 1

mov dl,'1'

mov ah,2

int 21h

jmp output

exit:

mov ah, 4ch

;return control to dos

int 21h

main endp

end main

ANALYSIS:
fl
This assembly language program performs addition of two numbers input by the

user. It uses the prompt1 and prompt2 labels to display prompts for the first and second

numbers, respectively, and prompt3 to show the result of the addition. The program

reads each input character by character, converting ASCII values into binary by masking

or adjusting them accordingly. It handles both numeric digits and alphabetic characters,

allowing hexadecimal input, with logic for both digit-to-value conversion and letter-to-

value conversion. After storing the two input numbers in registers (with bx and dx holding

the values), it adds them. If the addition causes an overflow, it prints an additional ‘1’ to

signify the overflow. The program then converts the binary result to ASCII characters,

formatting it as a hexadecimal output. The sum is displayed using interrupt 21h, with

individual digits printed sequentially. Finally, control returns to DOS with interrupt 4Ch.

The structure of this program highlights typical assembly language techniques, including

register manipulation, bitwise operations, and handling of ASCII conversions.

2. Write and test a masm program to take a character from


keyboard and print it.

CODE:
.model small

.stack 100h

.data

msg1 db 10,13,"Enter a character: $"

msg2 db 10,13,"The character is: $"

.code

main proc

mov ax,@data

mov ds,ax
;display input prompt

lea dx,msg1

mov ah,09h

int 21h

;accept a character

mov ah,01h

int 21h

;al has the character

;display prompt

lea dx,msg2

mov ah,09h

int 21h

;display the character

mov dl,al

mov ah,02h

int 21h

mov ah,4ch

int 21h

main endp

end main

ANALYSIS:

This assembly language program is written for the x86 architecture using

DOS interrupt services. The program starts by setting up the data segment with

mov ax, @data and mov ds, ax to ensure access to defined data variables. It
then displays a message prompting the user to enter a character (msg1) by

loading the message address into dx, setting ah to 09h (DOS interrupt for

displaying a string), and calling interrupt int 21h. Next, it waits for the user to

input a character by setting ah to 01h and calling int 21h; the entered character is

stored in al. The program then displays another message (msg2), prompting the

output of the character. Finally, it moves the character from al to dl and displays it

using ah = 02h (DOS interrupt for displaying a single character) with int 21h. The

program ends by setting ah to 4Ch and calling int 21h to terminate and return

control to the operating system. This program demonstrates basic input/output

operations using DOS interrupts.

3. Write and test a program to add and subtract two 16 bit


numbers.

CODE:
.model small

.stack 300h

.data

msg1 db 0AH,0DH,'ENTER 1ST NUMBER: $'

msg2 db 0AH,0DH,'ENTER 2ND NUMBER: $'

msg3 db 0AH,0DH,'THE RESULT AFTER ADDITION IS: $'

msg4 db 0AH,0DH,'THE RESULT AFTER SUBTRACTION IS: $'

space db ' $'

endl db 0AH,0DH,'$'

val1 dw ?

val2 dw ?

.code

print macro msg


push ax

push dx

mov ah, 09h

lea dx, msg

int 21h

pop dx

pop ax

endm

main proc

mov ax,@data

mov ds,ax

start:

print msg1

call readnum

mov val1, ax

print msg2

call readnum

mov val2, ax

print msg3

mov ax, val1

mov bx, val2

add ax,bx

call writenum
print msg4

mov ax, val1

mov bx, val2

sub ax,bx

call writenum

exit:

mov ah, 4ch

int 21h

main endp

readnum proc near

; this procedure will take a number as input from user and store in AX

; input : none

; output : AX

push bx

push cx

mov cx,0ah

mov bx,00h

loopnum:

mov ah,01h

int 21h

cmp al,'0'

jb skip

cmp al,'9'

ja skip

sub al,'0'
push ax

mov ax,bx

mul cx

mov bx,ax

pop ax

mov ah,00h

add bx,ax

jmp loopnum

skip:

mov ax,bx

pop cx

pop bx

ret

readnum endp

writenum proc near

; this procedure will display a decimal number

; input : AX

; output : none

push ax

push bx

push cx

push dx

xor cx, cx

mov bx, 0ah

@output:

xor dx, dx
div bx ; divide AX by BX

push dx ; push remainder onto the STACK

inc cx

or ax, ax

jne @output

mov ah, 02h ; set output function

@display:

pop dx ; pop a value(remainder) from STACK to DX

or dl, 30h ; convert decimal to ascii code

int 21h

loop @display

pop dx

pop cx

pop bx

pop ax

ret

writenum endp

end main

ANALYSIS:
The Assembly program presented is designed to interactively receive numerical

input from a user, perform basic arithmetic operations (addition and subtraction), and

display the results. The program begins by setting the data segment to ensure all data

operations function correctly within the predefined memory space. It uses a macro called

`print` to streamline displaying prompts to the console, enhancing code maintainability

and readability. The core functionality revolves around two main procedures:

`getInputNumber` and `displayNumber`. `getInputNumber` is responsible for reading user


input character-by-character, converting these from ASCII to numerical values, and

constructing the actual numbers by considering the decimal place values (using

multiplication and addition). This method allows handling of multi-digit numbers

effectively. After obtaining two numbers from the user, the program computes their sum

and difference using simple register operations and then calls `displayNumber` to output

the results. `displayNumber` manages the conversion of numeric results back into their

ASCII equivalents for display, implementing a loop that divides the number to isolate

each digit, pushing these onto the stack to reverse their order, and then popping and

displaying them sequentially. This systematic approach to digit handling ensures that

numbers are displayed correctly from the most significant to the least significant digit.

The program concludes by cleaning up and returning control to the operating system,

demonstrating efficient use of system resources and good practice in handling user input

and output within a low-level programming context.

4. Write and test a program to identify the GCD and LCM of three
numbers.
CODE:
.model small
.stack 300h
.data
msg1 db 0AH,0DH,'Enter 3 numbers: $'
msg2 db 0AH,0DH,'GCD: $'
msg3 db 0AH,0DH,'LCM: $'
space db ' $'
endl db 0AH,0DH,'$'

val1 dw ?
val2 dw ?
val3 dw ?
num1 dw ?
num2 dw ?
num3 dw ?
.code
print macro msg
push ax
push dx
mov ah, 09h
lea dx, msg
int 21h
pop dx
pop ax
endm

main proc
mov ax,@data
mov ds,ax

start:

print msg1

call readnum
mov val1, ax

call readnum
mov val2, ax

call readnum
mov val3, ax

mov dx, 0000h

mov bx, val1


mov cx, val2
loopgcd:
mov ax, bx
mov dx, 0000h
div cx
cmp dx,0000h
jz ans
mov bx,cx
mov cx,dx
;mov ax,bx
;call writenum
;mov ax,cx
;call writenum
cmp cx, 0001h
jnz loopgcd
ans:
mov num1, cx
mov dx, 0000h

mov bx, val3

loopgcd1:
mov ax, bx
mov dx, 0000h
div cx
cmp dx, 0000h
jz ans1
mov bx, cx
mov cx, dx
cmp cx, 0001h
jnz loopgcd1

ans1:

print msg2
mov ax, cx
call writenum

mov ax, val1


mov bx, val2
mul bx
mov bx, num1
div bx

mov bx, val3


mul bx
div cx

print msg3
call writenum

exit:
mov ah, 4ch
int 21h

main endp

readnum proc near


; this procedure will take a number as input from user and
store in AX
; input : none

; output : AX

push bx
push cx
mov cx,0ah
mov bx,00h
loopnum:
mov ah,01h
int 21h
cmp al,'0'
jb skip
cmp al,'9'
ja skip
sub al,'0'
push ax
mov ax,bx
mul cx
mov bx,ax
pop ax
mov ah,00h
add bx,ax
jmp loopnum

skip:
mov ax,bx
pop cx
pop bx
ret
readnum endp

writenum proc near


; this procedure will display a decimal number
; input : AX
; output : none

push ax
push bx
push cx
push dx

xor cx, cx
mov bx, 0ah

@output:
xor dx, dx
div bx ; divide AX by BX
push dx ; push remainder onto the
STACK
inc cx
or ax, ax
jne @output

mov ah, 02h ; set output function

@display:
pop dx ; pop a value(remainder)
from STACK to DX
or dl, 30h ; convert decimal to
ascii code
int 21h
loop @display

pop dx
pop cx
pop bx
pop ax

ret
writenum endp

end main

ANALYSIS:
This assembly program for x86 architecture is designed to read
three numbers from the user, compute their Greatest Common
Divisor (GCD) and Least Common Multiple (LCM), and display the
results. The `.data` segment defines messages (`msg1`, `msg2`,
`msg3`), along with placeholders for storing user inputs and
intermediate results. The program uses DOS interrupt `int 21h` to
display messages and interact with the user. The `print` macro
simplifies message printing by encapsulating `int 21h` calls.

The main procedure begins by setting up the data segment and


displaying a prompt for input. The `readnum` procedure is called
thrice to input three numbers, storing each in `val1`, `val2`,
and `val3`. The program calculates the GCD of the first two
numbers through the `loopgcd` loop using the Euclidean algorithm,
storing the result in `num1`. Then, it computes the GCD of `num1`
with `val3` to get the GCD of all three values. The result is
displayed using the `writenum` procedure.

For the LCM, it multiplies `val1` and `val2`, divides by their


GCD, then multiplies by `val3` and divides by the overall GCD to
avoid overflow, giving the LCM. The `readnum` procedure converts
ASCII input into a numerical value, while the `writenum`
procedure converts a number in `AX` to ASCII for display. The
program exits cleanly by calling DOS interrupt `4Ch`. This
program effectively demonstrates looping, division-based
calculations, and DOS-based input/output.

You might also like