#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    [Assembly] signed math


    Hi,

    I'm taking my first steps in assembly and I'm a bit confused over signed versus unsigned math. I'm debugging a simple executable which at one point does a subtraction.

    Before the operation, this is what AX and BX look like:

    AX CF8F
    BX 2300

    After "sub ax, bx" the sign flag is set and AX becomes

    AX AC8F

    When thinking in unsigned numbers I understand what is happening.

    53135 - 8960 = 44175

    However, when I'm trying to understand what happens in terms of signed numbers, I'm a bit confused.

    AX CF8F = 1100 1111 1000 1111 = -100 1111 1000 1111 = -20367
    BX 2300 = 0010 0011 0000 0000 = +010 0011 0000 0000 = 8960

    -20367 - 8960 = -29327 = -111 0010 1000 1111 = 1111 0010 1000 1111 = F28F

    So obviously there is something wrong with my reasoning. Who can help me out?

    Thanks
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Are AX and BX 16 or 32 bit registers?

    Edit:
    Also, are you sure the architecture uses sign magnitude instead of 2's complement? If it's 2's complement then
    1100 1111 1000 1111 = - 011 0000 0111 0001 = -12,401
    Last edited by E-Oreo; October 16th, 2009 at 04:20 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0
    The registers are 16 bit. The architecture is x86, I'm using Windows XP on a virtual machine.

    I'll look into 2's complement, I wasn't aware of this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0
    Ok, it's clear to me now. Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo