flat assembler
Message board for the users of flat assembler.

Index > Main > add by sub vs sub by add

Author
Thread Post new topic Reply to topic
Ali.Z



Joined: 08 Jan 2018
Posts: 718
Ali.Z 17 Aug 2024, 03:56
title..

what is the advantage of one over the other? or properties?

perhaps eflags overhead when using sub by add? or in both cases?

anything else?, i cannot think of anything else, so i dont really have full coverage.

_________________
Asm For Wise Humans
Post 17 Aug 2024, 03:56
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 17 Aug 2024, 04:59
With regard to size:
Code:
add eax, -0x80  ; 83 C0 80
sub eax, 0x80   ; 2D 80 00 00 00    
... any instruction with an offset - we can change the direction of work, or order of operations.

Performatively, they use the same execution units.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 17 Aug 2024, 04:59
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 718
Ali.Z 17 Aug 2024, 05:49
bitRAKE wrote:
With regard to size


thanks i missed this one, an important note to consider for size optimizations

_________________
Asm For Wise Humans
Post 17 Aug 2024, 05:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 17 Aug 2024, 08:13
The carry flag will be inverted if the second argument is not zero.

The other flags are the same.

So if you need to use cmc after add/sub, then you can instead use sub/add and omit the cmc.
Post 17 Aug 2024, 08:13
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 718
Ali.Z 17 Aug 2024, 17:16
that implies carry and borrow if i understand correctly.

_________________
Asm For Wise Humans
Post 17 Aug 2024, 17:16
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 29 Aug 2024, 06:08
revolution wrote:
So if you need to use cmc after add/sub, then you can instead use sub/add and omit the cmc.
I remembered a use case for this that is quite common:

The DIV instruction quotient rounds to zero. Sometimes we want to round to infinity. One way to do that is to adjust the input number:
Code:
        mov rcx, [.divisor]
        lea rbx, [rcx-1]
        add rax, rbx
        adc rdx, 0
        div rcx    
... another way is to adjust the output based on the remainder:
Code:
        div [.divisor]
        cmp rdx, 1      ; sub rdx, 1
        cmc
        adc rax, 0    
... knowing the carry flag flips we can reduce the code to:
Code:
        div [.divisor]
        add rdx, -1
        adc rax, 0    
... and we can see quite clearly that the carry is only set when the remainder is NOT zero.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 29 Aug 2024, 06:08
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.