Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with arithmetic 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
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.
 
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!"
 
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."
 
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."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top