Hi, I wanna ask.. suppose I have an array and a constant called threshold in my data segments

array db 1,2,3,4,5,6
threshold equ 5

I want to compare my array elements with the constant, and only display/print the number which is less that the threshold, if there are none, display the none message

this is what I have done so far...I got error messages regarding the bold one

start2:
	mov ax, threshold
	mov al, array[bx]
	[B]mov bx,al
	cmp al,ax[/B]
	jl print
	dec al
	jnz start2

print:
	add bx,30h
	mov	al,array[bx]	;store the number at the array
	mov	ah,2		;display the number at the screen
	mov	dl,al
	add dl,30h
	int	21h
	inc	bl		;increment index register
	jnz print

can someone enlighten me?

if the initial value in the BX register was undefined then surely errors for those lines are expected.

so, I have to initialized? what value should it be?

if u dont set an initial value (like u did with threshold), how does it know where to begin comparing? i presume u want to use the value in BX for indexing, i.e. u are trying to run through the array as follows:

array[0] array[1] array[2] array[3] array[4] array[5]

if the initial value is undefined then to compare array[undefined] would be unpredictable.

and compare it with threshold 5. u need to work out the logic step by step first perhaps on a piece of paper.

BTW, what are those error messages ?

Hi,
The two error messages are because you are working with elements of two diferent sizes.
mov bx, al ;bx -> 16 bits. al -> 8 bits.
cmp al, ax ; al -> 8 bits. ax -> 16 bits.

I hope that this can be useful.

Rewrite:

I have no tested the next code because I am not with my computer. Please, correct the stuff if needed.

lea bx, array
mov al, value
mov di, 0
mov dx, itemsnumber
label1:
cmp al, [bx + di]
jne label2
call printvalue
label2:
inc di
dec dx
jnz label1

I was confused with the criteria. I do not remembered it.
Change jne to jae for unsigned numbers or to jge for signed numbers.

if u dont set an initial value (like u did with threshold), how does it know where to begin comparing? i presume u want to use the value in BX for indexing, i.e. u are trying to run through the array as follows:

array[0] array[1] array[2] array[3] array[4] array[5]

if the initial value is undefined then to compare array[undefined] would be unpredictable.

and compare it with threshold 5. u need to work out the logic step by step first perhaps on a piece of paper.

BTW, what are those error messages ?

I have set my threshold value to be 5, and my array values are 1,2,3,4,5,6
and before that I initialized bx, using

xor bx,bx

I want to compare all those value with 5, and only print the values which are less than 5 so in the end, it will display 1,2,3,4

basically I know how to compare, but I don't know how to print the values less than 5

Hi,
The two error messages are because you are working with elements of two diferent sizes.
mov bx, al ;bx -> 16 bits. al -> 8 bits.
cmp al, ax ; al -> 8 bits. ax -> 16 bits.

I hope that this can be useful.

Rewrite:

I have no tested the next code because I am not with my computer. Please, correct the stuff if needed.

lea bx, array
mov al, value
mov di, 0
mov dx, itemsnumber
label1:
cmp al, [bx + di]
jne label2
call printvalue
label2:
inc di
dec dx
jnz label1

what is the itemsnumber??

Hi,
You need to point bx to the start of the array.
xor bx, bx
is a faster version of:
mov bx, 0

bx must point to the start of the array. In masm you should use:

.data
array db 1,2,3,4,5,6
etc,

.code
mov ax, @data ;
mov ds, ax;
;You need to specify the data segment and offset. Now the data segment is in ds

.etc,

mov bx, offset array

and if this does not work:

lea bx, array ; (load efective address) Load the offset of the array inside the ds segment inside bx

Now in the pair: ds:bx is the adress of the first element of the array.
is a pointer to the array.

mov al, ds:[bx] ; will copy the first value inside the array to al

mov al, ds:[bx + 2]; will copy the third value.

The reason to use [bx] and not bx is because we use bx as a pointer. It contains the offset of array.

If inside bx there is a zero and you try something like:

mov register, [bx] ; Access violation error.

and if you try:

mov register, bx; and bx contains zero, the target register will contain zero.

And of course, I know more than needed to write your program and much more complex programs, but are you who are learning.

I you do not have a good assembler and you are interested in learning, search in google "masm32". It is free and includes:

