Masoud Fard
Masoud Fard

Reputation: 167

near jump changed to Short jump automatically

I need near jump(E9 XX XX) and specify that in code, but TASM (and MASM) changed that to Short(EB XX NOP) after assemble.

MAIN SEGMENT BYTE

ASSUME CS:MAIN,DS:MAIN,SS:NOTHING

ORG 100H


HOST:

jmp NEAR PTR VIRUS_START 

db ’VI’

mov ah,4CH

mov al,0

int 21H ;terminate normally with DOS

COMFILE DB ’*.COM’,0 ;search string for a com file
VIRUS_START:

Upvotes: 3

Views: 1649

Answers (3)

rprokop
rprokop

Reputation: 31

You can work around your assembler by manually encoding a jmp near rel16:

db  0E9h                    ;  JMP NEAR opcode
dw  VIRUS_START-$-2         ;  relative address

$ is the absolute address of current instruction (dw).
($+2) address of next instruction (after our jmp).

(VIRUS_START - ($+2)) - difference between target address (VIRUS_START) and next instruction. It will be added to IP register during execution JMP instruction.

Upvotes: 3

Peter Cordes
Peter Cordes

Reputation: 364160

I don't know the answer for MASM or TASM, but perhaps this will be useful to someone:

In NASM, jmp near VIRUS_START does enforce the long encoding. You can also use stuff like add dx, strict word 1 to force the imm16 encoding instead of the imm8. See http://www.nasm.us/doc/nasmdoc3.html#section-3.7

; ASSUME: I think there's a way to port that to NASM, but IDK how.
ORG 100H

HOST:
jmp NEAR VIRUS_START   ; with override
jmp VIRUS_START        ; without
... ; your code unmodified
VIRUS_START:

assemble with nasm -fbin foo.asm. Then see what we got with ndisasm -o 0x100 foo (which only knows about flat binaries):

00000100  E91000            jmp 0x113
00000103  EB0E              jmp short 0x113

Upvotes: 1

Sep Roland
Sep Roland

Reputation: 39166

An easy way to enforce the use of the near jump instead of the short jump is having enough bytes to jump over!

  • Either use some padding like:

    COMFILE DB ’*.COM’,0 ;search string for a com file
    padding db 127 dup (0)
    VIRUS_START:
    
  • or else add some useful subroutine(s) before the label VIRUS_START

A further possibility is to encode the jump manually.
Just write db 0E9h, 14, 0

Upvotes: 3

Related Questions