FranzVonHemmrich
FranzVonHemmrich

Reputation: 13

Assembly Language: Signed vs Unsigned Integer Representation using Hexadecimal

Given the following MASM assembly language code:

TITLE Move and add (AddSub.asm)
    INCLUDE Irvine32.inc
    INCLUDE macros.inc ; for memory dump

    .data
    x DWORD 0FFF4A348h ; summands
    y DWORD 00076540h
    z DWORD ? ; sum

    .code
    main PROC
    mov ebx,x
    mov eax,ebx
    mov ecx,y
    add eax,ecx
    mov z,eax
    call DumpRegs
    mDumpMem offset x,12,1 ; Display data area

    exit
main ENDP
END main

My understanding is that DWORD is a 32-bit unsigned data type. Once the entire program is run the eax register holds the hexadecimal value FFFC0888. There are two possible integer values that can result from this hexadecimal value, 4,294,707,336 or -259,960. According to my professor the correct Base 10 equivalent for this is -259,960. I'm a little confused on how I would identify this fact. If the initial data type is unsigned what is the giveaway that the integer result will be negative?

Upvotes: 1

Views: 2255

Answers (3)

kjp
kjp

Reputation: 51

The assignment details include one very important fact that clears ALL of the confusion about how to arrive at the answer sought: "All of the data being manipulated is 32-bit, two's complement integer data."

Upvotes: 0

John Sensebe
John Sensebe

Reputation: 1396

The CPU doesn't care if the operands are signed or unsigned when adding or subtracting integers, because you get the same result. That's why there are signed and unsigned versions of MUL and DIV, but not ADD and SUB, so the answer is whichever you want it to be. If your intention is to add two unsigned integers, the result should likewise be unsigned. If your intention is to add two signed integers, you should interpret the result as signed.

Upvotes: 3

Magoo
Magoo

Reputation: 79982

Simply, by convention, the most-significant bit being 1 means that if the number is being interpreted as a signed integer then it is negative. Exactly the same value can be interpreted as an unsigned integer. Two interpretations, two results.

Upvotes: 1

Related Questions