1. The Microsoft assembler for 16 an 32 bits. Microsoft does not sells it anymore and releases it freely from its web.
2. There is a link to the 16 bits linker in some place of its web.
3. Contains a lot of include files an libraries to develop windows assembly programs.
4. Contains a lot of macros very useful.

With the same assembler I can guide you trought your program.

I hope that this can be useful to someone.

Hi,

actually, I'm doing this in turbo assembler, and I didn't copy my whole code here, sorry for making it hard for you to explain..here I copy my whole code..

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 1,2,3,4,5,6 ;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	threshold equ 5
	num db 0
	count equ 6
	
data ends

code segment
assume cs:code, ds:data, ss:stk

start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
	xor dx,dx
	mov si,offset array
	

display_msg:
	lea dx, prompt1
	mov ah,09
	int 21h
	jmp key
	
key:	
	mov	al,array[bx]	;store the number at the array
	mov	ah,2		;display the number at the screen
	mov	dl,al
	add dl,30h
	int	21h
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key

done1:
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
	mov	ah,2		;printing a space
	mov	dl,20h
	int 21h

elements:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	mov ah,09
	mov dx, offset msg
	int 21h
	mov ah,2 ;sent space
	mov dl,01h
	mov dl,20h
	int 21h
	mov dl,len
	add dl,30h ;convert the ascii value to integer number
	mov ah,2
	int 21h

thr_msg:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	mov ah,09
	mov dx, offset thr_prompt
	int 21h
	mov ah,2 ;sent space
	mov dl,20h
	int 21h
	mov dl,offset threshold
	add dl,30h
	mov ah,2h
	int 21h
cmpr_thr:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	
	mov al,array[bx]
	cmp al,threshold
	jl print

next:	
	inc bx
	dec cl
	jnz cmpr_thr
	jmp exit1
	
print:	
	mov	ah,2		;display the number at the screen
	mov	al,array[bx]	;store the number at the array
	add al,30h
	int	21h
	jmp next


exit1:	
	mov ah,4ch
	int 21h

code ends

end start

I have this problem, I couldn't print my result (the array) after the comparison...in another words, I don't know how to do...i'm stuck..the problems lie on the red portion above.. if my array is 1,2,3,4,5,6 the result after comparison should be 1,2,3,4

Could you please also show us the exact output from the above program? so that we might able to understand what your problem is when you say you *couldn't* print. (e.g. nothing get displayed at all?)

Also for debugging purpose, you should write a simple program to just scan this array and display EACH value on screen one by one. (i.e. without threshold comparison). (Are u able to do that?) If it can output 1 2 3 4 5 6 correctly as expected, then modify it to add the threshold comparison.

Could you please also show us the exact output from the above program? so that we might able to understand what your problem is when you say you *couldn't* print. (e.g. nothing get displayed at all?)

Also for debugging purpose, you should write a simple program to just scan this array and display EACH value on screen one by one. (i.e. without threshold comparison). (Are u able to do that?) If it can output 1 2 3 4 5 6 correctly as expected, then modify it to add the threshold comparison.

I have declared my array : 1,2,3,4,5,6 and my threshold value is 5, so after the comparison the result should be 1,2,3,4 I should print this..

what I meant by couldn't print is yes..nothing get displayed at all after the comparison..(the red portions are where the problem lies)

and yes, I'm able to print and display the array 123456 before comparison (see my code),I just don't know how to do the comparison and print the result

Hi, you are a good programmer. This kind of bugs are the worst ones. The tipyng ones
Look at the next code:

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 1,2,3,4,5,6 ;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	threshold equ 5
	num db 0
	count equ 6
	
data ends

code segment
assume cs:code, ds:data, ss:stk

start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
	xor dx,dx
	mov si,offset array
	

display_msg:
	lea dx, prompt1
	mov ah,09
	int 21h
	

	;WHY DO YOU WRITE THIS JUMP. key IS THE ADDRESS OF THE NEXT INSTRUCTION.	
	jmp key;REMOVE THIS LINE.
	;REMOVE IT
	
key:	
	mov	al,array[bx]	;store the number at the array
	mov	ah,2		;display the number at the screen
	mov	dl,al
	add dl,30h
	int	21h
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key

done1: 
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
	mov	ah,2		;printing a space
	mov	dl,20h
	int 21h
		

