flat assembler
Message board for the users of flat assembler.

 Index > Main > What's the easiest way to have the opposite of a number ?
Author
vanzan

Joined: 27 Apr 2010
Posts: 14
vanzan
Hi !

I need to know a simple method to remplace a variable for his opposite number...

For example:

ttest = 1
;TREATEMENT
ttest = -1

I've tried to use IMUL but i failed to do it works...

Are there other solutions ?

My code
Code:
```      MOV [tttest],10
MOV AX,(-1)
IMUL [tttest]

CMP [tttest],(-10)
JE cici

invoke WriteConsole, [stdout], "don't" , 5, NULL, NULL

cici:
invoke WriteConsole, [stdout], "work" , 4, NULL, NULL
```
29 Apr 2010, 14:50
a115433

Joined: 05 Mar 2010
Posts: 144
a115433
neg?
it will substract it from 0.
29 Apr 2010, 15:27
vanzan

Joined: 27 Apr 2010
Posts: 14
vanzan
Oh ! I've forget that the result was stock into BX --"

Correst code:

Code:
```         MOV [tttest],10
MOV AX,-1
IMUL [tttest]
MOV  [tttest],BX

CMP [tttest],(-10)
JE cici

invoke WriteConsole, [stdout], "don't" , 5, NULL, NULL

cici:
invoke WriteConsole, [stdout], "work" , 4, NULL, NULL    ```
29 Apr 2010, 15:28

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
1) neg [ttest]
2A) not [ttest] \ inc [ttest]
2B) xor [ttest],-1 \ inc [ttest]
Actually mul puts the result into dx:ax or edx:eax or rdx:rax, but NOT in the BX
EDIT: my mistake removed

imul bx,[ttest],-1

imul bx,-1

etc.

but why would you use multiplication when its slow and hard to do? NEG-instructions is exactly designed for this!

Last edited by Madis731 on 29 Apr 2010, 16:18; edited 1 time in total
29 Apr 2010, 15:44
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr
imul never touches more than the registers you give it.
Are you sure? imul r/m uses the same pairs as mul (mul doesn't have two- or three-operand encodings, so it uses them always).
29 Apr 2010, 15:56
LocoDelAssembly

Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly
Quote:
1) neg [ttest]
2) xor [ttest],-1

Perhaps I misunderstood your point but if that was supposed to show equivalence then this would be correct:
1) not [ttest]
2) xor [ttest], -1
29 Apr 2010, 16:04

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
I'm sorry - fixed - I got so excited about the multiplication usage that I wrote faster than I thought... sorry...
29 Apr 2010, 16:19
vanzan

Joined: 27 Apr 2010
Posts: 14
vanzan
Quote:

but why would you use multiplication when its slow and hard to do? NEG-instructions is exactly designed for this!

Because I didn't learnt this very useful "command" at my IT school ^^

Thanks, a lot ! It can't be more simple

Edit: (I'm talking about "neg" operater)
29 Apr 2010, 18:30
edfed

Joined: 20 Feb 2006
Posts: 4251
Location: Now
edfed
in assembly, we don't say comand, but INSTRUCTION. say this to your teacher.

another way:
Code:
```xor eax,eax
sub eax,[ttest]
```

but franckly, none better than
Code:
```neg [ttest]
```
29 Apr 2010, 19:52
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum