×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

need help with arithmetic

need help with arithmetic

need help with arithmetic

(OP)
Im using Masm v6.13 for the pentium and new to the language.Can
someone show me how to add and subtract negative
numbers in assemble? I've heard in order to subtract
negative numbers with positive numbers, one has to put the negative in two's complement? How do I do that?thankx i advance.

RE: need help with arithmetic

hello,
basic elementry mathematics, you shouldnt have a problem with this.

twos compliment is used to convert a signed integer into an unsigned integer that is to be treated as a negative number. it was only used in the early days of processors but today the processors have all this functionality built in.

the add and sub operations will work as expected just like a calculator ie 1 sub -1 = 2 etc

here is an example of a signed integer, all numbers are in byte length. a signed byte can range from -128 to 127 using bit 7 as the sign.

binary     hex   decimal
00000001b  001h  +1
11111111b  0FFh  -1

to perform twos compliment invert all bits then add 1 or zero minus the number. twos compliments can be performed as many times as you like and it wont affect the absolute value.

the 80x86 provides an operation for twos compliment

    neg al

addition & subtraction:
when the operation and the sign are the same it is always an add and when they are opposite it is always a subtract.

 +1 add +1 = +2
 +1 add -1 = 0
 +1 sub +1 = 0
 +1 sub -1 = +2

 -1 add +1 = 0
 -1 add -1 = -2
 -1 sub +1 = -2
 -1 sub -1 = 0

points to note:
to perform twos compliment on a signed negative integer and then subtract it is the same as adding the signed negative integer.
only use the subtract operation if you are working with unsigned integers that are to be treated as negative numbers.

after operation
SF=the sign of the result
CF=if the result size is greater then the operand size

if you have to extend an integer, a zero for positive integer and 0FFh for negative will not affect the signed integers value.

"People who have nothing to say, say it too loud and have little knowledge. It's the quiet ones you need to worry about!"

RE: need help with arithmetic

Two's complement is completely natural to the processor, which is why it is so cool.  Any time you do something like 31-32 (=-1), the processor will wrap around to 0ffh (8-bit), 0ffffh (16-bit), 0ffffffffh (32-bit), which is two's complement for -1.  Subtract one again and you get 0feh or 0fffeh or 0fffffffeh, which is two's complement for -2.

In fact in many cases you don't really have to do two's complement except when you need to display a number (rather difficult in Asm really, in comparison to other languages).  And if ever you need to do two's complement for a number, you can always use the 'neg' instruction.



"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

RE: need help with arithmetic

straiph, looks like you beat me to it...



"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close