Subtraction using 2's complement addition question

  • Thread starter Thread starter mindauggas
  • Start date Start date
  • Tags Tags
    Addition
Click For Summary

Discussion Overview

The discussion revolves around the implementation of subtraction using 2's complement addition in Intel x86 assembly language. Participants analyze a provided code snippet and seek clarification on its functionality, particularly regarding the use of memory locations and the correctness of comments in the code.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions the meaning of the instruction that adds the contents of memory location 50 after negating the value from memory location 51, seeking clarification on the purpose of the number 50.
  • Another participant interprets the instructions in a higher-level language context, suggesting that the operations correspond to loading, negating, and adding values from specified memory locations.
  • There is a suggestion that the comment in the book regarding "subtract contents of 51 from 52" is likely a typo, as the code references memory location 50 instead.
  • Concerns are raised about the inconsistency in the operand order in the code, noting that the destination and source operands are not clearly defined according to Intel x86 conventions.
  • One participant expresses skepticism about the author's familiarity with the Intel x86 instruction set, implying that the code may not be correctly structured.
  • Another participant points out that the overlapping memory locations (50 and 51) could lead to issues due to the size of the EDX register.

Areas of Agreement / Disagreement

Participants generally agree that the comment about "52" is likely a typo and that there are inconsistencies in the code. However, there is no consensus on the overall correctness of the code or the author's understanding of the instruction set.

Contextual Notes

Participants note limitations regarding the clarity of operand definitions and potential issues with overlapping memory locations, but do not resolve these concerns.

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 what 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 :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
410
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
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