Wednesday, March 28, 2012

Multiplcation by Successive addition assembly X86

; Multiplcation by Successive addition
;----------------------------------------------------------------------------

.model small
.data
display_message macro msg
mov dx,offset msg
mov ah,09h
int 21h
endm

num1 db 00h
num2 db 00h
msg1 db 10,13,"Enter first number $"
msg2 db 10,13,"Enter second number $"
msg3 db 10,13,"Multiplication is $"
msg4 db 10,13,"$"
result dw 0000h
.code
mov ax,@data
mov ds,ax

display_message msg1

mov ah,01h
int 21h
cmp al,39h
jbe dnt_sub
sub al,07h
dnt_sub: sub al,30h
mov cl,04h
shl al,cl
mov bl,al

mov ah,01h
int 21h
cmp al,39h
jbe dnt_sub1
sub al,07h
dnt_sub1: sub al,30h
add bl,al
mov num1,bl ; mov number1 into num1

display_message msg2

mov ah,01h
int 21h
cmp al,39h
jbe dnt_sub2
sub al,07h
dnt_sub2: sub al,30h
mov cl,04h
shl al,cl
mov bl,al

mov ah,01h
int 21h
cmp al,39h
jbe dnt_sub3
sub al,07h
dnt_sub3: sub al,30h
add bl,al

mov num2,bl ; mov number2 in num2

mov ah,00h
mov al,num1

add_again: add result,ax ; add num1 in result for num2 times
dec num2
jnz add_again

mov ch,04h
mov cl,04h
mov bx,result

display_message msg3

loop1: rol bx,cl ; display final result
mov result,bx
and bx,000fh
mov dl,bl
cmp dl,09h
jbe dnt_add
add dl,07h
dnt_add: add dl,30h
mov ah,02h
int 21h
dec ch
mov bx,result
jnz loop1


mov ah,4ch
int 21h
end

;------------------------------------------------------------------------------------------
; done by Prof. Dhumane A.V. VIIT, Pune

Tuesday, March 27, 2012

compare two strings assembly X86

; compare two strings
;--------------------------------------------------------------------------

.model small

.data

message_display macro msg
mov dx,offset msg
mov ah,09h
int 21h
endm

msg1 db 10,13, "Enter first string $"
msg2 db 10,13, "Enter Second string $"
msg3 db 10,13, "$"
msg4 db 10,13, "Strings are not equal $"
msg5 db 10,13, "Strings are equal $"

string1 db 10h dup(0)
string2 db 10h dup(0)

len1 db 00h
len2 db 00h


.code
mov ax,@data
mov ds,ax

lea si,string1

nxt_char: mov ah,01h
int 21h
mov [si],al
inc si
inc len1
cmp al,0dh
jnz nxt_char

mov al,24h
mov [si],al

message_display msg3
message_display string1
message_display msg3

lea si,string2

nxt_char1: mov ah,01h
int 21h
mov [si],al
inc si
inc len2
cmp al,0dh
jnz nxt_char1

mov al,24h
mov [si],al

message_display msg3
message_display string2

mov al,len1
mov ah,len2

cmp al,ah
jnz not_equal

mov ax,@data
mov ds,ax
mov es,ax

mov bh,00
mov bl,len1

lea si,string1
lea di,string2

mov cx,bx
repz cmpsb

cmp cx,0000h
jnz not_equal

message_display msg5
jmp exit


not_equal:
message_display msg4

exit:
mov ah,4ch
int 21h
end

concatenation of strings in assembly X86

; concatenation of strings
;---------------------------------------------------------------------------

.model small

.data

message_display macro msg
mov dx,offset msg
mov ah,09h
int 21h
endm

msg1 db 10,13, "Enter the string $"
msg2 db 10,13, "Concatenated string is $"
msg3 db 10,13, "$"

string1 db 10h dup(0)
string2 db 10h dup(0)
string3 db 10h dup(0)
len1 db 00h
len2 db 00h

.code
mov ax,@data
mov ds,ax

message_display msg1

lea si,string1

next_char: mov ah,01h
int 21h
cmp al,0dh
jz l1

mov [si],al
inc si
inc len1
jmp next_char

l1: mov al,24h
mov [si],al

message_display msg3
message_display string1

message_display msg1

lea si,string2

next_char1: mov ah,01h
int 21h
cmp al,0dh
jz l2
mov [si],al
inc si
inc len2
jmp next_char1

l2: mov al,24h
mov [si],al

message_display msg3
message_display string2

message_display msg2
message_display msg3

mov bh,00h
mov bl,len1
mov ax,@data
mov ds,ax
mov es,ax

lea si,string1
lea di,string3

mov cx,bx
rep movsb

mov bh,00h
mov bl,len2
mov ax,@data
mov ds,ax
mov es,ax

lea si,string2

mov cx,bx
rep movsb

inc di
mov al,24h
mov [di],al

message_display string3

mov ah,4ch
int 21h
end

Monday, March 26, 2012

print the string in reverse manner using assembly X86

; print the string in reverse manner
;----------------------------------------------------------------------------
.model small

