0

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?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
dastan
  • 1,006
  • 1
  • 16
  • 36
  • 2
    yes, this instruction uses only 1 byte to store offset – Iłya Bursov Apr 26 '14 at 03:24
  • @Lashane Then why so? If we use more bytes to store offset, we'll avoid a lot of 'jump destination too far' errors, isn't it better? – dastan Apr 26 '14 at 03:30
  • But then code size increases, and that's bad. If most of your loops are tiny, why waste the extra byte? – nneonneo Apr 26 '14 at 03:31
  • @nneonneo It makes sense. But how if I have to write code which violate the rule in a loop? (I'm new to Assembly.) – dastan Apr 26 '14 at 03:35
  • 1
    Use a longer jump instruction. x86 has a bunch of jump instructions that take different arguments. – nneonneo Apr 26 '14 at 03:39
  • 1
    You should know that `loop` is deprecated: http://stackoverflow.com/questions/1756290/loop-loope-loopne http://stackoverflow.com/questions/4880461/is-there-a-list-of-deprecated-x86-instructions (You may already know that and may just be asking for the sake of complete knowledge, but I thought I'd post it just in case.) – Tyler Apr 26 '14 at 03:55
  • @Tyler - `loop` is NOT deprecated. It just was not optimized like small set of most frequently used instructions such as `mov` and `add`. Feel free to use `loop` whenever it looks comfortable, excluding tight loops when speed is really matters. – Egor Skriptunoff Apr 26 '14 at 07:32
  • 2
    `loop` is one of a number of stupid instructions included in the x86 instruction set to appease the *goto is evil* crowd and introduce structured programming to assembly (see also `enter/leave`, `bounds` etc.). It's a spectacular failure and a waste of encoding space. – EOF Apr 26 '14 at 09:50
  • @EOF - wow, there is a _goto is evil crowd_ among _assembly_ programmers? – BeeOnRope May 28 '18 at 19:34
  • 1
    @BeeOnRope Well, tbh the comment was a bit hyperbolic even when I wrote it, and I've come to change my views on some of this. `loop` is not intrinsically stupid, and it's only slow because Intel doesn't care. On AMD, it's apparently decent, which makes sense because its really just a fused "compute/conditional branch", potentially being a bit easier to decode than doing the macro-op fusion in the decoders. `enter` is *weird*, `leave` is useful, and there is a genuine need to have efficient bounds checking, so even though `bounds` is a failure it's an understandable one. – EOF May 29 '18 at 11:54

1 Answers1

2

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • *instruction lengths are different for 86-286 and 386+*. No, instruction-lengths are different for 16-bit mode vs. 32 or 64-bit mode. (jmp rel8 / rel16 vs. jmp rel8/ rel32). A Skylake or Ryzen in 16-bit mode agrees with a 286 about instruction lengths. – Peter Cordes May 28 '18 at 05:46