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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
Well, user4419802 was quicker than me, and he already gave the answer for that.
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.
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.
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.
|
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:

5 Comments

but if i do mov ax, 1, ax = 3. this is not what i was trying to get in the code.
@tilikompowaa mov doesn't change flags register, so it's safe to put it between cmp and jg.
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 skip0: is not an instruction. It's just a label, a point where execution resumes after the whole "pseudo-if-operator" is done.
I have changed the code to be more accurate about my question. please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.