views:

144

answers:

3

What am I doing wrong?

This is the assmebly I've written:

char encode(char plain){
__asm{
    mov al, plain   

    ;check for y or z status
    cmp al, 'y'
    je YorZ
    cmp al, 'z'
    je YorZ
    cmp al, 'Y'
    je YorZ
    cmp al, 'Z'
    je YorZ

    ;check to make sure it is in the alphabet now
    mov cl, al
    sub cl, 'A'

    cmp cl, 24
    jl Other

    sub cl, '6' ;there are six characters between 'Z' and 'a'

    cmp cl, 24
    jl Other
    jmp done    ;means it is not in the alphabet



YorZ:
    sub al, 24
    jmp done

Other:
    add al, 2
    jmp done

done:
    leave
    ret
}
}

and this is the C code it's supposed to replace, but doesn't

char encode(char plain){
char code;
if((plain>='a' && plain<='x') || (plain>='A' && plain <='X')){
    code = plain+2;
}else if(plain == 'y' || plain=='z' || plain=='Y' || plain == 'y'){
    code = plain - 24;
}else{
    code = plain;
}

return code;
}

It seems to convert every character that isn't an y,z,Y,Z into a plus 2 equivalent instead of just A-Xa-x. Any ideas why?

+2  A: 

jl is a signed comparison.

Doug Currie
then what should I use?
Malfist
ah-ha, jb is unsigned
Malfist
A: 

I think that

sub cl, '6'

should be

sub cl, 6

otherwise, you are stating that there are '6' -> 0x36 -> 54 characters between 'Z' and 'a'.

lavinio
A: 

For an unsigned comparison, you need 'jb' instead of 'jl'.

Jerry Coffin