flat assembler
Message board for the users of flat assembler.

Index > Main > How to extract one bit from a register?

Author
Thread Post new topic Reply to topic
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Hello,

how to extract the n-th bit from a general purpose register?
I think that I should use a shift instruction, or a logical instruction, but there are many instructions of this kind.

Maybe, the technique to do that is to put the n-th bit in a low part of a register as follow: [0000000n]
where n is the bit that I want get.

Can you help me?
What instruction would be useful for me?

Regards.

_________________
;; emc
Post 28 Aug 2011, 13:14
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6038
Location: Poland
MHajduk
You may consider use of the bt instruction for bits testing.
Post 28 Aug 2011, 14:56
View user's profile Send private message Visit poster's website Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Thanks, so this following code:
Code:
mov eax, 42
bt ecx, eax
    

This code stores the 42nd bit of ECX in CF flag?
Am I right?
Post 28 Aug 2011, 15:19
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
ecx is only 32 bits. maximum value is then 31 for a 32 bit register.
bit 0 to 31.

42 is illegal, but can work if you test on a memory operand, and it will let scan very large bitstreams in memory.
but for BT on registers, it is limited to the register size, sorry, a 32 bit register cannot hold more than 32 bits.
Post 28 Aug 2011, 15:22
View user's profile Send private message Visit poster's website Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Yes, I was distracted, sorry for the 42.

But this code is alright with 64-bits registers (or a value lesser than 31)?
Post 28 Aug 2011, 15:28
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
RTFM (Read The Fucking Manual)
en français, lire le foutu manuel, car justement, tout et dit sur les instructions dans les manuels du programmeur disponibles sur les sites d'intel, AMD, ou autre fondeur.

pour un debutant, le manuel amd est plus facile à aborder, les instructions sont expliquées avec des dessins, ce qui facilite la comprehension, par rapport aux explications à base de pseudo code d'intel.

bienvenue dans le monde magique de fasm.
Post 28 Aug 2011, 18:52
View user's profile Send private message Visit poster's website Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Je ne connaissais pas BT avant, c'est justement grâce au "foutu manuel" que j'ai pu savoir ce que ça faisait (donc je le lis, et plutôt bien je pense. On ne peut pas deviner à propos de CF).
Je demandais juste une confirmation pour savoir si j'avais bien compris, où est le mal ?

--

I didn't know the BT instruction earlier, but it's the "fucking manual" that given to me a knowledge about BT (so, I read it; I didn't could guess for CF).
I just asked if my code was alright because I wanted a confirmation. Nothing bad?

edfed wrote:
bienvenue dans le monde magique de fasm.

Merci bien edfed.
Post 28 Aug 2011, 19:18
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
pas de mal, juste la reponse la plus pertinente, car si à chaque instruction, on demande comment ça marche, on a pas fini. il y a plusieurs centaines d'instructions X86

nothing bad, just the more pertinent answer, because if for each instruction, man ask how it works, it will take mounth, there are several hundred x86 instructions.

Smile

BT have BTC, BTS and BTR variations:
Complement, complement the tested bit
Set, set the tested bit to 1
Reset, set the tested bit to 0
Post 28 Aug 2011, 19:55
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji
Quote:
But this code is alright with 64-bits registers? (or a value lesser than 31)?

donc je le lis, et plutôt bien je pense.
bein faut lire mieux alors, ou plus, ou plus longtemps, ou plus fort ... sais pas.

Je taquinne emc, rien de plus ...
pas pu m'empêcher (oui, je vais au coin, punis) Wink

c'est vrai que de demander, même si c'est écrit noir sur blanc,
ça rassure. Ceci dit, quand il y a un doute, rien de tel qu'un bon debugger
et de tester ça en "vrai", y'a pas mieux pour apprendre.



Image

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 28 Aug 2011, 20:21
View user's profile Send private message Send e-mail Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Ok sorry about that, I am yet beginner, so I feel the need to be reassured...
But no problem, I shall try to test alone in the future.

I did the program that I wanted to do thanks to you. Wink

Thanks!

PS: je penserai à mon gdb ouadji !
Post 28 Aug 2011, 20:24
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji

rien que de la taquine emc, rien de plus.
bein si que tu peux demander, un forum ça sert à ça.
Juste que les réponses étaient un poil évidentes
par rapport au texte du bouquin.
ceci dit, facile de taquiner quand on épluche lesdits bouquins depuis 15 ans.
Au début, c'était tout pareil que toi.
Donc, reviens poser tes soucis quand tu veux.

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 28 Aug 2011, 20:33
View user's profile Send private message Send e-mail Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Merci ouadji, c'est vrai que moi j'épluche le manuel que depuis hier, mais je vais songer à regarder un peu le manuel AMD (pour ses dessins) comme edfed le conseille, ça sera peut-être moins ardue.
Post 28 Aug 2011, 20:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17667
Location: In your JS exploiting you and your system
revolution
And also remember the instruction PEXT. It can be used to transfer the desired bit directly to a register and doesn't affect the flags.
Post 28 Aug 2011, 23:52
View user's profile Send private message Visit poster's website Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Here, affecting CF suited me, to use JC/JNC then. But I'll remember the PEXT instruction, thanks revolution.
Post 29 Aug 2011, 00:06
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
PEXTR* extracts full bytes/words/dwords, single bits still need to be extracted separately through use of BT or AND,SHR instructions.
Post 29 Aug 2011, 06:01
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
Unfortunately, I don't have SSE4.1 and AVX so I can't use PEXTR*
Post 29 Aug 2011, 14:13
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17667
Location: In your JS exploiting you and your system
revolution
And you'll need AVX2 for PEXT.
Post 29 Aug 2011, 17:55
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
oh Wink nice one, revolution!
I didn't catch that. I plan on investing in AVX2 CPU, because I haven't got anything newer than Penryn.
It's got some really nice integer instructions. Maybe I can help make the world better helping Tomasz, Agner etc. Smile
Post 31 Aug 2011, 16:04
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.