Extending my jumps?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Metallic-Red

Extending my jumps?

Post 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?
IRBMe

Re:Extending my jumps?

Post 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..
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Extending my jumps?

Post by Perica »

..
Last edited by Perica on Tue Dec 05, 2006 9:34 pm, edited 1 time in total.
Metallic-Red

Re:Extending my jumps?

Post 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?
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Extending my jumps?

Post by Perica »

..
Last edited by Perica on Tue Dec 05, 2006 9:34 pm, edited 1 time in total.
Metallic-Red

Re:Extending my jumps?

Post by Metallic-Red »

Thanks! That worked. Now all my code works perfectly.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:Extending my jumps?

Post 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?
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
IRBMe

Re:Extending my jumps?

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Extending my jumps?

Post 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:
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Extending my jumps?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply