代码执行条件错误? [英] Code executes condition wrong?

查看:78
本文介绍了代码执行条件错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的基本问题,

我写了下面的代码块:

    IDEAL
    MODEL small
    STACK 100h
    DATASEG

    Var1 db 4
    Var2 db 2

    CODESEG

start:
    mov ax, @data
    mov ds, ax
    xor ax, ax
    mov al, [Var1]
    cmp al, [Var2]
    jg  Var1Greater
    mov ax, 1
Var1Greater:
    mov ax, 0

我是新来的.

我想创建一个比较[Var1]与[Var2]的代码.

I wanted to create a code that compares [Var1] to [Var2].

IF(!)[Var1]大于[Var2],执行mov ax, 1. IF(1)执行任何其他操作(等于或小于),mov ax, 0.

IF(!) [Var1] is greater than [Var2], execute mov ax, 1. IF(1) anything else(equal or less) excecute, mov ax, 0.

这怎么办?如果条件为真,则我编写的代码将同时执行这两个指令.

How can this be done? the code I wrote executes both instructions if the condition is true.

推荐答案

Ah,Turbo Assembler理想"模式;自从我上次看到它已经有一段时间了.我喜欢理想模式.与Microsoft Assembler的语法相比,这要好得多,而且要有意义得多.

Ah, Turbo Assembler "Ideal" mode; It has been a while since I last saw it. I love Ideal mode. It is so much better thought-out and it makes so much more sense than Microsoft Assembler's syntax.

好吧,正在发生的是 两者 指令都已执行.

Well, what is happening is that both instructions get executed.

首先,执行mov ax, 0,然后控制权转到下一个语句mov ax, 1,因此在ax中剩下的是1.

First, mov ax, 0 gets executed, and then control falls through to the next statement, which is mov ax, 1, so what you are left with in ax is 1.

汇编语言中的标签不会神奇地导致控件跳到其他地方.它们不会导致汇编程序发出任何指令.它们仅存在,以便您可以指示另一条跳转指令的目标.

Labels in assembly language do not magically cause control to jump elsewhere. They do not cause the assembler to emit any instructions. They only exist so that you can indicate the target of another jump instruction.

因此,您需要的是:

    ...
    cmp al, [Var2]
    jg  Var1Greater
    mov ax, 0
    jmp skip
Var1Greater:
    mov ax, 1
skip:

也是编写汇编语言时使用xor ax, ax而不是mov ax, 0的好形式.

also, it is good form when writing assembly language to use xor ax, ax instead of mov ax, 0.

这篇关于代码执行条件错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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