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
Thread Post new topic Reply to topic
vanzan



Joined: 27 Apr 2010
Posts: 14
vanzan 29 Apr 2010, 14:50
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
        
Post 29 Apr 2010, 14:50
View user's profile Send private message Reply with quote
a115433



Joined: 05 Mar 2010
Posts: 144
a115433 29 Apr 2010, 15:27
neg?
it will substract it from 0.
Post 29 Apr 2010, 15:27
View user's profile Send private message Reply with quote
vanzan



Joined: 27 Apr 2010
Posts: 14
vanzan 29 Apr 2010, 15:28
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    
Post 29 Apr 2010, 15:28
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 29 Apr 2010, 15:44
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
Post 29 Apr 2010, 15:44
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 29 Apr 2010, 15:56
Madis731 wrote:
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).
Post 29 Apr 2010, 15:56
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 29 Apr 2010, 16:04
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
Post 29 Apr 2010, 16:04
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 29 Apr 2010, 16:19
I'm sorry - fixed - I got so excited about the multiplication usage that I wrote faster than I thought... Sad sorry...
Post 29 Apr 2010, 16:19
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
vanzan



Joined: 27 Apr 2010
Posts: 14
vanzan 29 Apr 2010, 18:30
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 Wink

Edit: (I'm talking about "neg" operater)
Post 29 Apr 2010, 18:30
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4336
Location: Now
edfed 29 Apr 2010, 19:52
in assembly, we don't say comand, but INSTRUCTION. say this to your teacher. Very Happy

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


but franckly, none better than
Code:
neg [ttest]
    
Post 29 Apr 2010, 19:52
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.