David Daniels
David Daniels

Reputation: 131

Why is the comparing instruction not working?

So I have a simple assembly code in 8086 that compares the value in the variable value with 100 and if it's greater it prints out: "The value is greater than 100" and if not: "The value is not greater than 100".

Here is the code:

data segment
    string db "The value is larger$"
    value db 80
    right db "The value is greater than 100$"
    wrong db "The value is not greater than 100$"
ends

stack segment
    dw 128 dup(0)
ends

code segment
start:

    mov ax, data
    mov ds, ax
    mov es, ax

    lea ax, value
    cmp ax, 100d
    jae StatementWrong

StatementRight:
    lea dx, right
    jmp Ending

StatementWrong:
    lea dx, wrong

Ending:
    mov ah, 9
    int 21h

    mov ah, 1
    int 21h

    mov ax, 4c00h
    int 21h

ends

However the problem is no matter what value I have, whether it be larger or not than 100, the StatementRight segment is always being executed.

Any ideas on what might be causing the issue?

Upvotes: 2

Views: 406

Answers (2)

Shift_Left
Shift_Left

Reputation: 1243

Another issue is that you're declaring value as BYTE, but loading a WORD into AX. There are several solutions to this, but the simples one would be;

    cmp     al, 100

or

value:   dw   100

that way the high order of AX is zeroed and using CMP AX works.

Upvotes: 0

Fifoernik
Fifoernik

Reputation: 9899

lea ax, value
cmp ax, 100d
jae StatementWrong

3 instructions, all 3 wrong!

The lea instruction loads the address of the value variable. You need its content. You get this with either of next instructions:

mov al, value

or

mov al, [value]

Since your value variable is defined to be a byte, your code should also compare it as a byte.

cmp al, 100

The message that belongs to StatementWrong reads : "The value is not greater than 100". Why then do you jump to this message when the comparison produced ABOVE OR EQUAL ? That's contradictory.

jng StatementWrong

--------------------------------------------

In context (and improved a little!):

  mov  al, [value]
  cmp  al, 100
  lea  dx, right    ; "The value is greater than 100$"
  jg   Ending       ; JG means JUMP IF GREATER
  lea  dx, wrong    ; "The value is not greater than 100$"
Ending:
  mov  ah, 09h
  int  21h

Upvotes: 3

Related Questions