I am working on my first NASM program, and while trying to figure out the not instruction, I realized that instead of reversing the bit 0, it was reversing the byte 00000000. How would I tell it to work with a bit or otherwise fix this? Here is my code…
section .text
global start
start:
mov eax, 255
not eax
push eax
mov eax, 0x1
sub esp, 4
int 0x80
Feel free to give me pointers also on my assembly coding, as I don’t want to get into any bad habits.
In most computer architectures (including the x86), a bit is not a directly addressable unit of memory. The smallest unit that you can directly refer to is a byte, which happens to contain 8 bits on the x86. You have not stated what you’re exactly trying to accomplish, so I’m not able to give you an exact solution to your problem, but working with single bits (or groups of bits) is most often achieved by masking out the bits that are of no interest with the
ANDinstruction, eventually shifting the value left or right, and then doing the processing.If you want to actually get the value of the n-th bit in a register, then you’re most probably looking for the instruction
BT. It stores the value of the n-th bit in the Carry Flag.When it comes to other tips : the
pushinstruction decrements the stack pointer by the number of bytes pushed to the stack. This is a characteristic of the x86 architecture – the stack grows, by design, downwards. Therefore, if you want to free some space on the stack, you doadd esp, number_of_bytes, notsub(the way you did), which just reserves more space on the stack.