需要帮助理解与MASM条件指令 [英] Need help understanding conditional directives with MASM

查看:178
本文介绍了需要帮助理解与MASM条件指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用MASM指令来实现MASM下面的C code:

I am trying to implement the following c code in MASM using MASM directives:

if ( a > b )
    a = a - 1;
else
    if ( b >= c )
        b = b − 2;
    else
        if ( c > d)
            c = c + d;
        else
            d = d / 2;

这是我的尝试:

.if (a > b)
sub a, 1
.elseif b >= c1
sub b, 2
.elseif c1 > d
add c1, d
.else
mov eax, d
cdq
mov ebx, 2
idiv ebx
mov d, eax
.endif
.endif

我觉得我的逻辑是合理的但无论怎样我改变周围的,以保持它的完整,我收到错误信息。我相信我有误解的东西,但不要不确定的东西。

I feel like my logic is sound yet no matter what I change around to keep it intact I am getting errors. I am sure I have misunderstood something, but don't unsure about what.

推荐答案

首先,因为你只有一个开口。如果,你只需要一个 .endif注意。其次,至少在 A B C1 ,和 D 都是正常的内存操作数,你有大部分指令不能使用两个内存操作数(直接)的一个问题。对于典型的比较,操作数的至少一个最多可以在寄存器中。

First of all, since you have only one opening .if, you only need one .endif. Second, at least if a, b, c1, and d are normal memory operands, you have a problem that most instructions can't use two memory operands (directly). For your typical comparisons, at least one of the operands most be in a register.

顺便说一句,我也缩进。如果(等等)code,就像你通常会code在一个更高层次的语言。至少通常情况下,我还用而不是子X,1 ,大概 SHR 而不是 IDIV 来除以2。

As an aside, I'd also indent .if (and such) code just like you normally would code in a higher level language. At least normally, I'd also use dec instead of sub x, 1, and probably shr instead of idiv to divide by 2.

考虑上述所有的考虑,你最终是这样的:

Taking all of the above into account, you end up with something like this:

.model flat, c
.data
    a dd ?
    b dd ?
    c1 dd ?
    d dd ?

.code
junk proc
     mov eax, a
     mov ebx, b
     mov ecx, c1
     mov edx, d

    .if eax > ebx
        dec a
    .elseif ebx >= ecx
        sub b, 2
    .elseif ecx > edx
        add ecx, edx
        mov c1, ecx
    .else
        shr edx, 1
        mov d, edx
    .endif
junk endp

    end

这组装就好了,至少对我来说。

This assembles just fine, at least for me.

这篇关于需要帮助理解与MASM条件指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