Difference between revisions of "Subtraction"

From veswiki
Jump to: navigation, search
m (1 revision)
 
Line 1: Line 1:
 
Subtraction is one of the four basic arithmetic operations, where you take away a number from another number to get the result.
 
Subtraction is one of the four basic arithmetic operations, where you take away a number from another number to get the result.
  
Subtraction, as it applies to the [[Channel F]], doesn't exist as an [[opcode]]. However, the result of subtracting a number can be given by using the [[complement|two's complement]] of the number, or the inverse of the number plus one. The side effect of this is that the carry is set; however, since we're using 8-bit numbers, the 8-bit number will be the same as if we subtracted the two values, and the carry can be ignored.
+
Subtraction, as it applies to the [[Channel F]], doesn't exist as an [[opcode]]. However, the result of subtracting a number can be given by using the [[complement|two's complement]] of the number, which means the inverse of the number plus one. The side effect of this is that the carry is set; however, since we're using 8-bit numbers, the 8-bit number will be the same as if we subtracted the two values, and the carry can be ignored.
  
 
To subtract an immediate value, just [[addition|add]] the two's complement:
 
To subtract an immediate value, just [[addition|add]] the two's complement:
Line 7: Line 7:
  
 
<pre>
 
<pre>
lr A, 4
+
lr A, 4           ; load A from register 4 (or any other)
ai %11110110 ; subtract 10
+
ai %11110110 ; subtract 10, result is in A
 
</pre>
 
</pre>
  
10 in binary is %00001010, so the two's complement would be %11110110. If you want to subtract two arbitrary numbers, however, there is a little more work involved. Take, for instance, the folowing situation:
+
10 in binary is %00001010, so the inverse is %11110101 and then add one and you have the two's complement. If you want to subtract two arbitrary numbers, however, there is a little more work involved. Take, for instance, the following situation:
  
 
<pre>
 
<pre>
Line 18: Line 18:
 
</pre>
 
</pre>
  
You want to subtract r5 from r4, and then use the result. Since we don't know the value of r5, we can't use an immediate value. However, we can get the two's complement while running the program, like so:
+
You want to subtract r5 from r4 (minuend (r4) − subtrahend (r5)) and then use the result. Since we don't know the value of r5, we can't use an immediate value but we can get the two's complement and add the two together. As we need to process the subtrahend - let's start with that:
  
 
<pre>
 
<pre>
Line 24: Line 24:
 
com ; get the inverse of the number
 
com ; get the inverse of the number
 
inc ; add 1 (two's complement)
 
inc ; add 1 (two's complement)
as 4 ; add it to r4
+
as 4 ; add it to r4, result is saved in r4
  
 
</pre>
 
</pre>
  
Just remember, you need to get the two's complement of the number you're subtracting, not the one to be subtracted ''from''.
+
Just remember, you need to get the two's complement of the subtrahend.
 +
Other methods are possible as well, if the result is needed in r5 instead then two's complement can be saved in r5 and then r4 could be loaded into A and added to register 5 - but it requires more code and is of course slower.

Latest revision as of 22:51, 16 November 2012

Subtraction is one of the four basic arithmetic operations, where you take away a number from another number to get the result.

Subtraction, as it applies to the Channel F, doesn't exist as an opcode. However, the result of subtracting a number can be given by using the two's complement of the number, which means the inverse of the number plus one. The side effect of this is that the carry is set; however, since we're using 8-bit numbers, the 8-bit number will be the same as if we subtracted the two values, and the carry can be ignored.

To subtract an immediate value, just add the two's complement:


lr	A, 4            ; load A from register 4 (or any other)
ai	%11110110	; subtract 10, result is in A

10 in binary is %00001010, so the inverse is %11110101 and then add one and you have the two's complement. If you want to subtract two arbitrary numbers, however, there is a little more work involved. Take, for instance, the following situation:

lr	A, 4
; subtract r5

You want to subtract r5 from r4 (minuend (r4) − subtrahend (r5)) and then use the result. Since we don't know the value of r5, we can't use an immediate value but we can get the two's complement and add the two together. As we need to process the subtrahend - let's start with that:

lr	A, 5
com			; get the inverse of the number
inc			; add 1 (two's complement)
as	4		; add it to r4, result is saved in r4

Just remember, you need to get the two's complement of the subtrahend. Other methods are possible as well, if the result is needed in r5 instead then two's complement can be saved in r5 and then r4 could be loaded into A and added to register 5 - but it requires more code and is of course slower.