elements:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	
	mov dl,65d ;display line
	mov ah,2h
	int 21h	
	
	mov ah,09
	mov dx, offset msg
	int 21h
	mov ah,2 ;sent space
	mov dl,01h
	mov dl,20h
	int 21h
	mov dl,len
	add dl,30h ;convert the ascii value to integer number
	mov ah,2
	int 21h

thr_msg:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	mov ah,09
	mov dx, offset thr_prompt
	int 21h
	mov ah,2 ;sent space
	mov dl,20h
	int 21h
	mov dl,offset threshold
	add dl,30h
	mov ah,2h
	int 21h
cmpr_thr:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	
daniweb:	
	mov al,array[bx]
	cmp al,threshold
	jae bigger
	call print

bigger:
	inc bx
	dec cl
	jnz daniweb

exit1:	
	mov ah,4ch
	int 21h

print:	
	push ax
	push dx
	mov	ah,2		;display the number at the screen
	;YOU MUST PASS THE ASCII CHARACTER IN DL
	;mov al, array[bx]
	mov	dl,array[bx]	;store the number at the array
	;DO NOT WORRY. THIS IS A TIPYNG BUG
	;add al, 30h
	add dl,30h
	int	21h
	pop dx
	pop ax
	ret

code ends

end start

It compiles in masm, and I think that in tasm also.

Hi, you are a good programmer. This kind of bugs are the worst ones. The tipyng ones
Look at the next code:

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 1,2,3,4,5,6 ;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	threshold equ 5
	num db 0
	count equ 6
	
data ends

code segment
assume cs:code, ds:data, ss:stk

start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
	xor dx,dx
	mov si,offset array
	

display_msg:
	lea dx, prompt1
	mov ah,09
	int 21h
	

	;WHY DO YOU WRITE THIS JUMP. key IS THE ADDRESS OF THE NEXT INSTRUCTION.	
	jmp key;REMOVE THIS LINE.
	;REMOVE IT
	
key:	
	mov	al,array[bx]	;store the number at the array
	mov	ah,2		;display the number at the screen
	mov	dl,al
	add dl,30h
	int	21h
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key

done1: 
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
	mov	ah,2		;printing a space
	mov	dl,20h
	int 21h
		

elements:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	
	mov dl,65d ;display line
	mov ah,2h
	int 21h	
	
	mov ah,09
	mov dx, offset msg
	int 21h
	mov ah,2 ;sent space
	mov dl,01h
	mov dl,20h
	int 21h
	mov dl,len
	add dl,30h ;convert the ascii value to integer number
	mov ah,2
	int 21h

thr_msg:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	mov ah,09
	mov dx, offset thr_prompt
	int 21h
	mov ah,2 ;sent space
	mov dl,20h
	int 21h
	mov dl,offset threshold
	add dl,30h
	mov ah,2h
	int 21h
cmpr_thr:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	
daniweb:	
	mov al,array[bx]
	cmp al,threshold
	jae bigger
	call print

bigger:
	inc bx
	dec cl
	jnz daniweb

exit1:	
	mov ah,4ch
	int 21h

print:	
	push ax
	push dx
	mov	ah,2		;display the number at the screen
	;YOU MUST PASS THE ASCII CHARACTER IN DL
	;mov al, array[bx]
	mov	dl,array[bx]	;store the number at the array
	;DO NOT WORRY. THIS IS A TIPYNG BUG
	;add al, 30h
	add dl,30h
	int	21h
	pop dx
	pop ax
	ret

code ends

end start

It compiles in masm, and I think that in tasm also.

Thank you very much for your feedback.. Actually I have solved the problem before I see your reply.. I'm new to assembly language.. so I code using simple mnemonic.. I don't quite understand about pop and push.. but anyway, I will learn about it..Thanks..

and, if you don't mind.. I have another problem about finding even number, count them and compute the average.. it's still the continuation of my code.. here I copy my whole code..

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 2,3,4,5,6,7 ;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	threshold equ 5
	threshold_count db 0
	msg_thr db 'Numbers less than threshold: $'
	none_msg db 'There are no numbers less than threshold$'
	
	
data ends

code segment
assume cs:code, ds:data, ss:stk
count equ 6
start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
	xor dx,dx

display_msg:
	lea dx, prompt1
	mov ah,09
	int 21h
	
key:	
	mov	al,array[bx]	;store the number at the array
	mov	ah,2		;display the number at the screen
	mov	dl,al
	add dl,30h
	int	21h
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key

