hey all,

im trying to compare two strings 8 characters long, the first was predefined and the second was entered by the user. i tried used (cmpsb) instruction and byte by byte comparison, but both failed

its for a project that requires a student to enter their number and password, then outputs the courses a student should take.

i've figured the the BX register is not loaded the right value, also im not sure my variable declaration for input is right.

please take a look at the highlighted code

thanks in advnace

.MODEL SMALL
.STACK 100H
.DATA

st1	db 13,10,"You are a first-year student; you should register MATH 141, COMP142, ENGC 101.$"
st2	db 13,10,"You are a second-year student; you should register ENCS 234, COMP 231, ENEE 231.$"
st3	db 13,10,"You are a third-year student; you should register ENCS 331, COMP 333, ENEE 331.$"
st4	db 13,10,"You are a fourth-year student; you should register ENCS 432, COMP 433, MATH 331.$"
st5	db 13,10,"You are a fifth-year student; you should register ENCS 535, ENCS 536, ENCS 539.$"

mess1	db 13,10,"Enter Student Number:$"
mess2	db 13,10,"Enter Password:$"

pass	db "word1234"

stdnum	db 8 dup (' ')
stdpas	db 8 dup (' ')

.code

start:

	call setscr			;set screen


	mov	Ax, @data
	mov	ds, Ax

	
	mov	dx, offset mess1	;print "Enter Student Number"
	call	prnt

	mov	ah, 0ah			;take input for student number
	mov	dx, offset stdnum
	int	21h


	mov	dx, offset mess2	;print "Enter Password"
	call	prnt 

	mov	ah, 0ah			;take input for student password
	mov	dx, offset stdpas
	int	21h	

	call	cmppass	




	mov AH, 4cH
	int 21H

; ------------------------ print functions ------------------------


prnt: 

	mov ah, 09h
	int 21h
	ret

prntst1:				;print 1st year courses

	mov dx, offset st1
	call prnt
	ret	

prntst2:				;print 2nd year courses

	mov dx, offset st2
	call prnt	
	ret

prntst3:				;print 3rd year courses

	mov dx, offset st3
	call prnt	
	ret

prntst4:				;print 4th year courses

	mov dx, offset st4
	call prnt	
	ret

prntst5:				;print 5th year courses

	mov dx, offset st5
	call prnt	
	ret

; ----------------------- compare functions -----------------------

cmppass:


	mov	ax, @data
	mov	bx, @data
	mov	al, [pass]
	mov	bl, [stdpas]
    mov dx, 0
	mov	cx, 8
	
C20:	
    
    cmp	al,bl
	jne	noteq
	
	inc	al
	inc	bl
	dec	cx
	cmp	cx, 0
	jne	c20
	
	

	call	prntst1

	
ret


noteq:

	call	prntst3
	ret


; --------------- set screen, set color and set cursor --------------

setscr:

	mov ax, 0600H
	mov cx, 0        
	mov dx, 184FH    
	mov bh, 7
	int 10H

        mov ah,06H      
        mov al,00H      
        mov bh,89H     	;green background and red forground
        mov cx,0    	
        mov dx,184FH    
        int 10H

        mov ah,2      	;set cursor
        mov dh,02H      ;row number
        mov dl,00H      ;column number
        mov bh,0        ;page number
        int 10H

	ret




end start

Dani AI

Generated

Two root problems explain the failing comparisons: the DOS buffered-input format and treating byte registers as pointers. DOS function 0Ah does not place typed characters at the buffer label itself — it stores a maximum size at byte 0, the actual length at byte 1, and the characters beginning at offset+2 — so a simple db 8 dup(' ') will not work. Also, incrementing AL/BL or loading AL with an address won’t step through memory; string ops expect real pointers (SI/DI) and cmpsb compares DS:SI with ES:DI.

Correct buffer layout and a concise compare pattern (MASM/TASM style) look like this:

; DOS 0Ah buffer: [max][len][chars...]
stdnum  db 8, 0, 8 dup(0)
stdpas  db 8, 0, 8 dup(0)
; compare fixed password 'pass' with typed password
lea si, pass           ; DS:SI -> expected text
lea di, stdpas + 2     ; DS:DI -> typed chars
mov cx, 8
cld
mov ax, ds
mov es, ax             ; ES = DS so cmpsb compares same segment
repe cmpsb
jnz not_equal
; equal

Practical notes and troubleshooting:

  • Use the length byte at stdpas+1 to avoid comparing unused buffer bytes; compare lengths first or limit CX to that length.
  • Don’t try to treat AL/BL as pointers — increment SI/DI (or use the string instruction family).
  • Ensure DS is initialized to your data segment before using offsets, and set ES=DS before cmpsb (push ds/pop es or mov ax,ds / mov es,ax).
  • ’s point about many small prntst# routines is valid: consolidate printing by passing the message pointer in DX or keeping an array of message pointers to reduce repeated code.

Fixing the buffer layout, using SI/DI or repe cmpsb, and honoring the returned length will make the password comparison reliable.

Recommended Answers

All 3 Replies

are you BZU ??

may we help each other :)

add me

<snip email>

What's the point of all those prntst# functions?...

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.