Question

.code
> 
>     start:
>     mov ax,03h
>     int 10h
>     mov ax,seg msg1
>     mov ds,ax
>     mov dx,offset msg1
>     mov ah,09h
>     int 21h
>     mov si,offset str
>     read:
>     mov ah,01h
>     int 21h
>     cmp al,0dh
>     je next
>     mov [si],al
>     inc si
>     inc count
>     jmp read
>     next:
>     mov di,offset str
>     mov al,count
>     mov cl,al
>     mov ch,00h
>     dec si
>     check:
>     mov al,[si]
>     cmp al,[di]
>     jne nt
>     dec si
>     inc di
>     loop check
>     mov ax,seg msg2
>     mov ah,09h
>     int 21h
>     jmp exit
>     nt:
>     mov ax,seg msg3
>     mov ds,ax
>     mov dx,offset msg3
>     mov ah,09h
>     int 21h
>     exit:
>     mov ax,4c00h
>     int 21h
>     END start

This is part of 8086 masm code for checking whether a string is a palindrome or not.msg1 is 'Enter string',msg2 is 'string is palindrome',msg3 is 'string is not palinrome' What does 'cmp al,0dh' performs in this code?

Was it helpful?

Solution

It's not mentioned where this code came from, but it's incomplete (e.g. as Mario points out: no next: label) present. But we can piece it together:

.code

start:
    mov ax,03h           ; Get cursor position and shape
    int 10h

    ; Display a message to the user
    ; (NOTE: we only know it's in "msg1" but don't know the contents
    ;
    mov ax,seg msg1      ; DS:DX to point to msg1
    mov ds,ax
    mov dx,offset msg1

    mov ah,09h           ; Write the string (pointed by DS:DX) to stdout
    int 21h

    mov si,offset str    ; Get the the destination string location, DS:SI

    ; Read a string in from the user, terminated by new line (0dh)
    ;
read:
    mov ah,01h           ; Read a character
    int 21h

    cmp al,0dh           ; if it's a line feed, then go to "next"
    je next


    mov [si],al          ; otherwise, store the char in "str" and get the next one
    inc si
    inc count            ; increment character count
    jmp read

    ; Below is where the actual code to compute a palindrome starts
next:
    mov di,offset str
    mov al,count
    mov cl,al
    mov ch,00h
    dec si
check:
    mov al,[si]
    cmp al,[di]
    jne nt
    dec si
    inc di
    loop check

    mov ax,seg msg2

So all this code does is display a message to the user prompting them to enter a string, terminated by a line feed (0dh) and it reads the string in (to location str). It also provides the number of characters read in count. Where str, count, and msg1 are defined aren't given.

OTHER TIPS

The above piece of assembler does say all and nothing. It is probably a piece of code used by the (Microsoft) DOS, where the "int 21h" was the king of the entry points.

http://en.wikipedia.org/wiki/MS-DOS_API

By the way, as the docs say, the above call refers to the service 01h (=AH), and simply gets a character from the console. Once the "int 21h" returns, the actual character entered is stored in the low byte of the accumulator, that is AL.

At this point, the "cmp" instruction compares the AL with the fixed code 0Dh (i.e. CR=carriage return). Since the comparison is made by a subtraction of AL minus 0Dh, the match would be successful when the result is zero. If so, the program will jump to the label "next".

I really couldn't say more, but there's no "palindrome" checking at all in this snippet!

UPDATE: it looks that the snippet has been changed!

Well, even with the new code it seems that there's no palindrome checking, at least at first glance.

http://en.wikipedia.org/wiki/INT_10H

It looks like a character input with echo. However, I'm a bit rusty and I could mistake.

This code gets a String from the user and checks if it is a Palindrome or not.

org 100h

lea dx, string
mov ah, 0ah
int 21h

lea di, string+2
lea si, di
mov cl, string[1]
sub cl, 1
add si, cx
shr cx, 1

checkPal:
mov al, [di]
mov dl, [si]
cmp al, dl
jne printNotPal
inc di
dec si
loop checkPal

printPal:
lea dx, msgPal
jmp print

printNotPal:
lea dx, msgNotPal

print:
mov ah, 9h
int 21h

mov ah, 0
int 16h

string db  10 dup('$')
msgPal db  " is a Palindrome$"
msgNotPal db " is not a Palindrome$"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top