done1:
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
	mov	ah,2		;printing a space
	mov	dl,20h
	int 21h

elements:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	mov ah,09
	mov dx, offset msg
	int 21h
	mov ah,2 ;sent space
	mov dl,01h
	mov dl,20h
	int 21h
	mov dl,len
	add dl,30h ;convert the ascii value to integer number
	mov ah,2
	int 21h

thr_msg:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	mov ah,09
	mov dx, offset thr_prompt
	int 21h
	mov ah,2 ;sent space
	mov dl,20h
	int 21h
	mov dl,offset threshold
	add dl,30h
	mov ah,2h
	int 21h
	
	mov dl,10d ;display line
	mov ah,2h
	int 21h

cmpr_thr:
	mov al,array[bx]
	cmp al,threshold
	jl print
	
next:	
	inc bl
	dec cl
	jnz cmpr_thr
	mov al,threshold_count
	cmp al,0
	jz print_none
	jmp exit1
	
print:
	inc threshold_count
	mov ah,2
	mov	al,array[bx]	;store the number at the array;
	mov dl, al
	add dl,30h
	int	21h
	jmp next
	
	
print_none:
	mov ah,09
	mov dx, offset none_msg
	int 21h
	

check_even:
	mov si,offset array
	
again:
	mov ax,array[bx]
	ror ax,01
	jnc even_no ; CF=0 even number
	inc bx
	jmp next1

even_no:
	inc dx
	je print_even

next1:
	add si,02
	dec cl
	jnz again
	
print_even:
	mov dl,10d ;display line
	mov ah,2h
	int 21h
	mov ah,2
	mov	al,array[bx]	;store the number at the array;
	mov dl, al
	add dl,30h
	int	21h
	
exit1:	
	mov ah,4ch
	int 21h


code ends

end start

So, it says, after the threshold problem, now I have to find even number in my array, show,count them and compute the average.. I tried to code the even number, but I don't know how display the result..and I haven't coded the average...

[ removed ]

Hi,
Look at this code. It compiles in masm:

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 1,2,3,4,5,6 ;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	newline db 10, 13, '$'
	threshold equ 5
	num db 0
	count equ 6
	
data ends

code segment
assume cs:code, ds:data, ss:stk

start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
display_msg:
	lea dx, prompt1
	call printstring
key:	
	mov	dl,array[bx]	;store the number at the array
	call printnumber
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key
    
;done1: 
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
elements:
	call printnewline
	mov dx, offset msg
	call printstring
	mov dl,20h
	call printchar
	mov dl,len
	call printnumber
	call printnewline
	mov dx, offset thr_prompt
	call printstring
	mov dl,20h
	call printchar
	mov dl, threshold
    call printnumber
	call printnewline
daniweb:	
	mov dl,array[bx]
	cmp dl,threshold
	jae bigger
	call printnumber
bigger:
	inc bx
	dec cl
	jnz daniweb
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
check_even:
	call printnewline
	xor bx, bx
	;YOU DO NOT USE SI.
	;mov si,offset array
	mov cl, count
	xor dh, dh
	xor ax, ax
	xor si, si
again:
	;YOU ARE GETING WORDS (2 BYTES) BECAUSE YOU ARE USING AX. AX IS A 16 BITS REGISTER.
	;mov ax,array[bx]
	;ROR MODIFIES THE REGISTER.
	;ror ax,1 
	mov dl, array[si]
	;TEST PERFORMS AN AND OPERATION WITHOUT CHANGING THE OPERANDS. IT CHANGES THE FLAGS.
	test dl, 1
	jnz noeven_no
	call printnumber
	inc dh
	mov bl, dl
	add ax, bx
noeven_no:
	;add si,02 YOU ARE NOT USING SI
	inc si
	dec cl
	jnz again
	call printnewline
	;EVEN NUMBERS QUANTITY IS AT DH	
	mov dl, dh
	call printnumber
	;EVEN NUMBERS SUM IS IN AX. EVEN NUMBERS COUNT IS IN DH
	div dh
	call printnewline
	;EVEN NUMBERS AVERAGE IS IN AX
	mov dl, al
	call printnumber
	mov ah,4ch
	int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DX = offset of the string $ ended 
