user35443
user35443

Reputation: 6403

String comparison is not working

I have a small problem.

This is my code:

cmpstr:
pusha
xor cx, cx

mov ax, ds
push ax
mov ax, si
push ax
call strlen
mov dx, ax
mov ax, es
mov ds, ax
mov si, bx
call strlen
cmp al, dl
jnz .fail
pop ax
mov si, ax
pop ax
mov ds, ax

.loop:
    push bx
    mov al, byte [es:bx]
    mov bl, byte [ds:si]

    cmp al, bl
    jne .fail
    cmp bl, 0
    jz .suc

    pop bx
    add bx, 1
    inc si
    inc cx
    jmp .loop
.fail:
    mov al, 'C';
    mov ah, 0Eh
    int 10h
    popa
    mov ax, 0
    jmp .end
.suc:
    mov al, 'D';
    mov ah, 0Eh
    int 10h
    popa
    mov ax, 1
.end:
ret

This procedure should compare two strings and return (mov to ax) 1, if strings (first on es:bx, second ds:si) are same or 0 if they're different. My problem is that command before procedure call is executed, letter 'D' is being printed (comparasion was successful) but command after procedure call is not working. I think that problem must be somewhere in this procedure. Does anybody know what is wrong here?

Upvotes: 0

Views: 373

Answers (1)

Bo Persson
Bo Persson

Reputation: 92211

When you leave the loop with one of the conditional jumps

.loop:
    push bx
    mov al, byte [es:bx]
    mov bl, byte [ds:si]

    cmp al, bl
    jne .fail
    cmp bl, 0
    jz .suc

You have pushed BX, but you never pop it. That will make the next RET go to strange places.

Upvotes: 2

Related Questions