I'm reading some assembly(att sintax , x86 code, GAS) code and I found this instruction jnz .
or jmp .
, I know what jnz or jmp mean but I don't get where it goes when jnz .
or jmp .
is called, I don't have a way to debug or trace, I believe it goes to the next instruction but I'm not sure.

- 328,167
- 45
- 605
- 847

- 31
- 1
- 5
-
*I don't have a way to debug or trace*. You can at least assemble and look at the machine code / disassembly. But really, not being able to debug is the problem you need to fix first. You'll be able to solve so many more problems on your own without needing to ask people and wait for an answer. You have a web browser, so https://www.onlinegdb.com/online_gcc_assembler is an option. – Peter Cordes Jun 11 '20 at 20:09
-
BTW, another way to use `.` is shown in [Calculating padding length with GAS AT&T directives for a boot sector?](https://stackoverflow.com/q/47859273) – Peter Cordes Jun 11 '20 at 20:26
2 Answers
The syntax of JMP
is - JMP label
. Here, label specifies a label to which the code will jump from that point. In the question .
could be considered as the Label
. It is a special symbol which refers to the current address it is assembling into. JMP .
is essentially an infinite loop as the code will keep jumping back to itself infinitely until you get an interrupt .
The JNZ
statement is a conditional jump statement which will work as JMP
when the zero flag is not set (Z = 0
). If the zero flag is set, the program will simply skip the Jump and just move on to the next line of code.
So JNZ .
could be considered as move to the next line if Z = 0
(zero flag is not set) and if Z = 1
, then keep looping infinitely until Z
flag is reset or until you get an interrupt.
Hope this clears your problem.

- 5,583
- 3
- 15
- 32
https://sourceware.org/binutils/docs/as/Dot.html#Dot
The special symbol
.
refers to the current address that as is assembling into.
In the context of code, .
is the address of the current instruction. So jmp .
jumps back to itself; this is an infinite loop. jnz .
would jump back to itself if the zero flag is not set. Since the jump doesn't affect the zero flag, the effect of executing this instruction for the first time would be to loop forever if the zero flag was not set, and to just go on to the next instruction if it was.

- 48,811
- 6
- 54
- 82
-
The real question is, why would anyone do that in this day and age? – Seva Alekseyev Jun 11 '20 at 20:00
-
@SevaAlekseyev: to wait for an interrupt in a toy project or a microcontroller without power-management, I guess. Or at the end of a boot sector freestanding program. (Better to use `hlt` in a loop, though.) – Peter Cordes Jun 11 '20 at 20:10
-
-
@SevaAlekseyev: `hlt` is good for a low-effort approach, but it's still only a C0 sleep or something like that. You need `monitor` / `mwait` to save a lot more power in a deeper sleep. (But either are fine for running in an emulator.) – Peter Cordes Jun 11 '20 at 20:33