Pixel
Pixel

Reputation: 31

Conditional Jump (jg) after comparison (cmp)

Here is a snippet of code that my book provided. The question asks whether the execution bx,1 or ax,10 will be executed. Looking at the code I want to say Ax,10 will be executed, an answer online says bx,1 will be executed, and my emulator says both are executed. Will someone help me understand what is really going on here?

mov cx,5
not cx
mov dx,10
cmp cx,dx
jg jump1
mov bx,1
jump1:
mov ax,10

Upvotes: 1

Views: 247

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61388

Let's trace. Before the CMP line the value of CX is -6 (the result of NOT on 5). The JG command performs a signed comparison - it doesn't treat negative numbers as large positive ones (JA and JB do).

So CX (-6) is not greater than DX (10), and the conditional jump is not taken. So both lines execute.

Upvotes: 1

Related Questions