Code executes condition wrong?

后端 未结 2 1133
迷失自我
迷失自我 2020-11-30 15:49

Basic question here,

I wrote the following block:

    IDEAL
    MODEL small
    STACK 100h
    DATASEG

    Var1 db 4
    Var2 db 2

    CODESEG

sta         


        
相关标签:
2条回答
  • 2020-11-30 16:02

    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.

    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.

    So, what you need is:

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

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

    0 讨论(0)
  • 2020-11-30 16:22

    You must jump over Var1Greater too to skip mov ax, 1 instruction. As alternative you may do it like:

    mov ax, [Var1]
    cmp ax, [Var2]
    mov ax, 1
    jg  skip0
    mov ax, 0
    skip0:
    
    0 讨论(0)
提交回复
热议问题