Jump to specific line in x86 assembly language
Asked Answered
D

2

6

In x86 assembly language, is it possible to specify a jump to a specific line number? Here, I'm trying to use the jmp instruction to the line number 3. (I don't yet know of a way to pass a label as a parameter to a function, so I'm trying to use a line number instead of a label in this case.)

.686p
.model flat,stdcall
.stack 2048
.data

ExitProcess proto, exitcode:dword
.code

start:

jmp 3; this produces the error shown below

mov ax, 0
mov bx, 0
mov ah, 1

invoke  ExitProcess, 0
end start

The code above produces the error 1>p4.asm(11): error A2076: jump destination must specify a label.

Deafening answered 16/4, 2013 at 4:26 Comment(6)
Your program doesn't seem to have a line 3.Emmeram
It would, if the whitespace was counted.Deafening
But jumping to whitespace is meaningless, right? What exactly are you trying to do?Emmeram
@CarlNorum OK, I've fixed it- the program has a line 3 now.Deafening
So line 3 is mov bx, 0?Emmeram
@CarlNorum Yes, I think so.Deafening
E
10

You can just put a label at that line. Depending on your assembly language dialect, you might be be able to use a numeric local label, or you might need to use symbolic labels. A possible example follows. I have only NASM here to test, so I'm not really sure this example will map well to MASM, but you should get the idea:

start:
    jmp .line3
    mov ax, 0
.line3:
    mov bx, 0
    mov ah, 1

NASM uses a leading . to identify local labels.

Emmeram answered 16/4, 2013 at 4:27 Comment(4)
That might work. However, I still don't know of any way to pass a label as a parameter to a function in x86 assembly (and that's why I'm trying to use a number instead of a label).Deafening
@AndersonGreen, a label is usable pretty much as a pointer in most cases.Emmeram
In that case, how could the label be passed as a parameter to a function?Deafening
That depends on the calling convention, but for ia32, pushing the appropriate address to the right spot on the stack will do it. For x86_64, you'll want to put it in the appropriate register. Something like lea rcx, label.Emmeram
U
0

try this:

s1:   add ax,1
      jmp $-3

this can make you program jump to the s1. Be careful,the number is the offset of code,not the number of line.( instruction 'add ax,1' is 3 bytes) if you want to make a indirect jump,try this:

jmp far [address]
address dw 0123h ;the ip
        dw 5678h ;the cs

more imformation,google indirect jump.

Uranalysis answered 16/4, 2013 at 6:5 Comment(2)
In 16-bit code add ax,1 is 3 bytes 83 c0 01, but in 32-bit code it's 4 bytes 66 83 c0 01. In 32-bit code 83 c0 01 disassembles as add eax,1. So in this case jumping in the middle of the 66 83 c0 01 instruction produces a valid instruction (db 0x66 is a prefix), but gives a different instruction. In the rare case you really wanted to jump to an offset that's inside an instruction, it's best to encode the instruction yourself (db 0x66, 0x83, 0xc0, 0x01), so you know the precise encoding used.Ravi
It's a good habbit to use bits 16or bits 32,this can avoid the 0x66 problem.Uranalysis

© 2022 - 2024 — McMap. All rights reserved.