Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

JMP versus LOOP

vikingnoisevikingnoise Member Posts: 15
I've been learning 16-bit x86 assembly over the past year or so, and I've noticed that several code examples I've come across use LOOP instead of JMP in a conditional loop that doesn't rely on the value in CX, like printing a NULL-terminated ASCII string. For example:
[code]WriteASCIIZ proc
push ax
push dx

WriteLoop:
lodsb
or al,al
jz Done
mov dl,al
mov ah,02h
int 21h
loop WriteLoop

Done:
pop dx
pop ax
ret

WriteASCIIZ endp
[/code]
Is there an advantage to using LOOP instead of JMP in that context?

Comments

  • BretBret Member Posts: 114
    By definition, LOOP requires (and modifies) the CX register, so it actually matters very much what CX is. In addition, by using LOOP, the value of CX will be changed when the subroutine exits. So, unless you're actually using CX for something (like to keep track of how many characters you printed), you should include a push cx/pop cx.

    For this subroutine to work properly with LOOP, CX must be set to something appropriate before you call the subroutine. CX could be set to a really large value so that the LOOP won't fail, or set to a "maximum allowed string size" (something like 79 so that the entire string will fit onto a single screen line without scrolling).
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Also, LOOP is a slightly slower than SUB/JNZ combination:[/color]
    [code]
    ;
    ; SLIGHTLY SLOWER
    ;
    mov cx, counter
    loop1:
    ...
    loop loop1

    ;
    ; SLIGHTLY FASTER
    ;
    mov cx, counter
    loop2:
    ...
    sub cx, 1
    jnz loop2
    [/code]
  • vikingnoisevikingnoise Member Posts: 15
    Okay, that's what I thought. Thanks for clearing it up :)
  • vikingnoisevikingnoise Member Posts: 15
    This post has been deleted.
  • vikingnoisevikingnoise Member Posts: 15
    This post has been deleted.
Sign In or Register to comment.