Page 1 of 1

Extending my jumps?

Posted: Sun Dec 12, 2004 6:40 am
by Metallic-Red
I'm using assembly but my code has become too large that the jumps won't work because they are too far away. How can I fix this or set my code up so that it will work?

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 7:00 am
by IRBMe
By giving your code a good structure. You shouldn't need to be jumping over huge sections of code left right and centre. That totally ruins all of the optimisation features built into the CPU to take advantage of the phenomenon of program locality.

For a start, try to split some of your lengthy bits of code into neat functions which can be called with a single instruction

Code: Select all


jmp start

foo:
  ; some code
  ret

bar:
  ; some code
  ret

start:
  call foo
  call bar
  ; and so on..

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 7:02 am
by Perica
..

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 7:13 am
by Metallic-Red
I am using the NASM compiler and my code is part of a DOS style OS I've been working on.

I have the code to set up my commands:

MOV SI, strCommand
MOV DI, cmdCls
MOV CX, 4
REPE CMPSB
JE COMMAND_CLS

MOV SI, strCommand
MOV DI, cmdDate
MOV CX, 5
REPE CMPSB
JE COMMAND_DATE

etc.

But I have 6 functions and some of them have lengthy code so some of these tests can't reach their designated functions. I hope I make sense?

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 7:29 am
by Perica
..

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 7:37 am
by Metallic-Red
Thanks! That worked. Now all my code works perfectly.

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 7:55 am
by bubach
Maybe you should have a table with all commands and another table with the function address to each command?
That way you can have a single loop to compare the commandline against the command-table (and keep count), then call the address in the other table.
Do you understand what i mean?

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 10:09 am
by IRBMe
instead of:

Code: Select all

JE COMMAND_CLS
Make COMMAND_CLS a function, and call it instead. I also agree that you should do what bubach suggested.

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 10:19 am
by Candy
Perica wrote: If I've understood you correctly, then placing the word NEAR after your jump instructions should do the trick. For example:

Code: Select all

je NEAR jump_destination
Converting them to inverse logic and using a normal jump would also work:

Code: Select all

jne next_try_1
jmp jump_destination

next_try_1:

Re:Extending my jumps?

Posted: Sun Dec 12, 2004 10:46 am
by Brendan
Hi,
Perica wrote: If I've understood you correctly, then placing the word NEAR after your jump instructions should do the trick. For example:

Code: Select all

je NEAR jump_destination
Turning on NASM's multi-pass optimizer also works (or at least it does for versions released in the last few years). Check the NASM manual ("Section 2.1.16 The -On Option: Specifying Multipass Optimization").

I generally use something like this in my makefiles:
NASM = nasm -i$(INCDIR) -O99 -w+orphan-labels -f bin

It's easier than manually adding the NEAR keyword each time NASM gives an error. The optimizer doesn't change your instructions, it only finds better encodings for the instructions - the final binary will still match a disassembly line for line (useful for Bochs debugging) :).


Cheers,

Brendan