alonp
alonp

Reputation: 1337

How to convert a positive number to negative in assembly

I want to convert a positive number to negative in assembly, I need a code sample

The input is in hex sdword

Thanks

Upvotes: 11

Views: 58496

Answers (3)

Madhur Ahuja
Madhur Ahuja

Reputation: 22709

You can take the two's complement of the number. I.e. Bitwise NOT followed by addition of one.

Data Segment
  num dw 00000010B
Data Ends

Code Segment
  Assume cs:code, ds:data

  Begin:
    mov ax, data
    mov ds, ax
    mov es, ax
    mov ax, num
    NOT ax
    add ax, 00000001B

  Exit:
    mov ax, 4c00h
    int 21h
Code Ends
End Begin

Upvotes: 1

Necrolis
Necrolis

Reputation: 26181

Well, there are two ways to do this, best would be to load the number into a register, then use the NEG instruction as Hans mention, ie: NEG EAX would negate eax. The other way would be XOR EAX,EAX SUB EAX,EDX where edx contains the number you want to negate

Upvotes: 20

Tim
Tim

Reputation: 5421

Here's an online resource which shows how to do this for 80x86 processor: http://webster.cs.ucr.edu/AoA/DOS/ch01/CH01-2.html

Upvotes: -3

Related Questions