Skip to content

Instantly share code, notes, and snippets.

@mandarjog
Created October 23, 2013 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandarjog/7112077 to your computer and use it in GitHub Desktop.
Save mandarjog/7112077 to your computer and use it in GitHub Desktop.
Testing with unsigned int
#include <stdio.h>
unsigned int sub(unsigned int previous, unsigned int next)
{
unsigned int res = 0xffffffff;
unsigned int diff = (next - previous);
res += diff;
res += 1;
// This code performs the following
// res = res + 0 + Cary_flag
// Cary_Flag is set by the last arithmatic instruction
// if res = res + diff resulted in an overflow Cary_flag will be 1
#if 0
__asm__ __volatile__("adc %%ebx, %%eax"
:"=a" (res)
:"a"(res), "b"(0)
);
#endif
return (res);
}
int check_equal(unsigned int expected, unsigned int actual)
{
if (expected != actual){
printf ("check failed!, expected=%x, actual=%x\n", expected, actual);
return (1);
}
return (0);
}
int main(int argc, char* argv[])
{
unsigned int prev;
unsigned int next;
unsigned int res;
unsigned int expected;
prev = 20; next = 21;
expected = 1;
res = sub(prev, next);
printf("prev=%u, next=%u, sub=%u, %x\n",prev, next, res, res);
prev = 21; next = 20;
check_equal(expected, res);
// this is a complete roll over
expected = 0xffffffff;
res = sub(prev, next);
printf("prev=%u, next=%u, sub=%u, %x\n",prev, next, res, res);
check_equal(expected, res);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment