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

  • SEARCH
  • Home
  • 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 3594724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:45:10+00:00 2026-05-18T19:45:10+00:00

I have a little 8086 emulator and I’ve had a long standing bug for

  • 0

I have a little 8086 emulator and I’ve had a long standing bug for like 2 years now that AF does not behave properly inside of sub and add instructions.

My current way of computing its value is this for 8 bit numbers and subtraction:

uint8_t base=... , subt=...
base=base&0xF;
subt=subt&0xF; //isolate bottom nibble
if((int16_t)base-subt>7 || (int16_t)base-subt<-7){
    flags.af=1;
}else{
   flags.af=0;
}

(assuming an instruction like sub base,subt )

And for adding it’s like this:

uint8_t base=... , adder=...
base=base&0xF;
adder=adder&0xF; //isolate bottom nibble
if(base+adder>7 || base+adder<-7){
    flags.af=1;
}else{
   flags.af=0;
}

(for an instruction like add base,adder)

How do I properly calculate the AF flag in my emulator for such instructions?

  • 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-18T19:45:10+00:00Added an answer on May 18, 2026 at 7:45 pm
    flags.af = (((base-subt) & ~0xf) != 0);
    

    Checks to see if the upper bits are anything except zero, which would indicate an overflow or underflow out of the bottom 4 bits.

    Here’s a version that’s a little closer to your original. Note that the difference between two 4-bit quantities will never be greater than 15. Likewise the addition will never be less than 0.

    flags.af = ((int16_t)base-subt < -15);
    
    flags.af = ((int16_t)base+adder > 15);
    

    Putting parentheses around a boolean expression is just a style preference of mine, I know they’re redundant.

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

Sidebar

Related Questions

I have little doubt about string reading in C. string reading functions like gets,
I have little bit confusion regarding apache tomcat installation. So, I would like to
I have little Silverlight app that needs to make queries to server, is it
I have little problem with printing data like this, I have written script like
I have little knowledge on oracle. I tried to create a function like below.
I have little code like: using (WebClient wc = new WebClient()) { wc.Proxy =
I have little problem in my iPhone application code but can not identify. please
I have little notification divs that should appear above the navigation. I tried other
I have been working on this for a while now and, unfortunately have little
I am new to linked list, now I have little problems in population of

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.