printstring:
	push ax
	mov ah,09
	int 21h
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = ASCII VALUE OF THE CHARACTER TO PRINT
printchar:	
	push ax
	push dx
	mov	ah,2		;display the number at the screen
	int	21h
	pop dx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = NUMBER
printnumber:
	push dx
	add dl, 30h
	call printchar
	pop dx
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: NONE
printnewline:
	push dx
	mov dx, offset newline
	call printstring
	pop dx
	ret
	
code ends
end start

Hi,
Look at this code. It compiles in masm:

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 1,2,3,4,5,6 ;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	newline db 10, 13, '$'
	threshold equ 5
	num db 0
	count equ 6
	
data ends

code segment
assume cs:code, ds:data, ss:stk

start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
display_msg:
	lea dx, prompt1
	call printstring
key:	
	mov	dl,array[bx]	;store the number at the array
	call printnumber
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key
    
;done1: 
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
elements:
	call printnewline
	mov dx, offset msg
	call printstring
	mov dl,20h
	call printchar
	mov dl,len
	call printnumber
	call printnewline
	mov dx, offset thr_prompt
	call printstring
	mov dl,20h
	call printchar
	mov dl, threshold
    call printnumber
	call printnewline
daniweb:	
	mov dl,array[bx]
	cmp dl,threshold
	jae bigger
	call printnumber
bigger:
	inc bx
	dec cl
	jnz daniweb
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
check_even:
	call printnewline
	xor bx, bx
	;YOU DO NOT USE SI.
	;mov si,offset array
	mov cl, count
	xor dh, dh
	xor ax, ax
	xor si, si
again:
	;YOU ARE GETING WORDS (2 BYTES) BECAUSE YOU ARE USING AX. AX IS A 16 BITS REGISTER.
	;mov ax,array[bx]
	;ROR MODIFIES THE REGISTER.
	;ror ax,1 
	mov dl, array[si]
	;TEST PERFORMS AN AND OPERATION WITHOUT CHANGING THE OPERANDS. IT CHANGES THE FLAGS.
	test dl, 1
	jnz noeven_no
	call printnumber
	inc dh
	mov bl, dl
	add ax, bx
noeven_no:
	;add si,02 YOU ARE NOT USING SI
	inc si
	dec cl
	jnz again
	call printnewline
	;EVEN NUMBERS QUANTITY IS AT DH	
	mov dl, dh
	call printnumber
	;EVEN NUMBERS SUM IS IN AX. EVEN NUMBERS COUNT IS IN DH
	div dh
	call printnewline
	;EVEN NUMBERS AVERAGE IS IN AX
	mov dl, al
	call printnumber
	mov ah,4ch
	int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DX = offset of the string $ ended 
printstring:
	push ax
	mov ah,09
	int 21h
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = ASCII VALUE OF THE CHARACTER TO PRINT
printchar:	
	push ax
	push dx
	mov	ah,2		;display the number at the screen
	int	21h
	pop dx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = NUMBER
printnumber:
	push dx
	add dl, 30h
	call printchar
	pop dx
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: NONE
printnewline:
	push dx
	mov dx, offset newline
	call printstring
	pop dx
	ret
	
code ends
end start

Thanks a lot for the revision it's really help... and I have another question, I promise you this is my last question, about finding the prime number on the array and display them... highlighted is my portion of code which is unfortunately not working (again) I have "divide overflow" error.. I don't quite understand how to set my limit, so the loop won't go over my array

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 2,3,4,5,6,7,8,9 ;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	newline db 10, 13, '$'
	threshold equ 5
	;num db 0
	count equ 8
	
data ends

code segment
assume cs:code, ds:data, ss:stk

start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
display_msg:
	lea dx, prompt1
	call printstring
	
key:	
	mov	dl,array[bx]	;store the number at the array
	call printnumber
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key
    
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
	
elements:
	call printnewline
	mov dx, offset msg
	call printstring
	mov dl,20h
	call printchar
	mov dl,len
	call printnumber
	call printnewline
	mov dx, offset thr_prompt
	call printstring
	mov dl,20h
	call printchar
	mov dl, threshold
    call printnumber
	call printnewline

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
threshold_check:	
	mov dl,array[bx]
	cmp dl,threshold
	jae bigger
	call printnumber
	
