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:
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?
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?
Use LOCAL labels:
This way the macro can be expanded as many times as you wish without generating symbol redefinition errors.
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.
One byte.
To skip one instruction you need to know the size of the next instruction and add that.
To skip one instruction you need to know the size of the next instruction and add that.
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:
@@: is a special label, and @F will jump forward to the next @@:
@B will just back (up) to the next @@:
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 @@:
x86asm, your syntax is correct as far as I know... It should work..
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).
Why not use labels ?
MazeGen and roticv,
What would be the advantage of using JZ $+2+size_of_next_instructions ?
MazeGen and roticv,
What would be the advantage of using JZ $+2+size_of_next_instructions ?
Well, it can reduce the time and memory requirements when assembling.
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.
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.
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.
Thanks guys, sorry for not checking in for a while, had a stressful week :(
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
:)
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
:)