Subtraction using 2's complement addition question

  • Thread starter Thread starter mindauggas
  • Start date Start date
  • Tags Tags
    Addition
AI Thread Summary
The discussion focuses on implementing subtraction using 2's complement addition in Intel x86 assembly language without using the SUB instruction. The provided code snippet involves moving data from memory location 51 to the EDX register, negating it to obtain the 2's complement, and then adding the value from memory location 50. The result is stored in memory location 101. There is a consensus that the reference to "52" in the explanation is a typo, as it should refer to the contents of memory location 50. Additionally, the code exhibits inconsistencies regarding operand order, which should follow the format of <destination>, <source> in Intel x86 assembly. The discussion also highlights that EDX is a 4-byte register, indicating potential overlap in memory usage between locations 50 and 51.
mindauggas
Messages
127
Reaction score
0
The problem states: "What Intel x86 instructions would you use to accomplish subtraction using 2’s complement addition? This instruction set has a SUB instruction, but don’t use that; write your own 2’s complement routine instead".

Then the book states the answer:

MOV 51, EDX // copy what’s in 51 to the register
NEG EDX // take 2s complement of register
ADD EDX, 50 // subtract contents of 51 from 52
MOV EDX, 101 // store the result in 101


However it is hard for me to understand waht does this allgorithm do, in particular line 3: it takes some value to the register, take's 2's complement then it adds. But what does the 50 mean? Can anyone explain line 3?
 
Computer science news on Phys.org
when it says what's in 51 it means memory location 51

if your memory was represented by an array in a higher language like java then these statements are equivalent to:

edx=mem[51]; // load edx with the data stored in location 51
edx=-edx; // negate it
edx=edx+mem[50]; // adding in the data stored in location 50
mem[101]=edx; // store the answer in location 101
 
jedishrfu said:
when it says what's in 51 it means memory location 51

if your memory was represented by an array in a higher language like java then these statements are equivalent to:

edx=mem[51]; // load edx with the data stored in location 51
edx=-edx; // negate it
edx=edx+mem[50]; // adding in the data stored in location 50
mem[101]=edx; // store the answer in location 101

Yes, it seems that I have understood this correctly. The thing I don't understand is the comment in the book in line 3: " subtract contents of 51 from 52" - the "52" should be considered a typo you think?
 
mindauggas said:
Yes, it seems that I have understood this correctly. The thing I don't understand is the comment in the book in line 3: " subtract contents of 51 from 52" - the "52" should be considered a typo you think?
Yes, the 52 is a typo, and also the code is inconsistent in which operand is source and which operand is destination.

For Intel and Microsoft X86 assemblers, the order for a two operand instruction is <destination>, <source>. Also memory reference operands using absolute address will normally specify a segment register, such as ds:0100h. The brackets are optional, such as ds:[0100h].

In addition, EDX is a 4 byte register, so loading and storing at 50 and 51 mean that the operands are overlapping in memory.
 
Last edited:
mindauggas said:
The problem states: "What Intel x86 instructions would you use to accomplish subtraction using 2’s complement addition? This instruction set has a SUB instruction, but don’t use that; write your own 2’s complement routine instead".

Then the book states the answer:

MOV 51, EDX // copy what’s in 51 to the register
NEG EDX // take 2s complement of register
ADD EDX, 50 // subtract contents of 51 from 52
MOV EDX, 101 // store the result in 101
Whoever wrote this code is not very familiar with the Intel x86 instruction set.
 
rcgldr said:
Yes, the 52 is a typo, and also the code is inconsistent in which operand is source and which operand is destination.

For Intel and Microsoft X86 assemblers, the order for a two operand instruction is <destination>, <source>. Also memory reference operands using absolute address will normally specify a segment register, such as ds:0100h. The brackets are optional, such as ds:[0100h].

In addition, EDX is a 4 byte register, so loading and storing at 50 and 51 mean that the operands are overlapping in memory.

Thanks, that was really helpfull :)
 
Well, the date has now passed, and Windows 10 is no longer supported. Hopefully, the readers of this forum have done one of the many ways this issue can be handled. If not, do a YouTube search and a smorgasbord of solutions will be returned. What I want to mention is that I chose to use a debloated Windows from a debloater. There are many available options, e.g., Chris Titus Utilities (I used a product called Velotic, which also features AI to prevent your computer from overheating etc...
Back
Top