.data
message_display macro msg
mov dx,offset msg
mov ah,09h
int 21h
endm

msg1 db 10,13, "Enter the string $"
msg2 db 10,13, "Reversed string is $"
msg3 db 10,13, "$"
string db 10h dup(0)
len db 00h

.code
mov ax,@data
mov ds,ax

message_display msg1

lea si,string

next_char: mov ah,01h
int 21h
mov [si],al
inc si
inc len
cmp al,0dh
jnz next_char

mov al,24h
mov [si],al
inc len

message_display msg2
message_display msg3
mov bh,len

loop1: mov dl,[si]
mov ah,02h
int 21h
dec si
dec bh
jnz loop1

mov ah,4ch
int 21h
end

;------------------------------------------------------------------------------------
; done by prof. Dhumane A.V. VIIT, Pune

to print the string length in assembly X86

; to print the string length
;----------------------------------------------------------------------------
.model small

.data
message_display macro msg
mov dx,offset msg
mov ah,09h
int 21h
endm

msg1 db 10,13, "Enter the String $"
msg2 db 10,13, "String is$"
msg3 db 10,13, "$"
msg4 db 10,13, "String Length is$"
string db 10h dup(0)

.code
mov ax,@data
mov ds,ax

message_display msg1

lea si, string
mov bh,00h

accept_nxt_char:
mov ah,01h
int 21h

mov [si],al
inc si
inc bh
cmp al,0dh
jnz accept_nxt_char

mov al,24h
mov [si],al

message_display msg2
message_display msg3
message_display string
message_display msg4
message_display msg3

dec bh
mov cl,04
mov bl,bh
mov dh,02h
shr bl,cl

print: and bl,0fh
cmp bl,09h
jbe dnt_add
add bl,07h
dnt_add: add bl,30h
mov dl,bl
mov ah,02h
int 21h
dec dh
mov bl,bh
jnz print

mov ah,4ch
int 21h

end

;-----------------------------------------------------------------------------------------
; done by prof. Dhumane A.V. VIIT, Pune

accept and display string in assembly X86

; accept and display string
;---------------------------------------------------------------------------
.model small

.data

display_message macro msg
mov dx,offset msg
mov ah,09h
int 21h
endm

msg1 db 10,13,"Enter the String $"
msg2 db 10,13,"String is $"
msg3 db 10,13,"$"
array db 10h dup(0)

.code
mov ax,@data ; intialization of data segment rgistr
mov ds,ax

display_message msg1

lea si,array


accept_next_char:
mov ah,01h
int 21h
mov [si],al
inc si
cmp al,0dh ; 0dh is enter character
jnz accept_next_char

mov al,24h ; 24h is '$' character
mov [si],al

display_message msg2
display_message msg3
display_message array

mov ah,4ch ; proram termination
int 21h
end

;-----------------------------------------------------------------------------------------
; done by prof. Dhumane A.V. VIIT, Pune
; Program of adding the elements from the array
;---------------------------------------------------------------------------

.model small

.data
message_disp macro msg ; macro for message display
mov dx,offset msg
mov ah,09h
int 21h
endm

msg1 db 10,13, "Array Addition's Program $"
msg2 db 10,13, "How many elements you want to add ? $"
msg3 db 10,13, "Enter Number $"
msg4 db 10,13, "Addition is $"

no_of_elements db ?
array db 0Ah dup(0)
result db 0
fd db 0

.code

mov ax,@data
mov ds,ax

message_disp msg1 ; display message 1

message_disp msg2 ; display message 2

mov ah,01h ; take no. of elements i/p
int 21h

cmp al,39h
jbe dnt_sub
sub al,07h

dnt_sub: sub al,30h
mov no_of_elements,al

mov bh,no_of_elements

lea si,array ; load base address of array
; in si register

accept_number:
message_disp msg3

call accept
mov [si],bl
inc si
dec bh
jnz accept_number

lea si,array
mov bh,no_of_elements
mov ch,00h
addition:
add ch,[si]
adc fd,00h
inc si
dec bh
jnz addition

call disply

mov ah,4ch ;terminate program
int 21h

disply proc near
message_disp msg4
mov dl,fd
add dl,30h
mov ah,02h
int 21h

mov cl,04h
mov dh,02h
mov bh,ch

shr ch,cl

disp_bit: and ch,0Fh

cmp ch,09h
jbe dnt_add1
add ch,07h

dnt_add1: add ch,30h
mov dl,ch
mov ah,02h
int 21h
mov ch,bh
dec dh
jnz disp_bit
endp


accept proc near
mov cl,04h

mov ah,01h
int 21h

cmp al,39h
jbe dnt_sub1
sub al,07h

dnt_sub1: sub al,30h
shl al,cl

mov bl,al

mov ah,01h
int 21h

cmp al,39h
jbe dnt_sub2
sub al,07h

dnt_sub2: sub al,30h
or bl,al

ret
endp

end
;----------------------------------------------------------------------------
; program done by Prof. Dhumane A.V. VIIT, Pune