bigger:
	inc bx
	dec cl
	jnz threshold_check
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
setup_even:
	call printnewline
	xor bx, bx
	mov cl, count
	xor dh, dh
	xor ax, ax
	xor si, si
	
check_even: 
	mov dl, array[si]
	test dl, 1 ;test performs and operation without changing the operands, it changes the flags
	jnz noeven_no
	call printnumber
	inc dh
	mov bl, dl
	add ax, bx
	
noeven_no:
	inc si
	dec cl
	jnz check_even
	call printnewline
	mov dl, dh ;even number quantity is at dh
	call printnumber
	;even numbers sum is in ax, and even numbers count is in dh
	div dh
	call printnewline
	;EVEN NUMBERS AVERAGE IS IN AX
	mov dl, al
	call printnumber
	;mov ah,4ch
	;int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
check_prime:
	mov al,array[si]
	mov ax,1 ;the first number
	jmp foundprime

newnum: ;next number
	inc ax
	mov bx,1

begintest: ;start test loop
	inc bx ;advance divisor
	cmp ax,bx ;check if number=divisor
	je foundprime ;if so, loop finished
	mov cx,ax ;save number
	xor dx,dx
	div bx ;divide number by divisor
	xchg dx,cx
	mov ax,dx ;put number back
	jcxz newnum ;it's not prime
	jmp begintest

foundprime:
	mov bx,ax ;store current number
	mov si,10 ;load divisor with 10;	
	xor cx,cx ;zero digit count

non_zero:
	xor dx,dx
	div si
	push dx
	inc cx
	or ax,ax ;check for the end of loop
	jne non_zero ;next digit to print
	mov ax,bx ;restore the current number
	
print_digit:
	pop dx
	add dl,30h
	mov si,ax ;store ascii value
	mov ah,2
	int 21h
	
	mov ax,si
	loop print_digit
	mov dl," " ;a space
	mov cx,ax
	mov ah,2
	int 21h
	
	mov ax,cx
	jmp newnum
	
	mov ah,4ch ;exit
	int 21h

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DX = offset of the string $ ended 
printstring:
	push ax
	mov ah,09
	int 21h
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = ASCII VALUE OF THE CHARACTER TO PRINT
printchar:	
	push ax
	push dx
	mov	ah,2 ;display the number at the screen
	int	21h
	pop dx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = NUMBER
printnumber:
	push dx
	add dl, 30h
	call printchar
	pop dx
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: NONE
printnewline:
	push dx
	mov dx, offset newline
	call printstring
	pop dx
	ret
	
code ends
end start

Hi,
What do you think about the next code:

stk	segment	stack
	db	128 DUP(?)
stk	ends
;

data segment
	prompt1 db 'Your array: $'
	array db 1,2,3,4,5,6, 7, 8, 9 , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24;declare a byte array
	len equ $-array ;"$" means current address. 
	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	newline db 10, 13, '$'
	threshold equ 5
	num db 0
	count equ 24
	
data ends

code segment
assume cs:code, ds:data, ss:stk

start:
	mov ax,data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
display_msg:
	lea dx, prompt1
	call printstring
key:	
	mov	dl,array[bx]	;store the number at the array
	call printnumber
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key
    
;done1: 
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
elements:
	call printnewline
	mov dx, offset msg
	call printstring
	mov dl,20h
	call printchar
	mov dl,len
	call printnumber
	call printnewline
	mov dx, offset thr_prompt
	call printstring
	mov dl,20h
	call printchar
	mov dl, threshold
    call printnumber
	call printnewline
daniweb:	
	mov dl,array[bx]
	cmp dl,threshold
	jae bigger
	call printnumber
bigger:
	inc bx
	dec cl
	jnz daniweb
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
check_even:
	call printnewline
	xor bx, bx
	;YOU DO NOT USE SI.
	;mov si,offset array
	mov cl, count
	xor dh, dh
	xor ax, ax
	xor si, si
again:
	;YOU ARE GETING WORDS (2 BYTES) BECAUSE YOU ARE USING AX. AX IS A 16 BITS REGISTER.
	;mov ax,array[bx]
	;ROR MODIFIES THE REGISTER.
	;ror ax,1 
	mov dl, array[si]
	;TEST PERFORMS AN AND OPERATION WITHOUT CHANGING THE OPERANDS. IT CHANGES THE FLAGS.
	test dl, 1
	jnz noeven_no
	call printnumber
	inc dh
	mov bl, dl
	add ax, bx
