Howdy, Stranger!

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


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

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

pop dx
pop ax

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


  • 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]
    mov cx, counter
    loop loop1

    mov cx, counter
    sub cx, 1
    jnz loop2
  • 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.