2

Basic question here,

I wrote the following block:

    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

I'm new to assembly.

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

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

2 Answers2

5

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.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Hey Mike, I understand this happens. But what is the correct way for making the code skip on the "Var1Greater" block because condition of "JG" didn't met? – tilikompowaa Sep 30 '15 at 17:49
  • Well, user4419802 was quicker than me, and he already gave the answer for that. – Mike Nakis Sep 30 '15 at 17:53
  • user4419802's answer is correct. But if it does not fit your purpose, then what can I say, I amended my answer with a slightly different approach. – Mike Nakis Sep 30 '15 at 18:58
  • Why do you use `cmp ax, [var2]` since `var2` is defined as a byte? I'd probably expect your code to not compile. Maybe you meant `cmp al, [var2]` – Michael Petch Sep 30 '15 at 23:24
  • 1
    Ah you are correct, the OP did change the question a bit in that regard. Your edit now matches. So all is good. And I upvoted. – Michael Petch Sep 30 '15 at 23:59
  • 2
    The only foot note I can add to this all is that it is unclear if the OP knows that `jg` (jump greater) is a signed comparison and `ja` (jump above) is unsigned comparison. With `jg` if you make Var1=128 and Var2=127 they won't get the expected answer if they were assuming unsigned bytes. – Michael Petch Oct 01 '15 at 00:18
3

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:
Matt
  • 13,674
  • 1
  • 18
  • 27
  • but if i do `mov ax, 1`, ax = 3. this is not what i was trying to get in the code. – tilikompowaa Sep 30 '15 at 18:08
  • 1
    @tilikompowaa `mov` doesn't change `flags` register, so it's safe to put it between `cmp` and `jg`. – Matt Sep 30 '15 at 18:17
  • but still if a=3 so skip0 will execute. I got pretty much confused here. Im just trying to create a simple "if" condition. – tilikompowaa Sep 30 '15 at 18:31
  • 2
    @tilikompowaa `skip0:` is not an instruction. It's just a label, a point where execution resumes _after_ the whole "pseudo-if-operator" is done. – Matt Sep 30 '15 at 18:44
  • I have changed the code to be more accurate about my question. please check. – tilikompowaa Sep 30 '15 at 18:52