In my Z80 CPU emulator I am using macros to handle certain tasks and I need to be able to branch without using labels as I will get a ton of symbol redifinition errors. Looks something like this:



or al,al
jz $+1
stc
HEREISWHEREIWANNAJUMP:
xor dh,dh




This is not actual code BTW.
So does that +1 mean 1 byte or 1 instruction? IF not how do I get the assembler to skip 1 instruction?
Posted on 2004-03-10 20:51:18 by x86asm
Use LOCAL labels:




myMAcro macro

LOCAL _L1

or al,al
jz _L1
stc
_L1:
xor dh,dh

endm



This way the macro can be expanded as many times as you wish without generating symbol redefinition errors.
Posted on 2004-03-10 21:08:09 by Waka
One byte.

To skip one instruction you need to know the size of the next instruction and add that.
Posted on 2004-03-10 22:27:55 by ThoughtCriminal
Jumps of that sort are usually byte size. If you wanna jump several instructions, and you don't know their sizes, you need to use labels.

With regard to your example, I would do this:



or al,al
jz @f
stc
@@:
xor dh,dh


@@: is a special label, and @F will jump forward to the next @@:
@B will just back (up) to the next @@:
Posted on 2004-03-10 22:31:48 by SubEvil
x86asm, your syntax is correct as far as I know... It should work..
Posted on 2004-03-11 05:55:32 by roticv




Label: jz $+1


That's the bug: "The target instruction is specified with a relative offset (a signed offset relative to the current value of the instruction pointer in the EIP register)." It means that processor sign-extend the relative offset (+1) and adds it to EIP, that is to the offset of "Label".
In your case, JZ $+1 performs jump within itself, because JZ $+1 is encoded in two bytes. Exactly, you get instuction with opcode 01F9h add ecx,edi and the code gets out-of-synchronization.
If you want be 100% sure, hard-code it using code 74h,02h+size_of_next_instructions (JZ short). BTW, I often use JZ $+2+size_of_next_instructions (in MASM) and it works (for size_of_next_instructions <= 7fh-2, of course).
Posted on 2004-03-11 13:46:09 by MazeGen
Why not use labels ?

MazeGen and roticv,

What would be the advantage of using JZ $+2+size_of_next_instructions ?
Posted on 2004-03-11 17:00:55 by Waka
Well, it can reduce the time and memory requirements when assembling.
Posted on 2004-03-11 19:05:26 by Sephiroth3
Try the 5 byte challenge by TheSvin to set ax to -1 if parity bit is set else set eax to -1. It is useful for size optimisation... sometimes better not to use labels.
Posted on 2004-03-12 03:46:17 by roticv

Why not use labels ?

MazeGen and roticv,

What would be the advantage of using JZ $+2+size_of_next_instructions ?

I assume he has his own good occasion.
Posted on 2004-03-12 13:20:14 by MazeGen
I thought his problem were symbol redifinition errors which appeared when he expanded his Z80 core's instruction implementation macros multiple times. Which could be easily solved by using local labels. That's what I did when coding my own Z80 emulator.
Posted on 2004-03-12 14:40:21 by Waka
Thanks guys, sorry for not checking in for a while, had a stressful week :(
Posted on 2004-03-12 14:48:21 by x86asm
This is exactly what LOCAL labels are for. The assembler generates a unique label for every LOCAL. So you can use the same macro over and over, and it generates a different label each time.

You need to be careful using Anonymous Labels - @@ - inside macros. See the MASM documentation:

http://webster.cs.ucr.edu/AsmTools/MASM/MASMDoc/ProgrammersGuide/Chap_09.htm

:)
Posted on 2004-03-12 15:55:15 by S/390