noeven_no:
	;add si,02 YOU ARE NOT USING SI
	inc si
	dec cl
	jnz again
	call printnewline
	;EVEN NUMBERS QUANTITY IS AT DH	
	mov dl, dh
	call printnumber
	;EVEN NUMBERS SUM IS IN AX. EVEN NUMBERS COUNT IS IN DH
	div dh
	call printnewline
	;EVEN NUMBERS AVERAGE IS IN AX
	mov dl, al
	call printnumber
	
	call printnewline
	xor bx, bx
	mov cl, count
	
daniwebprime:
	mov al, array[bx]
	call tryprime
	inc bx
	dec cl
	jnz daniwebprime	
	
	mov ah,4ch
	int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
tryprime:
;INPUT AL = Number to test and print if prime
	push ax
	push bx
	push cx
	push dx
	mov bl, al
	cmp bl, 4
	jae dotest
	mov dl, bl
	call printnumber
	jmp primeexit
dotest:
	xor ch, ch
	mov cl, bl
	shr cx, 1
	inc cx
	xor dh, dh
	mov dl , cl
tryloop:
	xor ah, ah	
	mov al, bl
	dec dl
	div dl
	cmp ah, 0 
	jne nexttryloop
	jmp primeexit
	jmp primeexit
nexttryloop:
	cmp dl, 2
	ja tryloop
	mov dl, bl
	call printnumber
primeexit:
	pop dx
	pop cx
	pop bx
	pop ax
	ret	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DX = offset of the string $ ended 
printstring:
	push ax
	mov ah,09
	int 21h
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = ASCII VALUE OF THE CHARACTER TO PRINT
printchar:	
	push ax
	push dx
	mov	ah,2		;display the number at the screen
	int	21h
	pop dx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = BYTE VALUE TO PRINT
printnumber:
	push ax
	push bx
	push cx
	push dx
	xor ah, ah
	mov al, dl
	mov bl, 100
	div bl
	mov dl, al
	call printifnozero
	mov al, ah
	xor ah, ah
	mov bl, 10
	div bl
	mov dl, al
	call printifnozero
	mov dl, ah
	call printonenumber
	pop dx
	pop cx
	pop bx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INTPUT: DL = 1 DIGIT NUMBER TO PRINT IF NOT EQUAL TO ZERO
printifnozero:
	cmp dl, 0
	jz endprintnz
	call printonenumber
endprintnz:
	ret		
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = NUMBER OF ONE DIGIT TO PRINT
printonenumber:
	push dx
	add dl, 30h
	call printchar
	pop dx
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: NONE
printnewline:
	push dx
	mov dx, offset newline
	call printstring
	pop dx
	ret
	
code ends
end start

REWRITE:
THIS CODE FAILS WHEN PRINTING NUMBERS OF THREE DIGITS. WHEN CORRECTED I SHALL
UPLOAD THE CORRECT CODE.

Hi,
I have rewriten the printing of numbers.
Now it is correct. Try this code:

.model small


threshold equ 5
count equ 24

.stack 2048





.data
	prompt1 db "Your array: $"
	mystring db 40 dup("a"), '$'

	thr_prompt db 'Your threshold number is: $'
	msg db 'The number of elements of your array is:$'
	newline db 10, 13, '$'
	array db 1,2,3,4,5,6, 7, 8, 9 , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 101;declare a byte array
	len equ $-array ;"$" means current address. 

	num db 0

	


.code 

start:
	mov ax,@data ;initialise data segment
	mov ds,ax
	xor	ch,ch		;initialize counter
	mov	cl,count	
	xor	bx,bx		;initialize index register	
display_msg:
	mov dx, offset prompt1
	call printstring
	
key:	
	mov	dl,array[bx]	;store the number at the array
	call printnumber
	inc	bl		;increment index register
	dec	cl		;decrement counter
	jnz	key
    
	xor	ch,ch		;initialize counter
	mov	cl,count
	xor	bx,bx		;initialize index register	
elements:
	call printnewline
	mov dx, offset msg
	call printstring
	mov dl,20h
	call printchar
	mov dl,len
	call printnumber
	call printnewline
	mov dx, offset thr_prompt
	call printstring
	mov dl,20h
	call printchar
	mov dl, threshold
    call printnumber
	call printnewline
