【问题标题】:Attempting to convert an if statement to assembly试图将 if 语句转换为程序集
【发布时间】:2010-04-27 01:02:07
【问题描述】:

我做错了什么?

这是我写的汇编:

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
}
}

这是它应该替换的 C 代码,但没有

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;
}

似乎将每个不是 y,z,Y,Z 的字符都转换为等值的 plus 2 而不仅仅是 A-Xa-x。任何想法为什么?

【问题讨论】:

  • 仅供参考 - 您的原始 C 代码对“y”进行了重复检查,但缺少对“Z”的检查。

标签: c assembly masm


【解决方案1】:

jl 是带符号的比较。

【讨论】:

    【解决方案2】:

    我认为

    sub cl, '6'
    

    应该是

    sub cl, 6
    

    否则,您是说在 'Z' 和 'a' 之间有 '6' -> 0x36 -> 54 个字符。

    【讨论】:

      【解决方案3】:

      对于无符号比较,您需要 'jb' 而不是 'jl'。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-09
        • 2023-04-08
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-09
        相关资源
        最近更新 更多