Question

I'm reading the book Assembly Language for Intel-Based Computers Fifth Edition.
The author said like the TITLE but he didn't explain.
Is it relative about instruction length?

Was it helpful?

Solution

think about this in next way, to create loop you need:

mov cx, iterations
label:
; loop body
dec cx ; this instruction takes 1 byte
jnz label ; this instruction takes 2 for short and 4 for long

so, you have choice:

  1. use dec + short jmp, 3 bytes
  2. use dec + long jmp, 5 bytes

as soon as most of the loops were (are?) quite short - special short-cut instruction was introduced to reduce size (640kb enough for all):

loop which takes only 2 bytes and works as dec + short jmp

so, loop is special "edge" case which I suppose is not used right now (UPDATE: it can be supposed as deprecated (because it's slow on modern CPUs), and it makes sense as loop forces you to use CX as loop variable, so you cannot create double loop or use another register)

anyway second part of your question - what to do if your loop body is greater:

  1. try to optimize it, there are many different techniques, like replace mov ax,0 (3 bytes) with xor ax,ax (2 bytes) and so on
  2. re-factor your code into functions (this could actually increase your final size, but reduce body size sometimes)
  3. use dec + long jmp

NOTE: these instruction lengths are for 16-bit mode; 32-bit and 64-bit modes use rel32 for long jumps, rather than rel16.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top