daniweb:	
	mov dl,array[bx]
	cmp dl,threshold
	jae bigger
	call printnumber
bigger:
	inc bx
	dec cl
	jnz daniweb
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
check_even:
	call printnewline
	xor bx, bx
	;YOU DO NOT USE SI.
	;mov si,offset array
	mov cl, count
	xor dh, dh
	xor ax, ax
	xor si, si
again:
	;YOU ARE GETING WORDS (2 BYTES) BECAUSE YOU ARE USING AX. AX IS A 16 BITS REGISTER.
	;mov ax,array[bx]
	;ROR MODIFIES THE REGISTER.
	;ror ax,1 
	mov dl, array[si]
	;TEST PERFORMS AN AND OPERATION WITHOUT CHANGING THE OPERANDS. IT CHANGES THE FLAGS.
	test dl, 1
	jnz noeven_no
	call printnumber
	inc dh
	mov bl, dl
	add ax, bx
noeven_no:
	;add si,02 YOU ARE NOT USING SI
	inc si
	dec cl
	jnz again
	call printnewline
	;EVEN NUMBERS QUANTITY IS AT DH	
	mov dl, dh
	call printnumber
	;EVEN NUMBERS SUM IS IN AX. EVEN NUMBERS COUNT IS IN DH
	div dh
	call printnewline
	;EVEN NUMBERS AVERAGE IS IN AX
	mov dl, al
	call printnumber
	
	call printnewline
	xor bx, bx
	mov cl, count
	
daniwebprime:
	mov al, array[bx]
	call tryprime
	inc bx
	dec cl
	jnz daniwebprime	
	
;EXIT OF THE PROGRAM
	mov ah,4ch
	int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
tryprime:
;INPUT AL = Number to test and print if prime
	push ax
	push bx
	push cx
	push dx
	mov bl, al
	cmp bl, 4
	jae dotest
	mov dl, bl
	call printnumber
	jmp primeexit
dotest:
	xor ch, ch
	mov cl, bl
	shr cx, 1
	inc cx
	xor dh, dh
	mov dl , cl
tryloop:
	xor ah, ah	
	mov al, bl
	dec dl
	div dl
	cmp ah, 0 
	jne nexttryloop
	jmp primeexit
	jmp primeexit
nexttryloop:
	cmp dl, 2
	ja tryloop
	mov dl, bl
	call printnumber
primeexit:
	pop dx
	pop cx
	pop bx
	pop ax
	ret	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DX = offset of the string $ ended 
printstring:
	push ax
	push dx
	mov ah,9h
	int 21h
	pop dx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = ASCII VALUE OF THE CHARACTER TO PRINT
printchar:	
	push ax
	push dx
	mov	ah,2		;display the number at the screen
	int	21h
	pop dx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: DL = BYTE VALUE TO PRINT
printnumber:
	push ax
	push bx
	push dx
	xor ah, ah
	mov al, dl
	mov bx, offset mystring
	call miinttostr
	mov dx, offset mystring
	call printstring			
	pop dx
	pop bx
	pop ax
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INPUT: NONE
printnewline:
	push dx
	mov dx, offset newline
	call printstring
	pop dx
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
miinttostr:
;INPUT: BX = OFFSET OF THE TARGET STRING
;INPUT: AX = THE UNSIGNED NUMBER TO CONVERT TO STRING
      
        push ax
        push bx
        push cx
        push dx
        push si
        xor     cx,     cx
        mov     si,     10d
label2i:
        ;I divide the number by 10 repeatedly
        ; and I put the remainders in the stack.
        cmp     ax,     10d
        jb      label1i
	xor 	dx,	dx
        div     si
        push    dx
        inc     cx
        jmp     label2i
label1i:
        push    ax
        inc     cx
label3i:
        ;I retrive the numbers in the stack and
        ;I add to them the ascii value for '0' (character).
        pop     dx
        add     dx, 30h
        mov     byte ptr ds:[bx], dl
        inc     bx
        dec     cx
        jnz     label3i
        ;The service 9h of the 21h interrupt prints a string
        ;terminated by the character '$'
        mov     byte ptr ds:[bx], '$'
        pop     si
        pop     dx
        pop     cx
        pop     bx
        pop     ax
        ret
end start

Hi, Thank you very much!! really thanks a lot...I don't know what to say..but, you really help me a lot..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.