Subtraction using 2's complement addition question

  • Thread starter Thread starter mindauggas
  • Start date Start date
  • Tags Tags
    Addition
Click For 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 :)
 
I’ve spent nearly my entire life online, and witnessed AI become integrated into our lives. It’s clear that AI is apart of us now whether we like it or not, unless your a anti tech cabin lover. AI has some form of control over your life. But what I’ve seen very recently is that people are loosing their ingenuity and deciding to use AI. I feel as if it’ll bleed into STEM which is kinda has already and, every idea or thought could become fully reliant on AI. Yeah AI makes life easier but at a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
5K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
859
  • · Replies 2 ·
Replies
2
Views
17K