Assembly Language – How to do Modulo?

If your modulus / divisor is a known constant, and you care about performance, see this and this. A multiplicative inverse is even possible for loop-invariant values that aren’t known until runtime, e.g. see https://libdivide.com/ (But without JIT code-gen, that’s less efficient than hard-coding just the steps necessary for one constant.) Never use div for known powers of 2: it’s much slower than and for remainder, … Read more

JNZ & CMP Assembly Instructions

JNZ is short for “Jump if not zero (ZF = 0)”, and NOT “Jump if the ZF is set”. If it’s any easier to remember, consider that JNZ and JNE (jump if not equal) are equivalent. Therefore, when you’re doing cmp al, 47 and the content of AL is equal to 47, the ZF is set, ergo the jump (if Not … Read more

Assembly – JG/JNLE/JL/JNGE after CMP

When you do a cmp a,b, the flags are set as if you had calculated a – b. Then the jmp-type instructions check those flags to see if the jump should be made. In other words, the first block of code you have (with my comments added): would jump to label1 if and only if al was greater than dl. You’re probably better … Read more

x86 Assembly pointers

As has already been stated, wrapping brackets around an operand means that that operand is to be dereferenced, as if it were a pointer in C. In other words, the brackets mean that you are reading a value from (or storing a value into) that memory location, rather than reading that value directly. So, this: … Read more

movq assembly function

movq (assuming you’re talking about x86) is a move of a quadword (64-bit value). This particular instruction: looks very much like code that will walk up through stack frames. This particular instruction grabs the quadword pointed to by the current stack pointer, and loads it into the stack pointer, overwriting it. By way of example, this … Read more

Difference between JA and JG in assembly

As Intel’s manual explains, JG interprets the flags as though the comparison was signed, and JA interprets the flags as though the comparison was unsigned (of course if the operation that set the flags was not a comparison or subtraction, that may not make sense). So yes, they’re different. To be precise, ja jumps if CF = 0 and ZF … Read more