Загрузка...

x64 Assembly Tutorial 29: Test Instruction

That's enough of the stack and the C calling convention. This was fiddley stuff and I'd like now to return to basic Assembly. There's still a bunch of x86 instructions we should go through before we move onto bigger things.

This tutorial is all about the TEST instruction. It allows us to test if particular bits in registers or memory are set to 1 or 0. We could use the AND instruction to accomplish much the same but the AND instruction alters the value in the first operand where the TEST instruction does not. TEST just sets the flags register.

You can test if single bits are set (ie. equal to 1) in memory or a register or you can check the sign. You can also see if two registers share any 1 bits. You could check if a value is divisible by any power of 2 without performing a divide!

Most of the time you mask the values you don't care about with 0 in the second operand and the bits that you want to read, you set to 1. To test if bits 5 or 3 are set in AX you can just use a second operand with only 1's in bits 3 and 5:

test ax, 10100b
jnz BitsSet

And it will only take the branch if either of the two bits is set!

I also want to say that this instruction clears the CF (carry flag) and OF (overflow) flags to 0.

Видео x64 Assembly Tutorial 29: Test Instruction канала Creel
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять