Search code examples
assemblyx86-16tasm

Can't seem to understand JZ, not working as expected


I'm trying to figure why the jz equ command is not working in my code. Clearly, the command before it (xor bl, bh) sets the zero flag to 1 and still when I debug the program it doesn't jump into equ.

What am I missing here?

I can't seem to make jump zero work in any way... Tried other code tests with jump zero that failed too I'm missing something about the jump zero command but what is it?

.model small
.stack 10h
.data
.code
start:
      mov ax, @data
      mov ds, ax

      mov ch, 0
      mov cl, 0
      mov bh, 21h
      mov bl, 21h
      xor bl, bh
      jz equ
      not ch
      and ch, cl
      jmp end
equ:
      add bh, bl
end:
      mov ah, 4ch
      int 21h

End start

Solution

  • jz equ
    

    The above equ is a directive that tells the assembler to substitute all instances of the text "jz" by no text at all. That's the reason why your conditional jump is not executed. It's not even there!

    These are the instructions that your program executes:

     mov ax, @data
     mov ds, ax
     mov ch, 0
     mov cl, 0
     mov bh, 21h
     mov bl, 21h
     xor bl, bh    \
                    |No jump was encoded here
     not ch        /
     and ch, cl
     jmp end
    
    end:
     mov ah, 4ch
     int 21h
    

    Simply choose a better name for this label:

      xor bl, bh
      jz  IsZero
      not ch
      and ch, cl
      jmp end
    IsZero:
    

    I hope you didn't conclude the jz equ not working by just looking at the registerdump at the conclusion of your program because all registers will have the same final values regardless. BX=2100h CX=0000h

    Neither branch changes register values in the end:

    • fall through

      not ch         0 becomes 255
      and ch, cl     255 returns back to 0
      
    • jump if zero

      add bh, bl     21h is raised by 0
      

    .stack 10h
    

    Better setup a larger stack. 16 bytes is somewhat unrealistic. I suggest 256 bytes as a minimum.