Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3303532
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:58:00+00:00 2026-05-17T20:58:00+00:00

I am new to x86 assembly language, I have a signed integer saved in

  • 0

I am new to x86 assembly language, I have a signed integer saved in register eax, and I want to check if the number is negative or positive. To do that, I used bt instruction to check the first bit.

Here is what I did:

bt eax,0
jnc isNegative

bt loads the first bit to carry flag, and I used jnc to check if carry flag is 0 or 1.

If it’s 1, it should be a negative number, and does negative instructions…
however, the output is unpredictable, sometimes I have a positive and it recognize it as a negative number. Am I doing something wrong?

EDIT: I just realized it could have something to do with bit numbering. It is actually checking the least significant bit instead of the first bit. Let me try using bt eax, 31

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:58:01+00:00Added an answer on May 17, 2026 at 8:58 pm

    The value is negative if the MSB is set. The SF (sign flag) in FLAGS is set according to that bit of the result.

    test eax, eax      ; set FLAGS according to the value in EAX; like cmp eax, 0
    js   negative      ; jump if top bit was set in EAX
    

    Alternatively since test reg,reg sets FLAGS the same way as cmp reg,0, you could use jl to jump if the value is "less than zero". That’s the same thing. Fun fact: this also works as a trick for optimizing unsigned if(eax >= 0x80000000U).

    You can manually do it as a single-bit test (not taking advantage of SF) with a less efficient instruction (longer because it has to include the 32-bit constant.) This is a lot like your bt eax,31 but setting ZF=0 instead of CF=1 when the sign bit is set.

    test eax, 0x80000000      ; set flags according to EAX & (1<<31)
    jnz  negative             ; jump if that result was non-zero, i.e. bit was set
    

    This works for any register size, byte (int8_t), word (int16_t), dword (int32_t), or qword (int64_t). For example with bytes:

    test al, al
    js   is_negative
    ; or
    test al, 80h
    jne  is_negative
    

    If the value in a register was produced by something that sets FLAGS "according to the result", like most ALU instructions such as add ecx, edx, you can js to see if it’s negative or not without needing to test ecx, ecx first. SF is already set according to the MSB of the value in ECX, by the add instruction.


    Terminology: positive doesn’t include zero

    • positive is x > 0. Numbers 1 or greater, not including zero. (Sometimes used loosely as the opposite of negative, but strictly speaking it’s not.)
    • non-negative is x >= 0.
    • negative is x < 0.
    • zero is not positive or negative. Its 2’s complement bit-pattern is all-zero, no bits set. (x86 uses 2’s complement signed integers, like all other modern CPUs. That’s why there’s no integer negative-zero.)

    So if you want to find out if a number is positive or negative, that’s two separate checks unless you already know it’s non-zero. (Assuming you’re using a strict definition of positive. Sometimes from context you can infer that someone means one condition, but precise terminology is useful in computing so prefer that yourself.)


    Your bt idea could work, but x86 numbers bits from bit 0 at the bottom (least significant), to bit 31 as the sign-bit of a 32-bit register like EAX. Also, CF set means negative, so jc negative. But bt isn’t faster unless you actually want the condition in the carry flag for use with adc edx, 0 to do negcount += (x<0) counting negative numbers or something. Alternatively, add eax,eax or shl eax,1 will shift the top bit of EAX into CF (and also modify EAX, unlike BT).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New class is a subclass of the original object It needs to be php4
New to javascript/jquery and having a hard time with using this or $(this) to
New to xml. Looking for XPath to search a xml file with python ElementTree
New to silverlight. Traditionally if I were to design a wizard-like process where a
New to both Ruby and Rails but I'm book educated by now (which apparently
New to WCF, but familiar with COM+ - can I wrap a WCF service
New to Linux programming in general. I am trying to communicate with a kernel
The new extensions in .Net 3.5 allow functionality to be split out from interfaces.
In new C++ code, I tend to use the C++ iostream library instead of
Being new to test based development, this question has been bugging me. How much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.