`js` and `jb` instructions in assembly
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am having a hard time understanding what exactly js
and jb
instruction do. I understand that jb
is jump if below. But, what would be the difference between jb
and jle
. And similarly, js
seems to me that it is equivalent to jb
, as it means jump if signed. Any help would be appreciated.
assembly x86 att
add a comment |
I am having a hard time understanding what exactly js
and jb
instruction do. I understand that jb
is jump if below. But, what would be the difference between jb
and jle
. And similarly, js
seems to me that it is equivalent to jb
, as it means jump if signed. Any help would be appreciated.
assembly x86 att
js
tests the Sign flag, andjb
tests the Carry flag.jle
is more complex and your assembler / processor text books are the place to begin. One uses a different set of flag tests for signed and unsigned arithmetic, as the processor does not (usually) distinguish the two.
– Weather Vane
Nov 23 '18 at 19:01
2
The instruction set reference clearly shows which flags each test.js
examines the sign flag so you can check for negative numbers.jb
is for unsigned comparison as it uses the carry flag.jl
is for signed comparison which handles overflow as well. In the given codejs
just checks whether the MSB ofeax
is set or not (meaning it's negative if viewed as signed). Thejb
is just doing an unsigned less-than.
– Jester
Nov 23 '18 at 19:02
1
felixcloutier.com/x86/Jcc.html
– Peter Cordes
Nov 23 '18 at 19:04
The conditional jumps check the flags (eflag
register), they are not aware of which instruction did produce the flag. I.e.stc
jb somewhere
will take the branch tosomewhere
always, becausestc
is "set carry flag" - yet it doesn't have anything to do with human concept of "below" term (and reasonable programmer would rather write thejc
alias there to not confuse reader by "below", but make it clear that he is interested into "carry" status).
– Ped7g
Nov 23 '18 at 22:43
add a comment |
I am having a hard time understanding what exactly js
and jb
instruction do. I understand that jb
is jump if below. But, what would be the difference between jb
and jle
. And similarly, js
seems to me that it is equivalent to jb
, as it means jump if signed. Any help would be appreciated.
assembly x86 att
I am having a hard time understanding what exactly js
and jb
instruction do. I understand that jb
is jump if below. But, what would be the difference between jb
and jle
. And similarly, js
seems to me that it is equivalent to jb
, as it means jump if signed. Any help would be appreciated.
assembly x86 att
assembly x86 att
edited Nov 24 '18 at 1:11
konstant
asked Nov 23 '18 at 18:55
konstantkonstant
391214
391214
js
tests the Sign flag, andjb
tests the Carry flag.jle
is more complex and your assembler / processor text books are the place to begin. One uses a different set of flag tests for signed and unsigned arithmetic, as the processor does not (usually) distinguish the two.
– Weather Vane
Nov 23 '18 at 19:01
2
The instruction set reference clearly shows which flags each test.js
examines the sign flag so you can check for negative numbers.jb
is for unsigned comparison as it uses the carry flag.jl
is for signed comparison which handles overflow as well. In the given codejs
just checks whether the MSB ofeax
is set or not (meaning it's negative if viewed as signed). Thejb
is just doing an unsigned less-than.
– Jester
Nov 23 '18 at 19:02
1
felixcloutier.com/x86/Jcc.html
– Peter Cordes
Nov 23 '18 at 19:04
The conditional jumps check the flags (eflag
register), they are not aware of which instruction did produce the flag. I.e.stc
jb somewhere
will take the branch tosomewhere
always, becausestc
is "set carry flag" - yet it doesn't have anything to do with human concept of "below" term (and reasonable programmer would rather write thejc
alias there to not confuse reader by "below", but make it clear that he is interested into "carry" status).
– Ped7g
Nov 23 '18 at 22:43
add a comment |
js
tests the Sign flag, andjb
tests the Carry flag.jle
is more complex and your assembler / processor text books are the place to begin. One uses a different set of flag tests for signed and unsigned arithmetic, as the processor does not (usually) distinguish the two.
– Weather Vane
Nov 23 '18 at 19:01
2
The instruction set reference clearly shows which flags each test.js
examines the sign flag so you can check for negative numbers.jb
is for unsigned comparison as it uses the carry flag.jl
is for signed comparison which handles overflow as well. In the given codejs
just checks whether the MSB ofeax
is set or not (meaning it's negative if viewed as signed). Thejb
is just doing an unsigned less-than.
– Jester
Nov 23 '18 at 19:02
1
felixcloutier.com/x86/Jcc.html
– Peter Cordes
Nov 23 '18 at 19:04
The conditional jumps check the flags (eflag
register), they are not aware of which instruction did produce the flag. I.e.stc
jb somewhere
will take the branch tosomewhere
always, becausestc
is "set carry flag" - yet it doesn't have anything to do with human concept of "below" term (and reasonable programmer would rather write thejc
alias there to not confuse reader by "below", but make it clear that he is interested into "carry" status).
– Ped7g
Nov 23 '18 at 22:43
js
tests the Sign flag, and jb
tests the Carry flag. jle
is more complex and your assembler / processor text books are the place to begin. One uses a different set of flag tests for signed and unsigned arithmetic, as the processor does not (usually) distinguish the two.– Weather Vane
Nov 23 '18 at 19:01
js
tests the Sign flag, and jb
tests the Carry flag. jle
is more complex and your assembler / processor text books are the place to begin. One uses a different set of flag tests for signed and unsigned arithmetic, as the processor does not (usually) distinguish the two.– Weather Vane
Nov 23 '18 at 19:01
2
2
The instruction set reference clearly shows which flags each test.
js
examines the sign flag so you can check for negative numbers. jb
is for unsigned comparison as it uses the carry flag. jl
is for signed comparison which handles overflow as well. In the given code js
just checks whether the MSB of eax
is set or not (meaning it's negative if viewed as signed). The jb
is just doing an unsigned less-than.– Jester
Nov 23 '18 at 19:02
The instruction set reference clearly shows which flags each test.
js
examines the sign flag so you can check for negative numbers. jb
is for unsigned comparison as it uses the carry flag. jl
is for signed comparison which handles overflow as well. In the given code js
just checks whether the MSB of eax
is set or not (meaning it's negative if viewed as signed). The jb
is just doing an unsigned less-than.– Jester
Nov 23 '18 at 19:02
1
1
felixcloutier.com/x86/Jcc.html
– Peter Cordes
Nov 23 '18 at 19:04
felixcloutier.com/x86/Jcc.html
– Peter Cordes
Nov 23 '18 at 19:04
The conditional jumps check the flags (
eflag
register), they are not aware of which instruction did produce the flag. I.e. stc
jb somewhere
will take the branch to somewhere
always, because stc
is "set carry flag" - yet it doesn't have anything to do with human concept of "below" term (and reasonable programmer would rather write the jc
alias there to not confuse reader by "below", but make it clear that he is interested into "carry" status).– Ped7g
Nov 23 '18 at 22:43
The conditional jumps check the flags (
eflag
register), they are not aware of which instruction did produce the flag. I.e. stc
jb somewhere
will take the branch to somewhere
always, because stc
is "set carry flag" - yet it doesn't have anything to do with human concept of "below" term (and reasonable programmer would rather write the jc
alias there to not confuse reader by "below", but make it clear that he is interested into "carry" status).– Ped7g
Nov 23 '18 at 22:43
add a comment |
2 Answers
2
active
oldest
votes
There exists a handy table that does explain very well which Jcc
instruction to use:
Jump conditions and flags:
Mnemonic Condition tested Description
jo OF = 1 overflow
jno OF = 0 not overflow
jc, jb, jnae CF = 1 carry / below / not above nor equal
jnc, jae, jnb CF = 0 not carry / above or equal / not below
je, jz ZF = 1 equal / zero
jne, jnz ZF = 0 not equal / not zero
jbe, jna CF or ZF = 1 below or equal / not above
ja, jnbe CF or ZF = 0 above / not below or equal
js SF = 1 sign
jns SF = 0 not sign
jp, jpe PF = 1 parity / parity even
jnp, jpo PF = 0 not parity / parity odd
jl, jnge SF xor OF = 1 less / not greater nor equal
jge, jnl SF xor OF = 0 greater or equal / not less
jle, jng (SF xor OF) or ZF = 1 less or equal / not greater
jg, jnle (SF xor OF) or ZF = 0 greater / not less nor equal
add a comment |
jb
(and ja
) branch based on the unsigned result of the flags, as opposed to the signed branch condition for jg
, jge
, jl
, and jle
.
In an unsigned comparison, the MSB is included as part of the number itself and not an indication of its sign. For example:
; Intel ; ; AT&T
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jl mybranch ; branch taken ; jl mybranch ; branch taken
Whereas:
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jb mybranch ; branch not taken ; jb mybranch ; branch not taken
js
will branch based solely on the state of the sign flag in the (R|E)FLAGS
register
1
The OP is using GAS, which only supports0x...
syntax for hex, not DOS-style0...h
. (And they have it in AT&T syntax mode, not.intel_syntax noprefix
).
– Peter Cordes
Nov 23 '18 at 19:11
@PeterCordes I was going to edit my post but it looks like some one beat me to it :)
– Govind Parmar
Nov 23 '18 at 20:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451732%2fjs-and-jb-instructions-in-assembly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There exists a handy table that does explain very well which Jcc
instruction to use:
Jump conditions and flags:
Mnemonic Condition tested Description
jo OF = 1 overflow
jno OF = 0 not overflow
jc, jb, jnae CF = 1 carry / below / not above nor equal
jnc, jae, jnb CF = 0 not carry / above or equal / not below
je, jz ZF = 1 equal / zero
jne, jnz ZF = 0 not equal / not zero
jbe, jna CF or ZF = 1 below or equal / not above
ja, jnbe CF or ZF = 0 above / not below or equal
js SF = 1 sign
jns SF = 0 not sign
jp, jpe PF = 1 parity / parity even
jnp, jpo PF = 0 not parity / parity odd
jl, jnge SF xor OF = 1 less / not greater nor equal
jge, jnl SF xor OF = 0 greater or equal / not less
jle, jng (SF xor OF) or ZF = 1 less or equal / not greater
jg, jnle (SF xor OF) or ZF = 0 greater / not less nor equal
add a comment |
There exists a handy table that does explain very well which Jcc
instruction to use:
Jump conditions and flags:
Mnemonic Condition tested Description
jo OF = 1 overflow
jno OF = 0 not overflow
jc, jb, jnae CF = 1 carry / below / not above nor equal
jnc, jae, jnb CF = 0 not carry / above or equal / not below
je, jz ZF = 1 equal / zero
jne, jnz ZF = 0 not equal / not zero
jbe, jna CF or ZF = 1 below or equal / not above
ja, jnbe CF or ZF = 0 above / not below or equal
js SF = 1 sign
jns SF = 0 not sign
jp, jpe PF = 1 parity / parity even
jnp, jpo PF = 0 not parity / parity odd
jl, jnge SF xor OF = 1 less / not greater nor equal
jge, jnl SF xor OF = 0 greater or equal / not less
jle, jng (SF xor OF) or ZF = 1 less or equal / not greater
jg, jnle (SF xor OF) or ZF = 0 greater / not less nor equal
add a comment |
There exists a handy table that does explain very well which Jcc
instruction to use:
Jump conditions and flags:
Mnemonic Condition tested Description
jo OF = 1 overflow
jno OF = 0 not overflow
jc, jb, jnae CF = 1 carry / below / not above nor equal
jnc, jae, jnb CF = 0 not carry / above or equal / not below
je, jz ZF = 1 equal / zero
jne, jnz ZF = 0 not equal / not zero
jbe, jna CF or ZF = 1 below or equal / not above
ja, jnbe CF or ZF = 0 above / not below or equal
js SF = 1 sign
jns SF = 0 not sign
jp, jpe PF = 1 parity / parity even
jnp, jpo PF = 0 not parity / parity odd
jl, jnge SF xor OF = 1 less / not greater nor equal
jge, jnl SF xor OF = 0 greater or equal / not less
jle, jng (SF xor OF) or ZF = 1 less or equal / not greater
jg, jnle (SF xor OF) or ZF = 0 greater / not less nor equal
There exists a handy table that does explain very well which Jcc
instruction to use:
Jump conditions and flags:
Mnemonic Condition tested Description
jo OF = 1 overflow
jno OF = 0 not overflow
jc, jb, jnae CF = 1 carry / below / not above nor equal
jnc, jae, jnb CF = 0 not carry / above or equal / not below
je, jz ZF = 1 equal / zero
jne, jnz ZF = 0 not equal / not zero
jbe, jna CF or ZF = 1 below or equal / not above
ja, jnbe CF or ZF = 0 above / not below or equal
js SF = 1 sign
jns SF = 0 not sign
jp, jpe PF = 1 parity / parity even
jnp, jpo PF = 0 not parity / parity odd
jl, jnge SF xor OF = 1 less / not greater nor equal
jge, jnl SF xor OF = 0 greater or equal / not less
jle, jng (SF xor OF) or ZF = 1 less or equal / not greater
jg, jnle (SF xor OF) or ZF = 0 greater / not less nor equal
answered Nov 23 '18 at 19:57
zx485zx485
15.4k133248
15.4k133248
add a comment |
add a comment |
jb
(and ja
) branch based on the unsigned result of the flags, as opposed to the signed branch condition for jg
, jge
, jl
, and jle
.
In an unsigned comparison, the MSB is included as part of the number itself and not an indication of its sign. For example:
; Intel ; ; AT&T
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jl mybranch ; branch taken ; jl mybranch ; branch taken
Whereas:
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jb mybranch ; branch not taken ; jb mybranch ; branch not taken
js
will branch based solely on the state of the sign flag in the (R|E)FLAGS
register
1
The OP is using GAS, which only supports0x...
syntax for hex, not DOS-style0...h
. (And they have it in AT&T syntax mode, not.intel_syntax noprefix
).
– Peter Cordes
Nov 23 '18 at 19:11
@PeterCordes I was going to edit my post but it looks like some one beat me to it :)
– Govind Parmar
Nov 23 '18 at 20:48
add a comment |
jb
(and ja
) branch based on the unsigned result of the flags, as opposed to the signed branch condition for jg
, jge
, jl
, and jle
.
In an unsigned comparison, the MSB is included as part of the number itself and not an indication of its sign. For example:
; Intel ; ; AT&T
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jl mybranch ; branch taken ; jl mybranch ; branch taken
Whereas:
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jb mybranch ; branch not taken ; jb mybranch ; branch not taken
js
will branch based solely on the state of the sign flag in the (R|E)FLAGS
register
1
The OP is using GAS, which only supports0x...
syntax for hex, not DOS-style0...h
. (And they have it in AT&T syntax mode, not.intel_syntax noprefix
).
– Peter Cordes
Nov 23 '18 at 19:11
@PeterCordes I was going to edit my post but it looks like some one beat me to it :)
– Govind Parmar
Nov 23 '18 at 20:48
add a comment |
jb
(and ja
) branch based on the unsigned result of the flags, as opposed to the signed branch condition for jg
, jge
, jl
, and jle
.
In an unsigned comparison, the MSB is included as part of the number itself and not an indication of its sign. For example:
; Intel ; ; AT&T
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jl mybranch ; branch taken ; jl mybranch ; branch taken
Whereas:
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jb mybranch ; branch not taken ; jb mybranch ; branch not taken
js
will branch based solely on the state of the sign flag in the (R|E)FLAGS
register
jb
(and ja
) branch based on the unsigned result of the flags, as opposed to the signed branch condition for jg
, jge
, jl
, and jle
.
In an unsigned comparison, the MSB is included as part of the number itself and not an indication of its sign. For example:
; Intel ; ; AT&T
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jl mybranch ; branch taken ; jl mybranch ; branch taken
Whereas:
mov eax, 08000000h ; mov $0x8000000, %eax
mov ecx, 00000001h ; mov $0x0000001, %ecx
cmp eax, ecx ; cmp %ecx, %eax
jb mybranch ; branch not taken ; jb mybranch ; branch not taken
js
will branch based solely on the state of the sign flag in the (R|E)FLAGS
register
edited Nov 23 '18 at 20:02
Matteo Italia
103k15150253
103k15150253
answered Nov 23 '18 at 19:09
Govind ParmarGovind Parmar
13.1k53764
13.1k53764
1
The OP is using GAS, which only supports0x...
syntax for hex, not DOS-style0...h
. (And they have it in AT&T syntax mode, not.intel_syntax noprefix
).
– Peter Cordes
Nov 23 '18 at 19:11
@PeterCordes I was going to edit my post but it looks like some one beat me to it :)
– Govind Parmar
Nov 23 '18 at 20:48
add a comment |
1
The OP is using GAS, which only supports0x...
syntax for hex, not DOS-style0...h
. (And they have it in AT&T syntax mode, not.intel_syntax noprefix
).
– Peter Cordes
Nov 23 '18 at 19:11
@PeterCordes I was going to edit my post but it looks like some one beat me to it :)
– Govind Parmar
Nov 23 '18 at 20:48
1
1
The OP is using GAS, which only supports
0x...
syntax for hex, not DOS-style 0...h
. (And they have it in AT&T syntax mode, not .intel_syntax noprefix
).– Peter Cordes
Nov 23 '18 at 19:11
The OP is using GAS, which only supports
0x...
syntax for hex, not DOS-style 0...h
. (And they have it in AT&T syntax mode, not .intel_syntax noprefix
).– Peter Cordes
Nov 23 '18 at 19:11
@PeterCordes I was going to edit my post but it looks like some one beat me to it :)
– Govind Parmar
Nov 23 '18 at 20:48
@PeterCordes I was going to edit my post but it looks like some one beat me to it :)
– Govind Parmar
Nov 23 '18 at 20:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451732%2fjs-and-jb-instructions-in-assembly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
js
tests the Sign flag, andjb
tests the Carry flag.jle
is more complex and your assembler / processor text books are the place to begin. One uses a different set of flag tests for signed and unsigned arithmetic, as the processor does not (usually) distinguish the two.– Weather Vane
Nov 23 '18 at 19:01
2
The instruction set reference clearly shows which flags each test.
js
examines the sign flag so you can check for negative numbers.jb
is for unsigned comparison as it uses the carry flag.jl
is for signed comparison which handles overflow as well. In the given codejs
just checks whether the MSB ofeax
is set or not (meaning it's negative if viewed as signed). Thejb
is just doing an unsigned less-than.– Jester
Nov 23 '18 at 19:02
1
felixcloutier.com/x86/Jcc.html
– Peter Cordes
Nov 23 '18 at 19:04
The conditional jumps check the flags (
eflag
register), they are not aware of which instruction did produce the flag. I.e.stc
jb somewhere
will take the branch tosomewhere
always, becausestc
is "set carry flag" - yet it doesn't have anything to do with human concept of "below" term (and reasonable programmer would rather write thejc
alias there to not confuse reader by "below", but make it clear that he is interested into "carry" status).– Ped7g
Nov 23 '18 at 22:43